PDA

View Full Version : Start Switch



bennettdan
05-04-2006, 03:28 PM
I have a program that uses a DO LOOP to call subroutines, the problem I have is I want a switch when turned On it will start·the DO LOOP and when turned off will stop the program.

Example of code

DO
GoSUB 1
GoSub 2
GoSub 3
Loop

Post Edited (bennettdan) : 5/4/2006 7:45:19 AM GMT

Newzed
05-04-2006, 07:35 PM
Simple enough.· At the beginning of your program write:

begin:

debug "Press any key to start", cr
debugin com
go to start

start:

DO/Loop

As for stopping, find where you want to stop, then write:

serin pin, baud, 100, cont, [com]
goto begin

cont: 'continue with program

The serin will wait 100ms for an input.· If it gets nothing it will jump to cont and continue the program.· If it gets something it will go back to begin.· The 100 can be adjusted to whatever length of time your program can handle.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester yet?
http://hometown.aol.com/newzed/index.html

·

Chris Savage
05-04-2006, 10:39 PM
Assuming you have an active-low switch connected to P0 the following code will do what you ask.



' {$STAMP BS2}
' {$PBASIC 2.5}

DO : LOOP WHILE IN0 = 1

DO
GOSUB Routine1
GOSUB Routine2
GOSUB Routine3
LOOP WHILE IN0=0

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Newzed
05-04-2006, 10:50 PM
Chris, shouldn't that be

LOOP UNTIL IN(0) = 0

Sid

Chris Savage
05-04-2006, 11:05 PM
No, that type of context refers to an array element.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Franklin
05-05-2006, 12:42 AM
I think Newzed ment LOOP UNTIL IN0 = 0 instead of WHILE IN0 = 0

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

Chris Savage
05-05-2006, 12:46 AM
Okay, I think I see what you're getting at...And no, because the switch state is incorrect.· If it said UNTIL IN0 = 1 then it could work.· Then again moving the conditionals to the DO instead of LOOP could work as well...There's a lot of ways this could work as long as the logic and syntax is correct.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

bennettdan
05-05-2006, 12:58 AM
Thanks guys I got it going with the While statement.

Newzed
05-05-2006, 01:05 AM
Chris Savage (Parallax) said...
Assuming you have an active-low switch connected to P0 the following code will do what you ask.



' {$STAMP BS2}
' {$PBASIC 2.5}

DO : LOOP WHILE IN0 = 1

DO
GOSUB Routine1
GOSUB Routine2
GOSUB Routine3
LOOP WHILE IN0=0

I still don't get it, Chris.· You say

LOOP WHILE IN0 = 11
then DO.......

LOOP WHILE IN0 = 0
If he throws his switch so IN0 = 1, then IN0 will still be 1 after the third GOSUB and the loop will shut down after one cycle.· I suppose if he was using a pushbutton and pushed it very quickly the loop would start and if he let go of the button before the third GOSUB ended then the loop would continue running.· If he pushes the button to stop it it is going to start over unless he is very quick.
Sid

Chris Savage
05-05-2006, 01:57 AM
Sid,

·· If you read the original post you would see that I mentioned if you have an active LOW switch connected.· This means the OPEN state of the switch will be 1 per the pull-up resistor.· When the switch is CLOSED (turned on) the signal will be LOW and the code will dropw through into the DO...LOOP.· At this point the switch is still in a 0 state...And the code says to LOOP WHILE in that state.· When the switch is turned off (opened) it will go HIGH again and the program will end.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)