#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <stdbool.h>

int main(void) {
    uint8_t delay = 2;
    bool swSnel, swTraag, oldswSnel = false, oldswTraag = false;
    volatile uint16_t i;
    DDRB = 0xFF;
    PORTB = 0xFF;
    DDRA = 0x00;
    OCR0 = 36 * delay;
    TCCR0 = 0x0D; 
    TIMSK |= 1<<OCIE0;
    sei();
    while (1) {
        swTraag = ~PINA & 0x08; /* SW3 */
        if (swTraag != oldswTraag) {
            oldswTraag = swTraag;
            if (swTraag && delay < 5) OCR0 = ++delay * 36;
        }
        swSnel = ~PINA & 0x04; /* SW2 */
        if (swSnel != oldswSnel) {
            oldswSnel = swSnel;
            if (swSnel && delay > 1) OCR0 = --delay * 36;
        }
        for (i = 0; i < 3000; ++i); /* anti-dender vertraging */
    }
    return 0;
}

ISR(TIMER0_COMP_vect) {
    static uint8_t patroon[] = {
        0x00, 0x80, 0xC0, 0x40, 0x60, 0x20, 0x30, 0x10,
        0x18, 0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x00
    };
    static uint8_t j = 0;
    static bool heen = true;
    PORTB = ~patroon[j];
    heen = heen ? ++j != sizeof patroon - 1 : --j == 0;
}

