You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Debounce tutorial, a push button press is toggling a LED, which can lead people to think that the example shows how to reliably detect button presses. There are some problems with this:
First of all, this code does not detect short presses. From the comments,
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state
The reading that is accepted as final is the current one, and it's taken at least 50ms from the first change (debounceDelay + time between first and last change). So in order for a press to register, the button should be held depressed for 50ms (or more!). I find that it's not hard to press a button for less than that. So the code doesn't detect button presses quite reliably.
I was told that the reason for this behavior is to ignore random voltage spikes that could change the signal on the pin for a very short time. I think that this is a problem that is quite not related to debouncing, and if it is, must be mentioned in the description.
Also, the LED will only switch after 50ms (or more!). It is probably fine with LEDs, but totally unacceptable if you use buttons to play midi notes, some of the games and probably in many other applications.
These problems can be somewhat mitigated by using a lower debounceDelay value, if you have good buttons. Or straight away solved by firing event first, and waiting for the button to debounce later, e.g.
check():
current_reading = read_pin()
if reading_is_settling():
if last_reading != current_reading:
start_settling()
else if current_reading != button_state:
dispatch_change(current_reading)
button_state = current_reading
start_settling()
reading_is_settling():
return now - last_reading_change_time > SETTLE_TIME
start_settling():
last_reading = current_reading
last_reading_change_time = now
If there's a need to detect voltage spikes, you can still do it by changing the logic of read_pin(), separating the concerns.
At the very least, the aforementioned problems and alternative solutions should be mentioned in the description.
Additionally, the example should probably use LED_BUILTIN instead of ledPin = 13 for clarity. And maybe not use confusing names such as lastDebounceTime.
The text was updated successfully, but these errors were encountered:
The reason for the debounce is because most switches, especially those used for hobby purchases (cheap), don't cleanly go from 0 to 5V (or whatever the output level is). Usually the output bounces around before hitting 5V and likewise before hitting 0V when released. The debounce logic is used to ignore these.
The code above looks correct, but the way I've done that I think is quicker (I'm not familiar with the Python implementation of Arduino, only C/C++ variant) is by using interrupts which will get you a little quicker depending on where you put this code at. The way I've done in the past is trigger on CHANGE and then in the change code, first check if the time limit between presses has been met. If not, return. If so, record the current reading and time change.
Don't know if this helped at all, but I wanted to make sure the debounce reasoning was clarified as I couldn't tell completely by your description. Good luck
In the Debounce tutorial, a push button press is toggling a LED, which can lead people to think that the example shows how to reliably detect button presses. There are some problems with this:
First of all, this code does not detect short presses. From the comments,
The reading that is accepted as final is the current one, and it's taken at least 50ms from the first change (
debounceDelay
+ time between first and last change). So in order for a press to register, the button should be held depressed for 50ms (or more!). I find that it's not hard to press a button for less than that. So the code doesn't detect button presses quite reliably.I was told that the reason for this behavior is to ignore random voltage spikes that could change the signal on the pin for a very short time. I think that this is a problem that is quite not related to debouncing, and if it is, must be mentioned in the description.
Also, the LED will only switch after 50ms (or more!). It is probably fine with LEDs, but totally unacceptable if you use buttons to play midi notes, some of the games and probably in many other applications.
These problems can be somewhat mitigated by using a lower
debounceDelay
value, if you have good buttons. Or straight away solved by firing event first, and waiting for the button to debounce later, e.g.If there's a need to detect voltage spikes, you can still do it by changing the logic of
read_pin()
, separating the concerns.At the very least, the aforementioned problems and alternative solutions should be mentioned in the description.
Additionally, the example should probably use
LED_BUILTIN
instead ofledPin = 13
for clarity. And maybe not use confusing names such aslastDebounceTime
.The text was updated successfully, but these errors were encountered: