#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/irq.h"
#define LED_PIN 15
#define BUTTON_PIN 16
#define DEBOUNCE_DELAY_MS 20
volatile bool button_pressed = false;
void button_irq_handler(uint gpio, uint32_t events) {
sleep_ms(DEBOUNCE_DELAY_MS);
if (gpio_get(BUTTON_PIN) == 0) {
button_pressed = true;
}
}
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);
// Set up IRQ handler for the button press
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &button_irq_handler);
// Enable IRQs
irq_set_enabled(BUTTON_PIN, true);
while (true) {
// Toggle the LED when the button is pressed
if (button_pressed) {
gpio_put(LED_PIN, !gpio_get(LED_PIN));
button_pressed = false; // Reset
}
sleep_ms(100); // delay time
}
return 0;
}