Left here for archival purposes.

User avatar
By mrgreedy
#11346 Hey.

Noticed that with a very simple circuit connecting GPIO2 to ground that a lot of the time the ESP01 misses interrupts.. One is triggered when the button is depressed and another SHOULD be when the button is released.. But this only happens 90% of the time.

Is there something wrong with the LUA code, or do I need additional electronics in my circuit to make it more "obvious" to the ESP01 what is happening?

Thanks.
User avatar
By picstart
#11361 All mechanical switches bounce ( rapid on off before settling into on or off state) . Interrupts are very very fast compared to the typical switch bounce so the interrupt will of react to the bounce state which is very variable. The solution is to use a timer to de-bounce. On the first interrupt the timer is started and it then measures the down time or up time depending on the transition if it is up or down for longer than the bounce time it is a valid transition.
User avatar
By Vitoa
#11974 picstart is correct
U should add a kind of delay and only read pin after about 100mS after button pressed, the first mS since button pressed are unstable.
interrupt detected -> delay 100mS -> read pin

pin=4
function start_delay()
tmr.alarm(0, 100, 0, function() print(gpio.read(pin)) end )
end
gpio.trig(4, "both",start_delay)

Report if this works
Other point is that using interruption it consumes some processing when triggered. During first mS of button pressed the input oscilates between 0 and 1 a lot of times.
So interrupt is called lots of times consuming much more processing power
U can add a periodic pin read for example each 300mS and if press detected call some function to something(better also add 100mS debounce time) Reading io each 300mS consume of very few proessing power, and u can read many pins at same time, and call function for each pin
See also
https://github.com/nodemcu/nodemcu-firm ... en#nodekey
https://github.com/nodemcu/nodemcu-firm ... w_gpio_map

Regards,Vito