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

Maybe not the best for button debouncing #15

Open
X-Ryl669 opened this issue Apr 11, 2022 · 0 comments
Open

Maybe not the best for button debouncing #15

X-Ryl669 opened this issue Apr 11, 2022 · 0 comments

Comments

@X-Ryl669
Copy link

Debouncing the Elliot's way has multiple disadvantages:

  1. Needs to be polled so it consumes a task and the polling frequency should be constant (it's not on a FreeRTOS task).
  2. Can't detect 000101011111 like bouncing pattern. The mask used to detect a button press edge is 00xxx111. So there is at least a frequency of the bouncing that'll fail detection. In your code, you're using longer patterns, but it's the same issue.
  3. Use of a Queue takes a lot of resources (with cross CPU interrupts) on ESP32. It's a huge amplification of the debouncing cost.
  4. Button pressed is only detected after multiple polling. So you have a high latency.

I think it'd be better if you used relaxing timers or counters instead:

Relaxing timer

  1. Set an interrupt on any edge of the button
  2. In the interrupt handler, you're checking if you are in the relax period or not
  3. If you are in the relax period, return and ignore the interrupt
  4. Else, start the relax period, and call the user's GPIO handler directly (or use a Queue if you need cross task here)
  5. Optional: The GPIO handler can store/maintain the state of the button

This typically react as fast as possible to the first button press (no latency) and is not sensitive to bounce frequency or task scheduling

Counters

  1. Set an interrupt on any edge of the button
  2. In the interrupt handler, you're checking if you are pressed or released. If pressed, increase counter. If released, decrease it.
  3. If the counter is above a given threshold, call the GPIO pressed counter.
  4. If the counter is below a given (negative) threshold, call the GPIO released counter.

This adds a bit of latency to the processing, but is a bit safer to EMI discharge in the GPIO input (where you get a 000010000 like event). You can combine the with the other method for robustness.

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

1 participant