#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint PUSH_BUTTON_01 = 2;
int led_state = 0;
volatile bool button_pressed = false;
void button_isr() {
button_pressed = true;
}
void debounce_and_process() {
static uint32_t last_interrupt_time = 0;
uint32_t current_time = to_ms_since_boot(get_absolute_time());
if (current_time - last_interrupt_time > 50) { // 50 ms
if (gpio_get(PUSH_BUTTON_01) == 0) {
if (led_state == 0) {
led_state = 1;
} else {
led_state = 0;
}
printf("Button press detected\n");
}
}
last_interrupt_time = current_time;
button_pressed = false;
}
int main() {
stdio_init_all();
printf("ISR test\n");
gpio_init(PUSH_BUTTON_01);
gpio_set_dir(PUSH_BUTTON_01, GPIO_IN);
gpio_pull_up(PUSH_BUTTON_01);
gpio_set_irq_enabled_with_callback(PUSH_BUTTON_01, GPIO_IRQ_EDGE_FALL, true, &button_isr);
while (1) {
if (button_pressed) {
debounce_and_process();
}
sleep_ms(10); // Short delay to prevent busy waiting
}
}