/*
Name: K M AKASH
Program: To reverse the action of previous program upon toggling the switch
*/


#include <stdio.h>
#include "pico/stdlib.h"    // Provides standard library functions
#include "pico/time.h"      // Provides libraries for working with time


#define TOGGLE_PIN 15
#define LED_PIN 26

// Function to blink LED for desired number of times
void blinkLED(int times) {
  
    for (int i = 0; i < times; i++) {

        // LED ON
        gpio_put(LED_PIN, 1); 

        // 1 Second Delay
        sleep_ms(500); 

        // LED OFF
        gpio_put(LED_PIN, 0); 

        // 1 Second Delay
        sleep_ms(500); 
    }
}

// Main Function
int main() {

    // Initializing all standard Input/Output facilities provided by PICO SDK
    stdio_init_all();

    // Initialize GPIO pin 26 for LED
    gpio_init(LED_PIN); 

    // Set GPIO pin 26 as OUTPUT
    gpio_set_dir(LED_PIN, GPIO_OUT); 


    // Initialize GPIO pin for toggle switch
    gpio_init(TOGGLE_PIN);
    // Set GPIO pin as INPUT
    gpio_set_dir(TOGGLE_PIN, GPIO_IN);
    // Enable pull-down resistor
    gpio_pull_down(TOGGLE_PIN);

    while (1) {
        
        uint64_t start_time = time_us_64(); 

        if (!gpio_get(TOGGLE_PIN)) {
            printf("Toggle button is pressed.\n");

            // Blink LED 30 times in the first minute
            blinkLED(30);

            // Calculate the time elapsed since the start of the minute
            uint64_t elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            uint64_t remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);


            // Store the start time for the second minute
            start_time = time_us_64(); 

            // Blink LED 20 times in the second minute
            blinkLED(20);

            // Calculate the time elapsed since the start of the minute
            elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);


            // Store the start time for the third minute
            start_time = time_us_64(); 

            // Blink LED 10 times in the third minute
            blinkLED(10);

            // Calculate the time elapsed since the start of the minute
            elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);

            while (!gpio_get(TOGGLE_PIN)); // Wait until the button is released

        }
        else{
            printf("Toggle button is not pressed.\n");

            uint64_t start_time = time_us_64(); 

            // Blink LED 10 times in the first minute
            blinkLED(10);

            // Calculate the time elapsed since the start of the minute
            uint64_t elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            uint64_t remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);



            // Store the start time for the second minute
            start_time = time_us_64(); 

            // Blink LED 20 times in the second minute
            blinkLED(20);

            // Calculate the time elapsed since the start of the minute
            elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);


            // Store the start time for the third minute
            start_time = time_us_64(); 

            // Blink LED 30 times in the third minute
            blinkLED(30);

            // Calculate the time elapsed since the start of the minute
            elapsed_time = time_us_64() - start_time;

            // Calculate the remaining time in the minute (60 seconds - elapsed time)
            remaining_time = 60000000 - elapsed_time;

            // Delay until the end of the minute
            sleep_us(remaining_time);

        }
        sleep_ms(100); // Adjust delay as needed
    }
   
    return 0;
}

BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT