Step 5Swing it Back and Forth
A little code-cleanup, and we can run it back and forth.
I put the clockwise sequence into an array so that you can step through the phases with a simple for loop. Now you can run the loop up or down to go clockwise or counterclockwise.
int main(void){ const uint8_t delay = 50; const uint8_t clockwise[] = {BLUE, BLACK, RED, YELLOW}; uint8_t i; DDRB = 0xff; /* Enable output on all of the B pins */ PORTB = 0x00; /* Set them all to 0v */ while(1){ /* main loop here */ for ( i=0; i<=3; i++ ){ /* step through the colors clockwise */ PORTB = clockwise[i]; _delay_ms(delay); } for ( i=3; i>=0; i-- ){ /* step through the colors ccw */ PORTB = clockwise[i]; _delay_ms(delay); } }}
See the racy video for the back-and-forthing.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|











































