#include <avr/io.h> // For DDRx, DDxn, PORTx, PORTxn, PINx, PINxn.
#include <avr/sfr_defs.h> // For _BV(), loop_until_bit_is_clear().
#include <util/delay.h> // For _delay_ms().
int main(void)
{
// Configure pin direction.
DDRD |= _BV(DDD7); // PD7 -> output (Arduino pin 7 - yellow)
DDRD |= _BV(DDD6); // PD6 -> output (Arduino pin 6 - orange)
DDRD |= _BV(DDD5); // PD5 -> output (Arduino pin 5 - blue )
DDRD |= _BV(DDD4); // PD4 -> output (Arduino pin 4 - pink )
DDRD &= ~(_BV(DDD2)); // PD2 -> input (Arduino pin 2 - button)
// Configure input pin pull-ups.
PORTD |= _BV(PORTD2); // PD2 -> enabled
// Button is GND when pressed, else floating.
// Drive output pins low.
PORTD &= ~(_BV(PORTD7)); // PD7 (yellow)
PORTD &= ~(_BV(PORTD6)); // PD6 (orange)
PORTD &= ~(_BV(PORTD5)); // PD5 (blue)
PORTD &= ~(_BV(PORTD4)); // PD4 (pink)
// Drive PD7 high (yellow).
PORTD |= _BV(PORTD7);
while (true)
{
// Yellow
PORTD &= ~(_BV(PORTD6)); // Drive PD6 low (orange)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Pink, Yellow
PORTD |= _BV(PORTD4); // Drive PD4 high (pink)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Pink
PORTD &= ~(_BV(PORTD7)); // Drive PD7 low (yellow)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Blue, Pink
PORTD |= _BV(PORTD5); // Drive PD5 high (blue)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Blue
PORTD &= ~(_BV(PORTD4)); // Drive PD4 low (pink)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Orange, Blue
PORTD |= _BV(PORTD6); // Drive PD6 high (orange)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Orange
PORTD &= ~(_BV(PORTD5)); // Drive PD5 low (blue)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
// Yellow, Orange
PORTD |= _BV(PORTD7); // Drive PD7 high (yellow)
_delay_ms(200); // Prevent switch bounce.
loop_until_bit_is_clear(PIND, PIND2); // Wait for button down.
}
}