const int ledPin = 9;
const int buttonPin = 7;
byte lastButtonState = LOW;
byte ledState = LOW;
unsigned long debounceDuration = 53;
unsigned long lastTimeButtonStateChanged = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Initialize the pin as an output
pinMode(buttonPin, INPUT);
}
void loop() {
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW:HIGH;
digitalWrite(ledPin, ledState);
}
}
}
}