Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with the Debounce tutorial #12

Open
oakkitten opened this issue Nov 26, 2018 · 1 comment
Open

Problems with the Debounce tutorial #12

oakkitten opened this issue Nov 26, 2018 · 1 comment

Comments

@oakkitten
Copy link

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.

@pmmcmullen94
Copy link

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

@per1234 per1234 transferred this issue from arduino/Arduino Sep 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants