General area when it fits no where else

Moderator: Mmiscool

User avatar
By kumaran512
#46189 I use the below code to toggle an led. Now I insert an push button to toggle the same led externally. Please help me up with the code for input pin monitoring, i tried with my beginner skills went void :cry: :cry:

Code: Select allButton "On" [on]
Button "Off" [off]
Wait

[on]
Po 2 0
Wait

[off]
Po 2 1
Wait
User avatar
By Electroguard
#46226 Hi, and welcome.

I should mention that this thread is actually for 'new program functionality' requests though, rather than help requests, so best asking for help in General thread in future, ok.

And to save you some future head-scratching, I suggest you Don't Mix Case in your scripts because some things are case sensitive and just won't work.

I need to point out that your code example does not actually toggle the led... each web button will only turn it on or off. Thats not being pedantic, it's to let you know that things may not work as you expect even if and when you can react to a hardware button press (I haven't been able to).
If you want to do a toggle, you only need a single button that toggles between on and off with each press, but to do that you need to be able to be aware of the current led state in order to change it. A simple way to do that is to create a variable for remembering the state, ie; let ledstate = 1. If you do that, you need to know its's original state, and update that state every time anything changes the state, including your on and off branches.

Might as well keep things neat and tidy by also creating a variable for the led pin number, which will allow you to easily change led pins later if you wish simply by changing that single line, rather than having to change every mention of a specified pin number.
For instance, I've specified 'let ledpin = 1' to use the esp's onboard blue gpio01 led, but you can change it back to 2 simply by changing to 'let ledpin = 2'.
Here's what your code looks like after adding a toggle button, a toggle branch for the button to goto, and a variable to remember the led state...

Code: Select alllet ledpin = 1              '  Your chosen led
let ledstate = 1           '  Default startup condition for the led
po ledpin ledstate    '  Set led to startup condition (off)

Button "On" [on]
Button "Off" [off]
Button "Toggle" [toggle]    ' new toggle button added
wait

[on]
let ledstate = 0
po ledpin ledstate   
wait

[off]
let ledstate = 1           
po ledpin ledstate   
wait

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


If instead of 'wait'ing in all branches you return to just before where the buttons are declared, you have opportunity to re-paint the buttons after each press to show the current state if you wish (the program still drops down to the 'wait' below).
Easier to show than explain, so try this example and note the extra button that changes it's name to let you know in the browser whether it is on or off.

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
po ledpin ledstate          '  Set led to startup condition (off)

[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)"
wait

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

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

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

As far as the hardware button is concerned, I'm not the best person to be trying to help you with that, because I can't get a button press working for myself, and can't get hold of a complete recent proven example which I can copy and paste to rule out finger trouble or pinpoint if it might my particular hardware perhaps.
You can't poll for a pin if the program is 'wait'ing for browser input, so presumably you need to use the interrupt instruction, and if something works for you I would appreciate it if you could please let me know what.
User avatar
By viscomjim
#46236 Electroguard, this is a GREAT example!!!! This should be put in the manual as it is clean, well remarked and works perfectly. Also, should be moved to program example thread.

I am using a wemos mini d1 so the only change, if you want to use the onboard led for testing, is change pin 1 to pin 2.

Great Job, keep em' coming!!!!