#include #include #include #include #define D1 PB0 #define D2 PB1 #define D3 PB2 #define D4 PB4 #define SW1 PD2 #define SW2 PD4 int main(void) { // ouput pins for leds DDRB |= _BV(D4); DDRB |= _BV(D3); DDRB |= _BV(D2); DDRB |= _BV(D1); // set input (not neccessary since input by default) DDRD &= ~(_BV(SW1)); DDRD &= ~(_BV(SW2)); PORTB |= _BV(D4); uint8_t prev = 0; uint8_t value = 0; while (true) { // lets read both button in a single byte, and keep track of that too uint8_t cur = (PIND & (_BV(SW1) | _BV(SW2))); // check for button SW1 if (prev & (_BV(SW1)) && !(cur & (_BV(SW1)))) { value++; } // check for button SW2 if (prev & (_BV(SW2)) && !(cur & (_BV(SW2)))) { value--; } prev = cur; // for each bit of the counter -> lets turn on or of the led if (value & 0b0001) PORTB |= (_BV(D1)); else PORTB &= ~(_BV(D1)); if (value & 0b0010) PORTB |= (_BV(D2)); else PORTB &= ~(_BV(D2)); if (value & 0b0100) PORTB |= (_BV(D3)); else PORTB &= ~(_BV(D3)); if (value & 0b1000) PORTB |= (_BV(D4)); else PORTB &= ~(_BV(D4)); _delay_ms(20); } }