Shop OBEX P1 Docs P2 Docs Learn Events
Switch between 2 programs — Parallax Forums

Switch between 2 programs

JAMB1234JAMB1234 Posts: 8
edited 2008-10-16 13:26 in BASIC Stamp
The following are examples of two programs that I want my stamp to run. I would like to use a switch on pin 0 to switch from one to the other. I think I've tried every possible thing I could but have not been successful. I'm not sure this is even possible. Can someone please help.

Thanks

Here's the first program.

'Pin P3: Player A Pushbutton, Active High
'Pin P4: Player B Pushbutton, Active High
'{$STAMP BS2}
'{$PBASIC 2.5}

timecounterA VAR······· Word·············· ' Time Score of player A
timecounterB VAR······· Word·············· ' Yime Score of Player B

DEBUG "Press and hold pushbuttons" , CR

DO
·DO
·' Nothing
·LOOP UNTIL (IN3 = 1) AND (IN4 = 1)
HIGH 15
PAUSE 2000
HIGH 14
PAUSE 2000
HIGH 13
PAUSE 3000
HIGH 12
PAUSE 1000
LOW 15
LOW 13
LOW 14
LOW 12
HIGH 9
HIGH 10
HIGH 11
PAUSE 400
LOW 9
LOW 10
LOW 8
LOW 7
LOW 11
PAUSE 1

HIGH 8
HIGH 7
timecounterA = 0
timecounterB = 0
DO
PAUSE 1
IF (IN3 = 1) THEN
timecounterA = timecounterA + 1
ENDIF
IF (IN4 =1) THEN
timecounterB = timecounterB + 1
ENDIF
LOOP UNTIL (IN3 = 0) AND (IN4 = 0)
LOW 8
LOW 7
IF (timecounterA =0) THEN
DEBUG " Left Lane FOUL" , CR
DEBUG " Right Lane Wins" , CR, CR
HIGH 6
PAUSE 4000
LOW 6
ELSEIF (timecounterB = 0) THEN
DEBUG "· Right Lane FOUL" , CR
DEBUG·· "Left Land Wins" , CR
HIGH 5
PAUSE 4000
LOW 5
ELSE
DEBUG "Left Lane time: ", DEC timecounterA/265,".",DEC3 timecounterA//1000,CR
DEBUG "Right Lane time: ", DEC timecounterB/265,".",DEC3 timecounterB//1000, CR, CR
IF (timecounterA < timecounterB) THEN
··· DEBUG "Left Lane is the winner!", CR
HIGH 15
PAUSE 2000
LOW 15
ELSEIF (timecounterB < timecounterA) THEN
DEBUG "Right Lane is the winner!", CR
HIGH 14
PAUSE 2000
LOW 14
ELSE
DEBUG "It's a Tie!", CR
ENDIF
DEBUG CR
DEBUG "To play again, Hold the", CR
DEBUG "BUTTONS DOWN AGAIN.",CR, CR
ENDIF
LOOP

And here's the one I want to add.

'Pin P3: Player A Pushbutton, Active High
'Pin P4: Player B Pushbutton, Active High
'{$STAMP BS2}
'{$PBASIC 2.5}

timecounterA VAR······· Word·············· ' Time Score of player A

DEBUG "Press and hold pushbutton" , CR

DO
·DO
·' Nothing
·LOOP UNTIL (IN3 = 1)
HIGH 15
PAUSE 2000
HIGH 14
PAUSE 2000
HIGH 13
PAUSE 2000
HIGH 12
PAUSE 1000
LOW 15
LOW 13
LOW 14
LOW 12
HIGH 9
PAUSE 500
HIGH 10
PAUSE 500
HIGH 11
PAUSE 500
LOW 9
LOW 10
LOW 8
LOW 7
LOW 11
PAUSE 1

HIGH 8
HIGH 7
timecounterA = 0

DO
PAUSE 1
IF (IN3 = 1) THEN
timecounterA = timecounterA + 1

ENDIF
LOOP UNTIL (IN3 = 0)
LOW 8
LOW 7
IF (timecounterA =0) THEN
DEBUG " Left Lane foul " , CR
DEBUG " Right Lane Wins" , CR, CR
HIGH 6
PAUSE 4000
LOW 6

ELSE
DEBUG "Left Lane Time: ", DEC timecounterA/260,".",DEC2 timecounterA//1000, " seconds ", CR,CR

