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.)
1 Answer 1
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.
-
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– Delta_G2020年09月14日 23:03:49 +00:00Commented Sep 14, 2020 at 23:03
-
If loop takes a microsecond and bouncing takes two milliseconds,
reading
will be evaluated 2000 times in those 2 ms, and change a couple of times during this period. Only if is stable for 50 ms (you got nearly an infinite number of the same reading values) you accept that as a new debounced button stateDataFiddler– DataFiddler2020年09月14日 23:10:47 +00:00Commented Sep 14, 2020 at 23:10 -
@thegoodhunter-9115 seriously think about what i said about the light and the stopwatch .... what would you do with the stopwatch if the light turned on or turned off?jsotola– jsotola2020年09月15日 00:36:43 +00:00Commented Sep 15, 2020 at 0:36
-
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 confusionjsotola– jsotola2020年09月15日 03:36:06 +00:00Commented 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() takejsotola– jsotola2020年09月15日 03:41:16 +00:00Commented Sep 15, 2020 at 3:41
reading
needs to exist only for the duration of one iteration ofloop()
lastDebounceTime completes