ElectricRCAircraftGuy / eRCaGuy_TimerCounter

An Arduino micros()-like function (encapsulated in a library) with 0.5us precision (since the built-in Arduino micros() function has only 4us precision)
http://www.electricrcaircraftguy.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html
31 stars 13 forks source link

Can I use this library to take timestamps to debounce button presses? #11

Open PeteHaughie opened 1 year ago

PeteHaughie commented 1 year ago

I'm building an electronic instrument with an Uno I had lying about. To create tones I'm using the Mozzi library which I think hijacks the internal timer for PWM because when I poll millis I always get a return of 0. I saw your post in an old thread from about a decade ago which mentioned a similar issue and thought I'd try it out. My particular use case is trying to software debounce a button press but I'm not sure how I would go about it using your library. Is there a way of utilising the lib in this way?

ElectricRCAircraftGuy commented 1 year ago

My particular use case is trying to software debounce a button press but I'm not sure how I would go about it using your library. Is there a way of utilising the lib in this way?

Absolutely. You simply need to use the timestamps to measure time intervals between button bounces (changes of state). Once the button state has not changed in a long time--ex: 50ms, you can assume it is done bouncing, and the state at that instant is the actual state.

Here's a debounce library I wrote a while back (be gentle: it was before I knew so much about programming and software design): https://github.com/ElectricRCAircraftGuy/eRCaGuy_EventReader

The interface may not be ideal, but the debounce algorithm is solid.

Simply replace the usage of millis() in this file here with a timestamp call from the timerCounter library: https://github.com/ElectricRCAircraftGuy/eRCaGuy_EventReader/blob/master/eRCaGuy_EventReader.cpp