JChristensen / JC_Button

Arduino library to debounce button switches, detect presses, releases, and long presses
GNU General Public License v3.0
425 stars 102 forks source link

Would this be quick enough for the debounce on a reed switch flow meter? #24

Closed 3h50 closed 5 years ago

3h50 commented 5 years ago

What’s the max frequency this can handle?

JChristensen commented 5 years ago

A very simple sketch that doesn't do much else can poll a switch in excess of 70kHz when running on a 16MHz AVR microcontroller like an Arduino Uno.

But at some point I assume there is other work to do, something needs to be done with the data, etc. So a better question may be how long does that take.

Depending on the expected frequency generated by the reed switch flow meter, and how long other processing takes, etc., I might opt for hardware debouncing and use the reed switch to drive an interrupt, especially if I were concerned about missing a switch closure.

Hope this helps.

JChristensen commented 5 years ago

Maybe a better way to think about it is that by definition the maximum switch frequency is the reciprocal of the debounce time. So for example, if I have a 20mS debounce time, then the switch can't operate faster than 50Hz.

Then the question becomes, can whatever other processing that is needed occur in less than the debounce time. I'd probably want a significant safety factor there, so if I have a 20mS debounce time, then I'd want other processing to take no longer than maybe 10mS to be safe. If that's not possible, then I'd go with the hardware debounce/interrupt approach.

3h50 commented 5 years ago

I guess I was hoping this was on an interrupt pin but at the same time it might never get to send data out if that was the case.

JChristensen commented 5 years ago

The library will work on any pin, it just polls the switch. I pretty much only intended it for HMI or similar situations. Specialized sensors probably deserve their own coding in many cases.