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

Debouncing interrupts #5

Open
colinodell opened this issue May 12, 2016 · 5 comments
Open

Debouncing interrupts #5

colinodell opened this issue May 12, 2016 · 5 comments

Comments

@colinodell
Copy link

Do you think there's a need for the InterruptWatcher to have debouncing logic built-in? For example, when I run the code in the README example and toggle the connection between the GPIO pin and ground, I get the following result:

Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 1
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 0
Pin 4 changed to: 1

Note how sometimes the interrupt callback is fired but the value doesn't appear to have changed. This happens because the value rapidly switches (or "bounces") due to noise or mechanical/physical imperfections with the button/connection.

A common approach to "debouncing" is timing the duration between the current interrupt and the previous one. If it's faster than, say, 100ms then the toggle probably wasn't intentional and should be ignored. Here's an example: https://raspberrypi.stackexchange.com/questions/8544/gpio-interrupt-debounce

As a developer I could implement this myself on top of your library, but it would be much easier if that functionality was baked in.

@AndrewCarterUK
Copy link
Contributor

How about as an optional parameter when registering the interrupt?

$interruptWatcher->register($pin, $callback);
// or...
$interruptWatcher->register($pin, $callback, $delay);

If the parameter isn't set then no bouncing protection, otherwise it represents a time in milliseconds that must occur between invoking callbacks?

@colinodell
Copy link
Author

Yeah that would be great!

@allebb
Copy link

allebb commented Aug 17, 2017

I'm experiencing the same issue as @colinodell at the moment.

Having baked in support to prevent bouncing would be fantastic... at the moment on my RPi3 it rarely registers the button "release".

Any idea when this may be released?

@AndrewCarterUK
Copy link
Contributor

Hi @allebb - I haven't got a setup for this at the moment and I'm quite busy with my paid work. I've not been doing PHP for a while and haven't got any of my electronics equipment set up in my new residence.

If anyone reading is capable with some available spare time I'd welcome a PR for this

@fcrossen
Copy link

fcrossen commented Aug 1, 2020

@allebb / @colinodell #25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants