lua - NodeMCU gpio triggering incorrectly -
i'm attempting read ir information nodemcu running lua 5.1.4 master build of 8/19/2017.
i might misunderstanding how gpio works , i'm having hard time finding examples relate i'm doing.
pin = 4 pulse_prev_time = 0 ircallback = nil function trgpulse(level, now) gpio.trig(pin, level == gpio.high , "down" or "up", trgpulse) duration = - pulse_prev_time print(level, duration) pulse_prev_time = end function init(callback) ircallback = callback gpio.mode(pin, gpio.int) gpio.trig(pin, 'down', trgpulse) end -- example print("monitoring ir") init(function (code) print("omg got something", code) end)
i'm triggering initial interrupt on low, , alternating low high in trgpulse
. in doing i'd expect levels alternate 1 0 in perfect pattern. output shows otherwise:
1 519855430 1 1197 0 609 0 4192 0 2994 1 589 1 2994 1 1198 1 3593 0 4201 1 23357 0 608 0 5390 1 1188 1 4191 1 1198 0 3601 0 3594 1 25147 0 608 1 4781 0 2405 1 3584 0 4799 0 1798 1 1188 1 2994
so i'm doing wrong or fundamentally don't understand how gpio works. if expected, why interrupts being called multiple times if low/high levels didn't change? , if seem wrong, ideas how fix it?
i'm doing wrong or fundamentally don't understand how gpio works
i suspect it's bit combination of both - latter may cause former.
my explanation may not 100% correct mechanical/electronic perspective (not world) should enough far writing software gpio goes. switches tend bounce between 0 , 1 until settle one. article read on https://www.allaboutcircuits.com/technical-articles/switch-bounce-how-to-deal-with-it/. effect can addressed hardware and/or software.
doing software involves introducing form of delay skip bouncing signals you're interested in "settled state". documented nodemcu lua function use @ https://gist.github.com/marcelstoer/59563e791effa4acb65f
-- inspired https://github.com/hackhitchin/esp8266-co-uk/blob/master/tutorials/introduction-to-gpio-api.md -- , http://www.esp8266.com/viewtopic.php?f=24&t=4833&start=5#p29127 local pin = 4 --> gpio2 function debounce (func) local last = 0 local delay = 50000 -- 50ms * 1000 tmr.now() has μs resolution return function (...) local = tmr.now() local delta = - last if delta < 0 delta = delta + 2147483647 end; -- proposed because of delta rolling over, https://github.com/hackhitchin/esp8266-co-uk/issues/2 if delta < delay return end; last = return func(...) end end function onchange () print('the pin value has changed '..gpio.read(pin)) end gpio.mode(pin, gpio.int, gpio.pullup) -- see https://github.com/hackhitchin/esp8266-co-uk/pull/1 gpio.trig(pin, 'both', debounce(onchange))
note: delay
empiric value specific sensor/switch!
Comments
Post a Comment