Shop OBEX P1 Docs P2 Docs Learn Events
converting BS2 code to BS1 — Parallax Forums

converting BS2 code to BS1

MatthewMatthew Posts: 200
edited 2005-01-16 17:56 in BASIC Stamp
So I·recently made a simple code a BS2. Now I'm trying to translate it so the BS1 can understand it. But one problem arises, the BS1 doesn't understand 'elseif' commands.

Pretty much the program is monitoring 2x push-down/push-up buttons, and depending on which configuration it sees (only 4 different kinds) it will turn a continuous rotating servo either clockwise or counterclockwise.

Here's my code, any help would be greatly apreciated:


'
[noparse][[/noparse] Title ]
' What's a Microcontroller - String Aligner 1.bs2
' String Aligner 1
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] Declarations ]

'
[noparse][[/noparse] Initialization ]
direction VAR Bit

'
[noparse][[/noparse] Main Routine ]
DO
· GOSUB Check_Switches
· GOSUB Turn_Servo
· GOSUB Display
LOOP

'
[noparse][[/noparse] Subroutines ]
Check_Switches:
IF (IN1=0) AND (IN2=0) THEN
direction=0
ELSEIF (IN1=0) AND (IN2=1) THEN
direction=1
ELSEIF (IN1=1) AND (IN2=1) THEN
direction=0
ELSEIF (IN1=1) AND (IN2=0) THEN
direction=1
ENDIF
RETURN

Turn_Servo:
IF direction=0 THEN
· PULSOUT 14, 1000
· PAUSE 18
ELSEIF direction=1 THEN
· PULSOUT 14, 500
· PAUSE 18
ENDIF
RETURN

Display:
IF direction=1 THEN
DEBUG HOME, "Turning right"
ELSEIF direction=0 THEN
DEBUG HOME, "Turning left "
ENDIF
RETURN

Comments

  • dandreaedandreae Posts: 1,375
    edited 2005-01-15 18:24
    I·believe that the "BRANCH" command will help you with·this project regarding the BS1.· Page 83 of the BASIC Stamp Manual version 2.0 will have some examples on how to implement it in your program.

    Dave



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave Andreae

    Tech Support
    dandreae@parallax.com
    www.parallax.com

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-15 19:36
    I've got to run out the door, so I'll leave you to figure this out.· The greatest simplifcation is your logic: you can use ^ (XOR) because when one bit or the other is high, but not both, the direciton is one.
    '----- [noparse][[/noparse] Title ] ------------------------------------------------------
    '
    ' What's a Microcontroller - String Aligner 1.BS1
    ' String Aligner 1
    '
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
     
    
    '----- [noparse][[/noparse] Declarations ] -----------------------------------------------
     
    
    '----- [noparse][[/noparse] Initialization ] ---------------------------------------------
     
    SYMBOL  direction       = BIT0
     
    
    '----- [noparse][[/noparse] Main Routine ] -----------------------------------------------
     
    Main:
      GOSUB Check_Switches
      GOSUB Turn_Servo
      GOSUB Display
      GOTO Main
     
     
    '----- [noparse][[/noparse] Subroutines ] ------------------------------------------------
     
    Check_Switches:
      direction = PIN1 ^ PIN2
      RETURN
     
    
    Turn_Servo:
      IF direction = 1 THEN Turn_1
      PULSOUT 7, 200
      GOTO Turn_Exit
     
    Turn_1:
      PULSOUT 7, 50
     
    Turn_Exit:
      PAUSE 18
      RETURN
     
    
    Display:
      DEBUG CLS
      IF direction = 1 THEN Display_1
      DEBUG "Turning left"
      GOTO Display_Exit
     
    Display_1:
      DEBUG "Turning right"
     
    Display_Exit:
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA


    Post Edited (Jon Williams) : 1/16/2005 5:54:13 PM GMT
  • MatthewMatthew Posts: 200
    edited 2005-01-16 06:49
    Thanks Jon. Seems like the BS1 code is quite different from the BS2. I understand most of it except the:
    Check_Switches:
    direction = PIN1 ^ PIN2
    RETURN

    I'll look up what the ^ sign means.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-16 17:56
    The ^ (XOR - exclusive OR) is a logical operator. Here's the truth table:

    0 ^ 0 = 0
    0 ^ 1 = 1
    1 ^ 0 = 1
    1 ^ 1 = 0

    The way to remember is this: If one bit or the other is 1 -- but NOT both -- then the result is 1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
Sign In or Register to comment.