const int buttonPin = 26; // Replace with your GPIO pin
const int ledPin = 33; // Replace with your LED GPIO pin
int buttonState = LOW; // Current button state (LOW when pressed, HIGH when not)
int lastButtonState = LOW; // Previous button state
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT_PULLDOWN); // Configure the button pin with pull-up resistor
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure the LED is initially turned off
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH); // Turn on the LED when the button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn off the LED when the button is released
}
}
}
lastButtonState = reading;
}