Toggle function possible with momentary switch in BoeBot kit?
John Kauffman
Posts: 653
I'd like to use the momentary contact switch in the BoeBot kit as a toggle to turn on/off a SUB that senses a stimulus and reacts. The sub would be called again and again (I call this Active=True) until the switch is pressed again and it goes to Active=false. The program is then waiting for the next switch press to toggle back to Active=1
Above happens without interaction with PowerSwitch - the idea is that power is turned on for the BOE and it is the momentary contact switch that controls turn on/off the action.
Below is my latest try. There is an LED "ledActiveOn" to display the state & make troubleshooting easier
But the code seems to cycle through on / off states without interaction from the switch.
'Pins '''''''''''''
swtActiveOn PIN 0 ' press should toggle between Active On and Active Off
ledActiveOn PIN 1 ' indicates System on
' VARS '''''''''''''''''''''
bitActiveOn VAR Bit ' Holds current state of Active: on=1, off=0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
bitActiveOn = 0 ' start off
DO
IF bitActiveOn = 1 THEN ' starting state = on, so will set active state = off, not do code
LOW ledActiveOn
bitActiveOn = 0 ' set active to off
ELSE ' starting state = off, so will do code and turn active = on
HIGH ledActiveON
GOSUB Activity
bitActiveOn = 1 ' set to Active = ON
ENDIF
PAUSE 100
LOOP
' SUB activity will run once when state is Active=1.
Activity:
' begin RCtime
' check RCtime result
' react to RCTime result
RETURN
Above happens without interaction with PowerSwitch - the idea is that power is turned on for the BOE and it is the momentary contact switch that controls turn on/off the action.
Below is my latest try. There is an LED "ledActiveOn" to display the state & make troubleshooting easier
But the code seems to cycle through on / off states without interaction from the switch.
'Pins '''''''''''''
swtActiveOn PIN 0 ' press should toggle between Active On and Active Off
ledActiveOn PIN 1 ' indicates System on
' VARS '''''''''''''''''''''
bitActiveOn VAR Bit ' Holds current state of Active: on=1, off=0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
bitActiveOn = 0 ' start off
DO
IF bitActiveOn = 1 THEN ' starting state = on, so will set active state = off, not do code
LOW ledActiveOn
bitActiveOn = 0 ' set active to off
ELSE ' starting state = off, so will do code and turn active = on
HIGH ledActiveON
GOSUB Activity
bitActiveOn = 1 ' set to Active = ON
ENDIF
PAUSE 100
LOOP
' SUB activity will run once when state is Active=1.
Activity:
' begin RCtime
' check RCtime result
' react to RCTime result
RETURN
Comments
I've done some debug to see the switch action (active high, pull-down resistor ) without problems.
I think I see the problem, but don't know how to solve. I believe each loop is just flipping active states on its own and the switch is immaterial. I'm guessing people smarter than I have come across this and found a different way to code the relationship between the switch and the active on/off flag.
It looks like you never test the condition of PIN0 (swtActiveOn) in your routine.
If swtActiveOn=0 THEN bitActiveOn = 0
ELSE bitActiveOn = 1
Jim
As an aside, maybe TOGGLE command works for variable, I could only get to work with a pin. Perhaps could dedicate an used pin to be the Active state bit and thus use Toggle command
For anyone finding this in a future search, here is working code. I included an abundance of comments and debugs to explain.
Search terms: switch toggle loop momentary software code
' use a momentary contact switch (in Boe-Bot kit) to toggle state between ActiveOn and ActiveOff
swtToggleState PIN 0
bitState VAR Bit ' 1 = ActiveOn
bitstate = 0 ' start with active off
DO
DEBUG "begin loop",TAB
' check switch
IF swtToggleState = 1 THEN 'switch closed so take action
DEBUG "swtich closed ", TAB
' check active state
IF bitstate=0 THEN ' active off
DEBUG "start bitstate = ", BIN bitstate, TAB
GOSUB ACtiveOn
bitstate = 1
DEBUG "end bitstates = ", BIN bitstate, TAB
ELSE ' active on
DEBUG "start bitstates = ", BIN bitstate, TAB
GOSUB ACtiveOff
bitstate = 0
DEBUG "end bitstates = ", BIN bitstate, TAB
ENDIF ' check active state
ELSE 'switch open so no action
DEBUG "swtich open ", TAB
ENDIF 'check switch
DEBUG "end loop",CR
PAUSE 100
LOOP
activeOn:
RETURN
activeOff:
RETURN