question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Button Debounce doesn't work correctly

See original GitHub issue

https://github.com/dotnet/iot/pull/1720

@pgrawehr

There’s an issue with this implementation, when the button is held down for longer then the debounce timeout. When it is released now, there will be a “pressed” event due to the bounces happening during release, and the desired “released” event is fired, due to the debouncing getting started by “pressed”.

I solved this by using a timer for debounce that checks the current button state when finished. Something like below, same for HandleButtonReleased()

protected void HandleButtonPressed()
{
    _stateForAfterDebounce = true;  // keep track of the button state, even with bounces, so we can get the current state at the end of the debounce timeout

    if (IsPressed)  // already pressed
        return;
    if (_debounceTimer!=null)   // currently debouncing
        return;

    IsPressed = true;

    if (_debounceTime.TotalMilliseconds > 0)
    {
        _debounceTimer = new Timer((o) =>
        {
            _debounceTimer?.Dispose();
            _debounceTimer = null;

            if (_stateForAfterDebounce == false)  // Button might have been been released while we were in the debounce. Check current state and handle
                HandleButtonReleased();
        }, null, _debounceTime, Timeout.InfiniteTimeSpan);
    }

    ButtonDown?.Invoke(this, new EventArgs());

    if (IsHoldingEnabled)
    {
        _holdingTimer = new Timer(StartHoldingHandler, null, (int)_holdingMs, Timeout.Infinite);
    }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
krwqcommented, Jun 21, 2022

@W1zzardTPU do you feel like sending a PR with a fix? I’m sure there will be more people hitting this. Perhaps we should be doing debounce on both pressing and releasing button (I didn’t look at the implementation so take what I’m saying with grain of salt)

0reactions
W1zzardTPUcommented, Apr 21, 2023

Still no time, for a quick test, try running a loop of random presses/releases to simulate bounce.

both before and after the sleep

Read more comments on GitHub >

github_iconTop Results From Across the Web

DEBOUNCE JS NOT WORKING with event onclick was ...
1 Answer 1 ... debounce is designed to create a debounced function. Every time you click you create a debounced function which you...
Read more >
Button debouncing not working [closed]
It is perfectly possible that when you close the switch, at the time the ISR handler gets control, the switch contact is bouncing....
Read more >
The simplest button debounce solution
Button debounce does not to be this complicate, in this article I will show you probably the simplest button debounce solution.
Read more >
Debounce button and delays - Programming Questions
I try to find out why my debounced button gets hardly recognized after pushing. ... No I do not use delay within the...
Read more >
How to debounce and throttle in React without losing your ...
Deep dive into debounce and throttle in React. What is debounce and throttle, how to use them in React properly, how to avoid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found