Condition check using single switch as toggle on/off?
Oldbitcollector (Jeff)
Posts: 8,091
Ok, this is probably very simple, but it's been driving me crazy for the last hour.
Time to ask for help...
I'm trying to use a single pushbutton input as an on/off toggle in my loop.
(Thanks Nick for this headache. [noparse]:)[/noparse]
I can't seem to get wrap my head around detecting the push of button
to change from and on/off state in the loop without it detecting wrong.
something like this:
Hopefully I've given and idea what I'm shooting for. Thanks in advance!
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
Time to ask for help...
I'm trying to use a single pushbutton input as an on/off toggle in my loop.
(Thanks Nick for this headache. [noparse]:)[/noparse]
I can't seem to get wrap my head around detecting the push of button
to change from and on/off state in the loop without it detecting wrong.
something like this:
counter++ 'main loop counter if ina[noparse][[/noparse]play] == 1 and counter > 5 '' Stop what I'm doing here if ina[noparse][[/noparse]play] == 1 '' Continue
Hopefully I've given and idea what I'm shooting for. Thanks in advance!
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
Comments
Two buttons, one "Start" and the other "Stop" are really easy to do with IF statements.
To set up toggling with one button, you'll first need to clean up the signal with debounce.
Microswitches don't bounce for more than about 20ms, but I've seen 12.5ms debounce times as well.
After you can reliably detect a button press, there's a need to find the rising edge of the debounced signal.
Then use the rising edge flag in the if statement to toggle the state of something.
Button = 0, State = 0
Button = 1, State = 0, Button XOR State = 1
Toggle State: State = 1
{Delay in which button is released}
Button = 0, State = 1, Button XOR State = 1
Toggle State: State = 0
{Delay, button is still released}
Button = 0, State = 0, Button XOR State = 0
Hope it helps.
You need a DEBOUNCING routine to make sure that you are getting a CLEAN state.
Try this routine
The line before last can be replaced with a DELAY (e.g. Waitcnt()) to enable a button to
register as MULTIPLE pushes if it stays down. The delay will define how fast the REPEAT RATE
is going to be.... just like in a PC keyboard.
If you keep the line then you will have to Push the button and Release it to register as a push.
The routine assumes an ACTIVE LOW button.... if you want active high then reverse the
logic whenever there is an InA[noparse]/noparse.
Regards
Samuel
P.S. as you can tell from the code this will tie up the program execution if the button is pushed
and stays pushed....I you need CONCURRENT execution then use a cog with the routine.
Post Edited (SamMishal) : 12/28/2009 4:39:10 AM GMT
Of course, this kind of code must run in a loop and that's not always convenient. It would be pretty easy to create a PASM cog that monitored several inputs, toggling their state (stored in the hub) as required.
That is the elegant solution I was looking for.
It looks like there is more going on in the code, so I've got more digging to do elsewhere.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.