Question about encoder

ok so I got everything working. the only thing is I can only do the first function or if I move the second function to the top I can do that one. how can I run both functions? thanks

[code]int main()
{
// Initialize the encoders and specify the four input pins.
encoders_init(IO_C2, IO_C3, IO_C4, IO_C5);
{
while(1)
{
// Read the counts for motor 2 and print to LCD.
lcd_goto_xy(0,0);
print_long(encoders_get_counts_m2());
print(" ");
delay_ms(20);
clear();
}

// wait for either the top or bottom buttons to be pressed
// store the value of the pressed button in the variable 'button'
unsigned char button = wait_for_button_press(TOP_BUTTON);
clear();
if (button == TOP_BUTTON)     // display the button that was pressed
print("Override");
else
print("Override");
wait_for_button_release(button);  // wait for that button to be released
red_led(1);
green_led(1);
delay_ms(1000);
clear();

}

}[/code]

What functions?

Sorry I’m new to this language of programming. I need the encoder to count which it does. I also have a button programmed for override. When I push it it doesn’t show anything. If I flip the program around I get override but no count. Are there like sub routines you can call up? Or is my while statements wrong. By the way I have a sv328. Thanks

It looks like the problem is that you are never checking the buttons while in the while loop. Once an infinite loop (such as while(1)) starts, the only code that runs is what’s in the loop or started by interrupts. The code after the while loop is never run. So you would need to check the button inside the loop with a non-blocking function (Google ‘blocking vs non-blocking functions’ if you don’t know what that means). I’m afraid I can’t help you much further as I’m not up on Pololu AVR Library code though, and I don’t know what function(s) you should use.

You said that you are new to C? Do you have experience with another language? If so, the syntax and constructs can often be explained in terms of another language, making it easier to understand.

Thanks i will give it a try.

SV-328p Ok so I read up on what you told me about but having some issues building the code. I get errors on undefined references. Im trying to get it so it always counts the encoder. then when I push the button it goes into override. when you hit reset it will go back to the encoder counting. Im not sure if im close. thanks.[code]#include <pololu/orangutan.h>
#include <pololu/orangutan>
#include <pololu/Pololu3pi.h>
#include <pololu/PololuQTRSensors.h>
#include <pololu/PololuWheelEncoders.h>

void BtnProcess();
void BtnInit();
void encoder_init();
void EncoderProcess();

int BtnPressEvent;
int BtnReleaseEvent;
int encoderEvent;

void main() {

// Override.		
BtnInit();

while(1)	{
	BtnProcess();
	if (BtnPressEvent){
		//do it now
		unsigned char button = wait_for_button_press(TOP_BUTTON);
		clear();
		if (button == TOP_BUTTON)     // display the button that was pressed
		print("Override");
		else
		print("Override");
		wait_for_button_release(button);  // wait for that button to be released
		red_led(1);
		green_led(1);
		delay_ms(100);
	}
}

}

void BtnInit()	{
	//Initialize Counting of the encoders and print to lcd
	encoder_init();
	
	while (1)	{
		EncoderProcess();
		if (encoderEvent){
			// do it now
				encoders_init(IO_C2, IO_C3, IO_C4, IO_C5);
				lcd_goto_xy(0,0);
				print_long(encoders_get_counts_m2());
				print(" ");
				delay_ms(20);
			
		}
	}
	
}[/code]

What is “override”?

Read your code as if the processor is actually running it. You’ll find that you can’t get out of your loops.

You either need to have one infinite loop, or several non-infinite loops. You need to watch for a button press in the same loop that checks the encoders. You also need to be able to leave the override loop via button press.