1

I don't understand the following Debounce code. (The complete code is at the bottom.) From what I understand, when the pin reads something, we wait at least 50 milliseconds before performing anything. But doesn't the reading variable get reset every loop iteration, so reading equals zero in the inner if-condition in every iteration.

What I think is the sequence of events: loop(), loop(), loop(), "loop(), button press, reading=1, set lastDebounceTime, first if condition fails because we have to wait, set lastButtonState=1", loop(), loop(),loop(),"loop() enough time has passed, but since no button press at in this iteration and the last few, reading and lastButtonState equal zero", loop(), ... .

However, this code works.

Why do we save reading in a local variable? Won't the code virtually always be this, then:

if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if 0 != buttonState) {
      buttonState = 0;

...

Entire code:

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

(Debounce code taken from: https://www.arduino.cc/en/tutorial/debounce.)

9
  • What's unclear with the comments? BTW: The last code line is part of the debouncing algorithm. Sep 14, 2020 at 22:26
  • Ah, sorry @jsotola. I seem to have copy/paste-d incorrectly Sep 14, 2020 at 22:31
  • @DataFiddler, I edited the question. I don't understand part of the algorithm. Sorry about that. Sep 14, 2020 at 22:32
  • think about this .... imagine that you have a stopwatch ... in front of you is an indicator light and a pushbutton ... the light turns on, and off, at random intervals ... your job is to press a button if the state of the lamp does not change for 10 seconds ... how would you accomplish your job? ... the algorithm does the same thing
    – jsotola
    Sep 14, 2020 at 22:36
  • I edited the question again. Why is reading local? Sep 14, 2020 at 22:42

1 Answer 1

2

loop() does not need any time and repeats permanently.

lastButtonState is reading from previous loop run.

As long as the button bounces, lastDebounceTime is updated.

Only when the button has settled (either pressed or released), the condition

  ((millis() - lastDebounceTime) > debounceDelay)

becomes true and the code is executed -> if buttonState (the previous debounced state) and reading show a change to HIGH, the led state is toggled.

Not sure if this explanation makes anything clearer, as it is clear already as it stands, IMO.

10
  • Why isn't it that we check instead lastButtonState shows a change to high? Reading gets reset every loop() and will virtually always be zero (in my limited understanding). Sep 14, 2020 at 23:01
  • Why would it always be 0? It will be whatever the pin reads. Every time loop repeats it reads the pin again and you get a new reading. We could check lastButtonState or reading, they will be the same at that point in the code. If they aren't the same then lastDebounceTime gets set to the current time and we don't enter that second if statement.
    – Delta_G
    Sep 14, 2020 at 23:03
  • 1
    @jsotola, I think I get it now. I (think) couldn't understand it due to my ignorance of how "long" a button press is and how many times loop() would run during a single press. Just to make sure: During the 50 ms (debounceDelay), the button would be remain in the pressed state by the user (even though it was just a click), and during that time, loop() would run multiple times (so, it would "remember" what reading was). I was thinking that the 50 ms would pass in one call of loop(). I could still be wrong in my understanding, but thank you kindly for your help, and sorry for the annoyance. Sep 15, 2020 at 2:55
  • 1
    @thegoodhunter-9115 thank you for the explanation of your misunderstanding ... i learned something from you today ... i now know that the loop() execution time can be the source of confusion
    – jsotola
    Sep 15, 2020 at 3:36
  • 1
    @thegoodhunter-9115 you can do a simple test ... write a sketch that, in setup(), prints the value of millis() and sets a counter to zero .... in loop() increments the counter ... when the counter reaches 1000, prints millis() again .... that will give you an idea of approximately how long 1000 iterations of loop() take
    – jsotola
    Sep 15, 2020 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.