constint BUTTON_PIN = PB6; // the number of the pushbutton pin constint DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers
int lastSteadyState = LOW; // the previous steady state from the input pin int lastFlickerableState = LOW; // the previous flickerable state from the input pin int currentState; // the current reading from the input pin
unsignedlong lastDebounceTime = 0; // the last time the output pin was toggled
voidloop(){ // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN);
if (currentState != lastFlickerableState) { // reset the debouncing timer lastDebounceTime = millis(); // save the the last flickerable state lastFlickerableState = currentState; }
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) { if (lastSteadyState == HIGH && currentState == LOW) Serial.println("The button is pressed"); elseif (lastSteadyState == LOW && currentState == HIGH) Serial.println("The button is released");