-
Notifications
You must be signed in to change notification settings - Fork 3
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
Does your debounce have an issue with tmr.now() rollover? #2
Comments
Awesome, cheers for the heads-up; I had no idea. I'll get that sorted when as I can (might be a couple of days). Thanks. |
NP. I'm not too familiar with debounce methods but I didn't initially get yours because you react on the first event and then wait rather than trying to react only once the contact has settled. Anyway, good stuff! What I've been using for a while is the following: function start()
gpio.mode(pin, gpio.INT, gpio.PULLUP)
gpio.trig(pin, "down", doorLocked)
end
function doorLocked()
-- doStuffOnLocked()
tmr.delay(50) -- time delay for switch debounce
gpio.trig(pin, "up", doorUnlocked) -- change trigger on falling edge
end
function doorUnlocked()
-- doStuffOnUnlocked()
tmr.delay(50)
gpio.trig(pin, "down", doorLocked) -- trigger on rising edge
end Works quite reliably and is easy to understand. |
You're quite right; strictly it is a throttle function, rather than debounce. I'll take a look at what's common elsewhere and fall in line. |
This happens because nodemcu does a bitwise AND with 0x7fffffff on the system timer: https://github.com/nodemcu/nodemcu-firmware/blob/master/app/modules/tmr.c#L129 This wouldn't be a problem if nodemcu passed through the real bits, even though Lua ints are signed:
But because of the AND we get instead:
The easiest, cleanest, and probably most efficient fix is to always AND the result of comparing two tmr.now() values with 0x7fffffff:
|
Thanks for the valuable feedback!
Except that you need the NodeMCU bit module which you might otherwise not have included in your firmware. |
The default firmware has it, or at least mine does. If you build your own firmware I assume you can just remove the AND? |
Forget the code I posted above at Nov 17, 2015 - nothing I should be proud of. Using |
@marcelstoer, actually your Gist at https://gist.github.com/marcelstoer/75ba30a4aec56d1b3810 does suffer from a rare problem; if the button is in the "wrong" state when the timer fires, the next transition will be missed. I've been using the same technique for a while. To see the problem, set the debounceDelay to half a second or so, but press the button for less than that time. The next button press will be ignored. |
I prefer this technique which I keep at https://gist.github.com/marcelstoer/59563e791effa4acb65f (including the fix proposed in this issue). |
Hmm, thanks. Although The level value given to the trigger callback may be of use. https://nodemcu.readthedocs.io/en/master/en/modules/gpio/#gpiotrig level comes from the underlying SDK and NodeMCU just passes it on. Reference: https://github.com/nodemcu/nodemcu-firmware/blob/master/app/modules/gpio.c#L35 |
|
Your debounce code at https://github.com/hackhitchin/esp8266-co-uk/blob/master/tutorials/introduction-to-gpio-api.md was (indirectly) discussed at http://www.esp8266.com/viewtopic.php?f=24&t=4833&start=5#p29127. It essentially says
And it goes on to propose adding
if delta < 0 then delta = delta + 2147483647 end;
as a remedy.-> https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#tmrnow
The text was updated successfully, but these errors were encountered: