#define LED 13
#define BTN 4
byte lastButtonState = LOW;
byte ledState = LOW;
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BTN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED, ledState);
}
}
}
}