Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Problems with the Debounce tutorial #12

Open
@oakkitten

Description

@oakkitten

In theDebounce 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 lowerdebounceDelay 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 ofread_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 useLED_BUILTIN instead ofledPin = 13 for clarity. And maybe not use confusing names such aslastDebounceTime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp