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

// Define GPIO pin for the LED and the switch
#define LED_PIN 16  // On-board LED on Raspberry Pi Pico
#define SWITCH_PIN 15  // GPIO pin for the switch

// Function to blink LED with specified parameters
void blinkLED(int times, float interval) {
    for (int i = 0; i < times; i++) {
        gpio_put(LED_PIN, 1);  // Turn LED on
        sleep_ms((int)(interval * 1000));  // Wait interval in milliseconds
        gpio_put(LED_PIN, 0);  // Turn LED off
        sleep_ms((int)(interval * 1000));  // Wait interval in milliseconds
    }
}

// Function for Code A
void runCodeA() {
    printf("Running Code A...\n");
    float interval;

    // 1st minute: Blink 10 times
    interval = 60.0 / (2 * 10);  // Interval for 10 blinks in 1 minute
    blinkLED(10, interval);

    // 2nd minute: Blink 20 times
    interval = 60.0 / (2 * 20);  // Interval for 20 blinks in 1 minute
    blinkLED(20, interval);

    // 3rd minute: Blink 30 times
    interval = 60.0 / (2 * 30);  // Interval for 30 blinks in 1 minute
    blinkLED(30, interval);

    printf("Code A completed.\n");
}

// Function for Code B
void runCodeB() {
    printf("Running Code B...\n");
    float interval;

    // 1st minute: Blink 30 times
    interval = 60.0 / (2 * 30);  // Interval for 30 blinks in 1 minute
    printf("Blinking 30 times with interval: %.2f seconds\n", interval);
    blinkLED(30, interval);

    // 2nd minute: Blink 20 times
    interval = 60.0 / (2 * 20);  // Interval for 20 blinks in 1 minute
    printf("Blinking 20 times with interval: %.2f seconds\n", interval);
    blinkLED(20, interval);

    // 3rd minute: Blink 10 times
    interval = 60.0 / (2 * 10);  // Interval for 10 blinks in 1 minute
    printf("Blinking 10 times with interval: %.2f seconds\n", interval);
    blinkLED(10, interval);

    printf("Code B completed.\n");
}

int main() {
    stdio_init_all();  // Initialize standard I/O

    // Set up the LED
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);  // Set as output

    // Set up the switch
    gpio_init(SWITCH_PIN);
    gpio_set_dir(SWITCH_PIN, GPIO_IN);  // Set as input
    gpio_pull_up(SWITCH_PIN);  // Use pull-up resistor (button pressed = LOW)

    int count = 1;  // Initialize count to 1 (odd, so Code A runs first)
    int prev_state = 0; // Variable to store the previous state of the switch

    while (1) {
        int current_state = gpio_get(SWITCH_PIN);
        if (current_state != prev_state) { // Check if switch state has changed
            prev_state = current_state; // Update previous state
            
            if (current_state == 0) { // Switch pressed
                count = 1; // Reset count to 1 when switch pressed
                printf("Switch pressed. Resetting count to 1.\n");
            } else { // Switch released
                count = 0; // Reset count to 0 when switch released
                printf("Switch released. Resetting count to 0.\n");
            }
        }

        printf("Current count: %d\n", count);

        if (count % 2 == 1) {  // Odd count: run Code A
            runCodeB();  // Execute Code A
        } else {  // Even count: run Code B
            runCodeA();  // Execute Code B
        }

        // Now, wait for the switch to be pressed to continue
        printf("Waiting for switch to toggle the count...\n");
        while (!gpio_get(SWITCH_PIN)) {  // Wait while switch is not pressed
            sleep_ms(100);  // Sleep a bit to avoid busy-waiting
        }

        // Increment the count when the switch is pressed
        count++;  // Increment count after the switch is pressed

        printf("Switch toggled. New count: %d\n", count);
        if (count % 2 == 1) {
            printf("Next, Code A will run.\n");
        } else {
            printf("Next, Code B will run.\n");
        }

        // Wait for the button to be released to avoid multiple toggles
        while (gpio_get(SWITCH_PIN)) {
            sleep_ms(100);  // Debounce
        }
    }

    return 0;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT