Shop OBEX P1 Docs P2 Docs Learn Events
Start Switch — Parallax Forums

Start Switch

bennettdanbennettdan Posts: 614
edited 2006-05-04 17:57 in BASIC Stamp
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

Comments

  • NewzedNewzed Posts: 2,503
    edited 2006-05-04 11:35
    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, [noparse][[/noparse]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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-04 14:39
    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
  • NewzedNewzed Posts: 2,503
    edited 2006-05-04 14:50
    Chris, shouldn't that be

    LOOP UNTIL IN(0) = 0

    Sid
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-04 15:05
    No, that type of context refers to an array element.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • FranklinFranklin Posts: 4,747
    edited 2006-05-04 16:42
    I think Newzed ment LOOP UNTIL IN0 = 0 instead of WHILE IN0 = 0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-04 16:46
    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
  • bennettdanbennettdan Posts: 614
    edited 2006-05-04 16:58
    Thanks guys I got it going with the While statement.
  • NewzedNewzed Posts: 2,503
    edited 2006-05-04 17:05
    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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-04 17:57
    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
Sign In or Register to comment.