#include "pico/stdlib.h"
#define LED_PIN 21
#define BUTTON_PIN 20
// Global mode: false = normal, true = reversed
bool reverse_mode = false;
// Please keep the button pressed for 1-2 seconds
// Function to detect button press and toggle state
void check_button_toggle() {
static bool last_button_state = 1;
bool current_state = gpio_get(BUTTON_PIN);
if (last_button_state && !current_state) {
reverse_mode = !reverse_mode;
sleep_ms(200); // Debounce delay
}
last_button_state = current_state;
}
// Blinks the LED with button check between each blink
void blink_with_button_check(int times, int delay_ms) {
for (int i = 0; i < times; i++) {
gpio_put(LED_PIN, 1);
sleep_ms(delay_ms / 2);
check_button_toggle(); // Check button mid-blink
gpio_put(LED_PIN, 0);
sleep_ms(delay_ms / 2);
check_button_toggle(); // Check button after blink
if (reverse_mode != (times == 10)) break; // React immediately if mode changed
}
}
int main() {
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
while (1) {
if (!reverse_mode) {
blink_with_button_check(10, 3000);
blink_with_button_check(20, 1500);
blink_with_button_check(30, 1000);
} else {
blink_with_button_check(30, 1000);
blink_with_button_check(20, 1500);
blink_with_button_check(10, 3000);
}
}
}