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 |
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
Hi I am new to this site so please forgive me for a very poor looking post.
I have written several lua interrupt handlers using OO timers and they make
coding easier and future proof. Please see badly pasted example lua code below...
(It has lost all it's formating. Cut and paste into Visual Studio, save to a *.lua file and reformat)