General area when it fits no where else

Moderator: Mmiscool

User avatar
By kumaran512
#46286 Electroguard, hope push button works for you.

Can we use Interrupt command for hardware push button, will it be stable without crashes :roll: ?

Anyone have example please post it.
User avatar
By Electroguard
#46497 Sorry it's taken a while, but to answer the rest of the question in a useful manner...

It seems that the interrupt instruction requires a pullup to register a change of state from a hardware button press, but it also responds to the release, thereby giving 2 interrupts per button press/release, which effectively cancel each other out if you are trying to toggle a led.

To get around this, instead of branching on interrupt directly to [toggle] the led, the example below branches to [pressed] which only branches to [toggle] every other interrupt.

It's using the onboard gpio00 button and gpio01 led so you don't need to add hardware to try it other than a pullup for the gpio00 button.

Hopefully it will make more sense when you run it.

Code: Select alllet ledpin = 1              '  Your chosen led  (if ledpin = 1 it uses the esp's onboard blue led)
let ledstate = 1            '  Default startup condition for the led
let buttonstate = "up"
po ledpin ledstate          '  Set led to startup condition (off)
let inputpin = 0            ' Using onboard GOIO00 flashing button by default, change to suit.
interrupt inputpin [pressed]  ' Will require pull-up resistor to work.

[screen]
cls
button "On" [on]
button "Off" [off]
html "<BR>"
button "Toggle" [toggle]    ' new toggle button added
html "<BR>"
if ledstate == 1 then button "led is OFF - Turn it ON" [on]
if ledstate == 0 then button "led is ON - Turn it OFF" [off]
html " (give time for  button status to update before clicking again)"
html "<BR>"
html "interrupts = " & count
wait

[pressed]
' This is needed because pressing and releasing the button causes 2 interrupts, press and release.
if buttonstate == "up" then buttonstate = "down" else buttonstate = "up"
if buttonstate == "down" then goto [toggle] else goto [screen]  'Only toggles led when button is pressed, not released.

[toggle]
if ledstate == 0 then ledstate = 1 else ledstate = 0
po ledpin ledstate
goto [screen]

[on]
let ledstate = 0
po ledpin ledstate   
goto [screen]

[off]
let ledstate = 1           
po ledpin ledstate   
goto [screen]

User avatar
By kumaran512
#46879 Nice coding.

It works, but after 20-25 button presses esp resets.

Also
Code: Select allhtml "interrupts = " & count
does to change any values in html page.

If I test the same with external ESP-12E module the pin is very sensitive, if my hand touches the pin it toggles. :x