// Define debounce delay in milliseconds
#define DEBOUNCE_DELAY 80
// Pin assignments for LEDs and buttons
#define buttonPin1 0
// Variables to track button states
volatile bool button1Pressed = false;
bool previousButton1State = false;
unsigned long lastDebounceTime1 = 0;
// Interrupt service routine to handle button 1 press event with debounce
void IRAM_ATTR handleButton1Press() {
unsigned long currentTime = millis();
// Check debounce delay
if ((currentTime - lastDebounceTime1) > DEBOUNCE_DELAY) {
// Check if buttonPin1 is pressed
if (digitalRead(buttonPin1) == LOW) {
button1Pressed = true; // Button 1 is pressed
} else {
button1Pressed = false; // Button 1 is released
}
lastDebounceTime1 = currentTime; // Update last debounce time
}
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Configure button pins with internal pull-up resistors and attach interrupts
pinMode(buttonPin1, INPUT_PULLUP);
attachInterrupt(buttonPin1, handleButton1Press, CHANGE);
}
void loop() {
// Print button states to serial monitor
if (button1Pressed != previousButton1State) {
previousButton1State = button1Pressed;
if (button1Pressed) {
Serial.println("Button 1 pressed ");
}
}
delay(100); // Add a small delay to avoid flooding the serial monitor
}