#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"

#define BUTTON_PIN 14
#define LED_PIN 0

void blinkLED(int times) {
    for (int i = 0; i < times; i++) {
        gpio_put(LED_PIN, 1); // Turn the LED on
        sleep_ms(300);        // Wait for 300 milliseconds
        gpio_put(LED_PIN, 0); // Turn the LED off
        sleep_ms(300);        // Wait for 300 milliseconds
    }
}

int main() {
    stdio_init_all();

    gpio_init(BUTTON_PIN);
    gpio_set_dir(BUTTON_PIN, GPIO_IN);
    gpio_pull_down(BUTTON_PIN); // Pull down button pin
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT); // Set LED pin as output

    bool reverse = false; // Toggle variable

    printf("Program started\n");

    while (true) {
        if (!gpio_get(BUTTON_PIN)) { // Check if button is not pressed
            printf("Button not pressed. Blinking LED...\n");
            if (!reverse) {
                // If not in reverse mode, blink LED according to the statement
                blinkLED(10); // Blink 10 times in the first minute
                printf("Blink 10 times in the first minute\n");
                sleep_ms(300);
                blinkLED(20); // Blink 20 times in the second minute
                printf("Blink 20 times in the second minute\n");
                sleep_ms(300);
                blinkLED(30); // Blink 30 times in the third minute
                printf("Blink 30 times in the third minute\n");
            } else {
                // If in reverse mode, reverse the blinking pattern
                blinkLED(30); // Blink 30 times in the first minute
                printf("Blink 30 times in the first minute\n");
                sleep_ms(300);
                blinkLED(20); // Blink 20 times in the second minute
                printf("Blink 20 times in the second minute\n");
                sleep_ms(300);
                blinkLED(10); // Blink 10 times in the third minute
                printf("Blink 10 times in the third minute\n");
            }
        } else {
            printf("Button pressed. Blinking LED in reverse...\n");
            reverse = !reverse; // Toggle the mode
        }
        sleep_ms(250);
    }
}