Skip to content

Instantly share code, notes, and snippets.

@marcelstoer
Last active October 3, 2018 14:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcelstoer/75ba30a4aec56d1b3810 to your computer and use it in GitHub Desktop.
Save marcelstoer/75ba30a4aec56d1b3810 to your computer and use it in GitHub Desktop.
Debounce with NodeMCU with two separate functions for down/up trigger
-- 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
@ChrisTBarnes
Copy link

Thank you marcelstoer for formatting my post.

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