#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#define BUTTON_PIN 14
#define LED_PIN 26 // Example LED pin; adjust to your setup
#define DEBOUNCE_DELAY_US 10000 // Adjust as needed
bool button_state = true; // Initialize to not pressed
void button_callback(uint gpio, uint32_t events) {
// Read the current state of the button
bool current_state = gpio_get(BUTTON_PIN);
if (current_state != button_state) {
// Debounce the button press
sleep_us(DEBOUNCE_DELAY_US);
current_state = gpio_get(BUTTON_PIN);
if (current_state != button_state) {
// Button press is stable
button_state = current_state;
if (!button_state) {
// Button is pressed
// Turn on the LED
gpio_put(LED_PIN, 1);
} else {
// Button is released
// Turn off the LED
gpio_put(LED_PIN, 0);
}
}
}
}
void setup() {
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_set_pulls(BUTTON_PIN, true, false);
// Set up an LED pin
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
// Set up a callback for button interrupts
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_BOTH, true, &button_callback);
while (true) {
tight_loop_contents();
}
}
void loop() {}