Stap 5: Het heen en weer slingeren
Dus je hebt draaien met de klok mee. Om het even wat interessanter?Een beetje code-opruiming, en we kunnen het heen en weer uitgevoerd.
Ik zette de rechtsom volgorde in een array zodat u kunt stap door de fasen met een eenvoudige for-lus. Nu kunt u de lus omhoog of naar beneden om gaan met de klok mee of tegen de klok in.
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); } }}
Zie de markante video voor de rug-en-forthing.