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

int main(void) {
    DDRB = 10;
    DDRD = 255;

    // Configure Timer1 for red LED (pin 9)
    TCCR1A |= (1 << WGM11) | (1 << COM1A1);
    TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11);
    ICR1 = 39999; // PWM period 20ms
    int offset_r = 800; // 2ms duty cycle offset

    // Configure Timer0 for green LED (pin 6)
    TCCR0A |= (1 << WGM01) | (1 << WGM00) | (1 << COM0A1);
    TCCR0B |= (1 << CS01);
    OCR0A = 0; // Initial duty cycle 0
    int offset_g = 64; // Duty cycle step size

    // Configure Timer2 for blue LED (pin 11)
    TCCR2A |= (1 << WGM21) | (1 << WGM20) | (1 << COM2A1);
    TCCR2B |= (1 << CS21);
    OCR2A = 0; // Initial duty cycle 0
    int offset_b = 64; // Duty cycle step size

    while(1) {
        // Update red LED duty cycle
        OCR1A = 3999 + offset_r; // 2ms
        _delay_ms(1000);
        OCR1A = 1999 - offset_r; // 1ms
        _delay_ms(1000);

        // Update green LED duty cycle
        OCR0A += offset_g;
        if (OCR0A > 255) {
            OCR0A = 0;
        }
        _delay_ms(100);

        // Update blue LED duty cycle
        OCR2A += offset_b;
        if (OCR2A > 255) {
            OCR2A = 0;
        }
        _delay_ms(100);
    }

    return 0;
}