Struggling with Pololu wheel encoder language

Hello there.

I am trying to build a Maze exploration robot from scratch and have purchased the Baby Orangutan 328P and USB programmer. I am reasonably new to C programming and have never attempted a build from completely new before.

I’ve recently added the wheel encoders and so have been trying to use the AVR library get some information from these sensors. I do not have an LCD or serial connection, but have confirmed that i am getting reliable square wave outputs from the encoders using a scope. I have connected the encoders to pins PC2, PC3, PC4, PC5.

The problem - The problem i have is that i cannot get the code to work for me. What i need to know, is the line of code “encoders_get_counts_m1();” returning an increasing counting value that i can load into a variable, compare and manipulate, or is it something completely different? The library example shows the value being loaded in “print_long” but that doesn’t help me i’m afraid.

This is what i have written in an attempt to get some kind of return.

int left_wheel=0;

	slow_fwd(); //function to set motors at 50% speed
	stop(); // function to stop motors

There is more to the code, but i didn’t want to bore you with the details for what i am hoping is a simple to answer question. Thanks in advance and feel free to call me a fool. :smiley:


Can you tell us what actually happens when you run that code?

What happens if you change it to something like:

unsigned int left_wheel=encoders_get_counts_m1();   // initialize left_wheel to current count
slow_fwd(); //function to set motors at 50% speed

while(encoders_get_counts_m1() - left_wheel < 50); // wait for 50 encoder counts
stop(); // function to stop motors

while (1);  // loop here forever; you should not let your program reach the end of main()

I suggest you also try changing the 50 to larger values (e.g. 200, 1000) to see what happens. By the way, depending on how your encoder is connected, spinning the motor “forward” might cause the encoder counts to decrease, which will instantly cause the while loop to exit (an unsigned integer with a value of -1 is considered greater than 50). If things don’t work, try this code while making the motor spin in the opposite direction.

Edit: I just noticed that you are using the wrong pins in your code (e.g. the library treats pin 2 as PD2). You should use our IO_x macros to indicate the pins as I have now done in my code example above. If you still have problems, please tell me exactly what happens when you run the code and let me know how you have everything connected (a picture might help). Also, you might find the user LED a convenient way to signal events and help you debug your program.

- Ben

Thanks Ben, I was setting up the ports incorrectly. I have now used the line below to get PortB to take the inputs.