Hlavný kód
main.c

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include "uart_P.h"

// tlačidlá
#define BTN_D12 PB4
#define BTN_D8  PB0
#define BTN_D5  PD5
#define BTN_D2  PD2

// LED
#define LED_YELLOW PB5   // D13
#define LED_RED    PB3   // D11

int read_button(void)
{
    if (!(PINB & (1 << BTN_D12))) return 12;
    if (!(PIND & (1 << BTN_D5)))  return 5;
    if (!(PIND & (1 << BTN_D2)))  return 2;
    if (!(PINB & (1 << BTN_D8)))  return 8;

    return 0;
}

void wait_release(void)
{
    while (read_button() != 0);
    _delay_ms(150);
}

int main(void)
{
    uart_init();

    int correct[4] = {12, 5, 2, 8};  //správny kód
    int entered[4];
    int index = 0;
    int button;
    int i;
    int ok;

    // LED výstupy
    DDRB |= (1 << LED_YELLOW);
    DDRB |= (1 << LED_RED);

    // tlačidlá vstupy
    DDRB &= ~(1 << BTN_D12);
    DDRB &= ~(1 << BTN_D8);
    DDRD &= ~(1 << BTN_D5);
    DDRD &= ~(1 << BTN_D2);

    // pull-up rezistory
    PORTB |= (1 << BTN_D12);
    PORTB |= (1 << BTN_D8);
    PORTD |= (1 << BTN_D5);
    PORTD |= (1 << BTN_D2);

    // LED vypnúť
    PORTB &= ~(1 << LED_YELLOW);
    PORTB &= ~(1 << LED_RED);

    while (1)
    {
        button = read_button();

        if (button != 0)
        {
            _delay_ms(80); // odstránenie zákmitov

            if (read_button() == button)
            {
                entered[index] = button;
                index++;

                wait_release();

                if (index == 4) //vyhodnotenie až po 4 stlačeniach
                {
                    ok = 1;

                    for (i = 0; i < 4; i++)
                    {
                        if (entered[i] != correct[i])
                        {
                            ok = 0;
                        }
                    }

                    if (ok == 1)
                    {
                        uart_puts("spravny kod\r\n");

                        PORTB |= (1 << LED_YELLOW);
                        _delay_ms(4000);
                        PORTB &= ~(1 << LED_YELLOW);
                    }
                    else
                    {
                        uart_puts("nespravny kod\r\n");

                        PORTB |= (1 << LED_RED);
                        _delay_ms(700);
                        PORTB &= ~(1 << LED_RED);
                    }

                    index = 0;
                }
            }
        }
    }
}

