int button = 2;
int LED = 4;
int buttonState;
unsigned long debounceDuration = 100; //Wait to validate second change in button state
unsigned int lastTimeButtonStateChanged; //Check last time state was changed
byte lastButtonState;
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//Debouncing
if (millis()-lastTimeButtonStateChanged > debounceDuration) {
buttonState = digitalRead(button);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
}
}
if (buttonState == 1) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
Serial.println(buttonState);
delay(500);
}