Skip to content

Instantly share code, notes, and snippets.

@lalyos
Forked from marcelstoer/debounce.lua
Created March 20, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalyos/505856beee171242b082454e4ceb8810 to your computer and use it in GitHub Desktop.
Save lalyos/505856beee171242b082454e4ceb8810 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment