const int buttonPin = 2;
const int ledPin = 13;
bool ledState = false;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
static bool buttonPressed = false;
if (reading == LOW && !buttonPressed) {
buttonPressed = true;
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
}
else if (reading == HIGH) {
buttonPressed = false;
}
}
lastButtonState = reading;
}