Ok I'm new to C and AVR's but have done a lot of studying and wanted to get ideas on this before proceeding. I want to have one switch to toggle through 4 LED's. Obviously this would be common but I couldn't find any code snippets to learn from so having a go at my own and asking for input on most efficient way to do this. Ultimately this will be a function within a larger program but I want to keep it simple for now.
I figure the program flow would go like this:
1. Initialize PORTA.
a) Switch is active low on PA0. Set as input, enable pullup.
b) LED's active high on PA1-PA4 Set as outputs, initialize to low state (off).
2. Initilize Timer. Set to compare mode, enable compare match interrupt and set OCR register value for ~1ms scanning.
3. Initialize a variable that will keep track of which LED is ON (call it led_on).
5. Is key press detected? (w/ appropriate debounce code).
a) if yes then figure out which LED is currently ON
and turn on the next one.
b) if no do nothing.
I can do everything including the debouncing and detecting of the key press, it's just the approach for toggling through the LED's I'm wondering about (most efficient, easiest etc..)
Would you use the switch function to detect which LED is currently ON and then turn ON the next one..?
case 0 turn off LED1, turn on LED2 and increment variable led_on
case 1 turn off LED2, turn on LED3 and increment variable led_on
case 2 turn off LED3, turn on LED4 and increment variable led_on
case 3 turn off LED1, turn on LED1 and reset variable led_on to 0
Would you use a series of IF statements?
if led_on=0 turn off LED1, turn on LED2 and increment led_on
else if led_on=1 turn off LED2, turn on LED3 and increment led_on
else if led_on=2 turn off LED3, turn on LED4 and increment led_on
else if led_on=2 turn off LED4, turn on LED1 and reset led_on to 0
Is there an easier way or a way that is most efficient. Do the two samples above even make sense? Are they really just doing the same thing?