2026-04-13 14:19:59 +02:00
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#define LED_PORT PORTB0
|
|
|
|
|
#define BTN_PORT PIND2
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
2026-04-14 09:56:31 +02:00
|
|
|
// set pin to output
|
|
|
|
|
// DDRB is the register that set the direction of the pins (IN/OUT)
|
|
|
|
|
// we want to say that the LED_PORT pin is an OUT pin, which isnt the default
|
|
|
|
|
DDRB |= (_BV(LED_PORT));
|
2026-04-13 14:19:59 +02:00
|
|
|
|
2026-04-14 09:56:31 +02:00
|
|
|
// lets keep track of the led state here
|
|
|
|
|
bool led_state = false;
|
|
|
|
|
// lets also keep track of the button state for the last iteration
|
|
|
|
|
bool prev = (PIND & _BV(BTN_PORT));
|
|
|
|
|
while (true) {
|
|
|
|
|
// we read it
|
|
|
|
|
bool cur = (PIND & _BV(BTN_PORT));
|
|
|
|
|
|
|
|
|
|
// if it wasnt pressed before, but pressed now -> it just turned on
|
|
|
|
|
if (prev && !cur) {
|
|
|
|
|
// toggle the state and update the led with the new state
|
|
|
|
|
led_state = !led_state;
|
|
|
|
|
if (led_state)
|
|
|
|
|
PORTB |= (_BV(LED_PORT));
|
|
|
|
|
else
|
|
|
|
|
PORTB &= ~(_BV(LED_PORT));
|
|
|
|
|
}
|
|
|
|
|
// keep track of the old state for next iteration
|
|
|
|
|
prev = cur;
|
|
|
|
|
// sleep
|
|
|
|
|
_delay_ms(20.f);
|
|
|
|
|
}
|
2026-04-13 14:19:59 +02:00
|
|
|
}
|