const int buttonPin = 23; // 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() {
Serial.begin(115200);
pinMode(buttonPin, INPUT); // 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 == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the LED when the button is pressed
Serial.println("button is pressed");
} else {
digitalWrite(ledPin, LOW); // Turn off the LED when the button is released
Serial.println("button is not pressed");
}
}
}
lastButtonState = reading;
}