DEBUG "To play again, Hold the", CR
DEBUG "BUTTONS DOWN AGAIN.",CR, CR
ENDIF
LOOP

Comments

  • sylvie369sylvie369 Posts: 1,622
    edited 2008-10-14 12:25
    You can combine them into a single program, and have the BS2 read the switch status to decide which of two parts of the program to run (IF ... GOTO ...).

    However, that's only going to choose one or the other set of code when it reaches that step in the program. I think that it might be impossible to monitor the switch status while running the program so that you can switch to a different set of code "on the fly". I think that's the kind of thing that interrupts are for, which don't exist on the Basic Stamp series. It MIGHT be possible to do something like this using POLLIN:

    (From the manual, P. 313)
    "The POLLIN command is used to specify an input pin to monitor, or
    "poll", in-between instructions during the rest of the PBASIC program.
    The BASIC Stamp will then perform some activity (in-between
    instructions) when the specified State is detected."

    This will only work on one of the BSp chips (BS2p, BS2pe, BS2px). Oh, on re-reading this I see that it may be possible to put the two programs into two different "program slots" on those processors, and use the POLL commands to move from one to the other. I suggest you read the manual pages on those commands carefully - I'll bet your answer is in there, and that it's going to require you to use one of those three chips.
  • stamptrolstamptrol Posts: 1,731
    edited 2008-10-14 12:42
    One technique is to consider each piece of programming as a separate sub-routine.

    At the beginning of the program, you'll have a loop running that watches for pin 0 to be active. When its active it runs one sub-routine and when its not active it runs the other. Have a look at the IF-THEN-ELSE construct in the help file. You will be able to consolidate some of the declarations at the beginning of the program.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Jesse HastyJesse Hasty Posts: 29
    edited 2008-10-14 16:25
    Since each code list is practically identical to the other, I'd just use the pin 1 input to inhibit the references to the right lane where ever they occur. This would decrease the size of the program and insure that the timing was identical in both instances.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-10-16 00:21
    Jamb1234

    Here is something i did on a project a while back
    how this works is if i want it to run the other runtine i just hold the routine switch on power up and it runs the other routine

    These two routine are also different in the Show_State_Power_OFF and· ·Show_State_Power_ON
    all·other routine are the same· Show_Time·/ Halt_clock / Restart_clock / Display_Time

    ··Pick_ONE:
    DO
    DEBUG CLS
    IF IN14 = 0 THEN
    ·· GOSUB Power_ON
    ELSE
    ·· GOSUB Power_OFF
    ENDIF
    LOOP

    Power_ON:
    DO WHILE IN15 = 1
    GOSUB Show_Time
    GOSUB Show_State_Power_ON
    LOOP

    DO WHILE IN15 = 0
    GOSUB Halt_clock
    GOSUB Display_Time
    GOSUB Show_State_Power_ON
    ·LOOP

    GOSUB Restart_clock
    GOSUB Power_Restarts
    GOSUB Power_ON
    RETURN


    Power_Off:
    DO
    ·GOSUB Show_Time
    ·GOSUB Show_State_Power_OFF
    ·LOOP UNTIL IN15 = 1

    ·DO
    ·GOSUB Display_Time

    ·GOSUB Halt_clock
    ·GOSUB Show_State_Power_OFF
    LOOP UNTIL IN15 = 0

    GOSUB Restart_clock
    GOSUB Power_restarts:
    GOTO Power_Off
    RETURN


    Jesse Hasty

    This what I did in these two routine is this what you are talking about

    I hope this helps Jamb1234


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 10/16/2008 12:46:00 AM GMT
  • Jesse HastyJesse Hasty Posts: 29
    edited 2008-10-16 13:26
    Sam,

    Your subroutine approach has part of the effect to which I was alluding. Your approach would keep the "token count" down, as it were. But by using subroutines you cause a lot of transfers of control. These transfers take up a lot of machine cycles since the processor has to push the registers, accumulator, and program pointer onto the stack and then pop them after the return. This would work well in most cases, but if you are timing on a 1 ms time slice it may not be accurate, unless the timing occurs within the sub routine. If all the timing occurs within one subroutine it might be necessary to start the timer counter at 1 or 2 in order for the results to be accurate.

    FWIW

    Jesse
Sign In or Register to comment.