const int buttonPinA = 2;
const int ledPinA = 3;
int ledStateA = HIGH; //the current state of the output pin
int buttonStateA; //the current reading from the input pin
int lastButtonStateA = LOW; //the previous reading from the input pin
unsigned long lastDebounceTimeA = 0;
// one constant for all buttins is probably OK
// this absurdly large "debounce" constant dose not impact repsonse time!
// use 50 in real deployment, here just for illustrative purposes I use 500
const unsigned long theDebounceDelay = 500;
void setup() {
pinMode (buttonPinA, INPUT_PULLUP);
pinMode (ledPinA, OUTPUT);
digitalWrite(ledPinA, ledStateA);
}
void loop() {
int reading = digitalRead(buttonPinA);
if ((millis() - lastDebounceTimeA) > theDebounceDelay) {
if (reading != buttonStateA) {
buttonStateA = reading;
if (buttonStateA == LOW) { // I like the press, not the release to take action
ledStateA = !ledStateA;
}
lastDebounceTimeA = millis(); // don't act on the switch again until the debounce period has passed
}
}
digitalWrite(ledPinA, ledStateA);
lastButtonStateA = reading;
}