Last active
October 3, 2018 14:31
-
-
Save marcelstoer/75ba30a4aec56d1b3810 to your computer and use it in GitHub Desktop.
Debounce with NodeMCU with two separate functions for down/up trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- inspired by: http://www.esp8266-projects.com/2015/03/buttons-pushbuttons-and-debouncing-story.html | |
local GPIO14 = 5 | |
local debounceDelay = <however-many-ms-your-sensor-requires> | |
local debounceAlarmId = <0-6> | |
gpio.mode(GPIO14, gpio.INT, gpio.PULLUP) | |
gpio.trig(GPIO14, "down", doorLocked) | |
function doorLocked() | |
-- don't react to any interupts from now on and wait 50ms until the interrupt for the up event is enabled | |
-- within that 50ms the switch may bounce to its heart's content | |
gpio.trig(GPIO14, "none") | |
tmr.alarm(debounceAlarmId, debounceDelay, tmr.ALARM_SINGLE, function() | |
gpio.trig(GPIO14, "up", doorUnlocked) | |
end) | |
-- finally react to the down event | |
end | |
function doorUnlocked() | |
-- don't react to any interupts from now on and wait 50ms until the interrupt for the down event is enabled | |
-- within that 50ms the switch may bounce to its heart's content | |
gpio.trig(GPIO14, "none") | |
tmr.alarm(debounceAlarmId, debounceDelay, tmr.ALARM_SINGLE, function() | |
gpio.trig(GPIO14, "down", doorLocked) | |
end) | |
-- finally react to the up event | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you marcelstoer for formatting my post.