#define BUTTON_PIN 14 // Button connected to GPIO 14
#define LED_PIN 15 // LED connected to GPIO 15
volatile bool buttonReleased = false; // State variable for button press detection
volatile unsigned long lastDebounceTime = 0; // To handle debounce
const unsigned long debounceDelay = 50; // Debounce delay (in milliseconds)
bool LEDState = LOW; // Stores LED state (ON/OFF)
// Interrupt Service Routine (ISR)
void IRAM_ATTR buttonReleasedInterrupt() {
unsigned long currentTime = millis(); // Get current time
if (currentTime - lastDebounceTime > debounceDelay) {
lastDebounceTime = currentTime; // Update last debounce time
buttonReleased = true; // Set flag to handle in the loop
}
}
void setup() {
Serial.begin(115200); // Initialize Serial Monitor
pinMode(BUTTON_PIN, INPUT_PULLDOWN); // Set button as input with pull-down resistor
pinMode(LED_PIN, OUTPUT); // Set LED as output
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonReleasedInterrupt, FALLING);
// Attach interrupt to BUTTON_PIN, trigger when button is released (FALLING edge)
}
void loop() {
if (buttonReleased) {
buttonReleased = false; // Reset flag
LEDState = !LEDState; // Toggle LED state
digitalWrite(LED_PIN, LEDState); // Update LED
Serial.print("LED State: ");
Serial.println(LEDState);
}
}