Shop OBEX P1 Docs P2 Docs Learn Events
Conditional statements and QTI's — Parallax Forums

Conditional statements and QTI's

gbd28gbd28 Posts: 2
edited 2009-12-13 13:41 in Accessories
I'm working with the line follow code that is supplied with the QTI sensor kit. It works great but I have two situations where the sensors read

CASE %1110

and I need to distinguish between them so that I can run different subroutines at different times. I have tried using "IF THEN" statements but I am not
getting the desired results. Is there some way to make the "case" both a condition and a variable? I am new to pbasic and would appreciate any help that is offered.

Comments

  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-12-12 00:55
    Hi gbd28,

    What are the two conditions that causes the sensors to read %1110, and what makes them different? If you attach the code that you have, it can help us get a better understanding of what you want to do.

    One solution would be to have the CASE %1110 go to a subroutine with IF...THEN statements to make another decision based on other parameters. But also keep in mind that you can only nest 4 subroutines before the program will get lost.

    Hope this helps.

    -- Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax Inc.
  • gbd28gbd28 Posts: 2
    edited 2009-12-13 00:34
    Thanks for your reply. The two conditions are both both 90 degree left turns. But I need the boebot to pause and activate a servo the first time it encounters the case %1110 before returning to the line follow routine. The second time it detects case %1110 I need the boebot to pause and turn 45 degrees to the right, activate the servo, then return to the original line by turning approximately 135 degrees then returning to the line follow routine.

    I have been successful at programing both of these individual series of operations with sub routines but have not found a way to have both perform.

    The code is

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    qtis VAR Nib             'qti black/white states
    
    OUTB = %1111
    
    DO
      GOSUB Check_Qtis
    
      SELECT qtis
    
    CASE %1000
    PULSOUT 13, 650                      '13 left
    PULSOUT 12, 650                      '12 right
    CASE %1100
    PULSOUT 13, 750
    PULSOUT 12, 650
    CASE %0100
    PULSOUT 13, 800
    PULSOUT 12, 650
    CASE %0110
    PULSOUT 13, 840
    PULSOUT 12, 650
    CASE %0010
    PULSOUT 13, 850
    PULSOUT 12, 700
    CASE %0011
    PULSOUT 13, 850
    PULSOUT 12, 750
    CASE %0001
    PULSOUT 13, 850
    PULSOUT 12, 850
    CASE %1110
    PAUSE 500                   ' Stop for 1/2 second
    GOSUB second_base     ' Turn approximately 45deg clockwise, resulting in right turn
    GOSUB servo14            ' Activate continuous servo for firing mechanism
    GOSUB line_return1      ' Turn approximately 135deg counter-clockwise, resulting in left turn and returning to line follow
    counter VAR Word
    FOR counter = 1 TO 25  ' Servos rotate simultaniously in opposite direction
    PULSOUT 12, 650          ' causing unidirectional motion
    PULSOUT 13, 750
    PAUSE 20
    NEXT
    CASE %1111
    PAUSE 500                   ' Stop for 1/2 second
    GOSUB third_base        ' Turn approximately 90deg clockwise, resulting in right turn
    'PAUSE 1000
    GOSUB servo14            ' Activate continuous servo for firing mechanism
    GOSUB line_return2      ' Turn approximately 135deg counter-clockwise, resulting in left turn and returning to line follow
    CASE %0000
    PULSOUT 13, 750
    PULSOUT 12, 650
    CASE ELSE
      PAUSE 3
    ENDSELECT
    
    LOOP
    '----------TURN TO SHOOT @ 2ND BASE SUBROUTINE---------
    second_base:
    FOR counter = 1 TO 38 ' Servos rotate simultaniously in opposite direction
    PULSOUT 13, 850       ' causing unidirectional motion
    PULSOUT 12, 850
    PAUSE 0
    NEXT
    RETURN
    '----------TURN TO SHOOT @ 3RD BASE SUBROUTINE---------
    third_base:
    FOR counter = 1 TO 80 ' Servos rotate simultaniously in opposite direction
    PULSOUT 13, 850       ' causing unidirectional motion
    PULSOUT 12, 850
    PAUSE 0
    NEXT
    RETURN
    '----------SERVO 14 SUBROUTINE-----------------
    servo14:
    FOR counter = 1 TO 1000
    PULSOUT 14, 850
    NEXT
    PAUSE 500
    RETURN
    '----------LINE RETURN 1 SUBROUTINE---------
    line_return1:
    FOR counter = 1 TO 113 ' Servos rotate simultaniously in opposite direction
    PULSOUT 13, 650       ' causing unidirectional motion
    PULSOUT 12, 650
    PAUSE 0
    NEXT
    RETURN
    '----------LINE RETURN 2 SUBROUTINE---------
    line_return2:
    FOR counter = 1 TO 150 ' Servos rotate simultaniously in opposite direction
    PULSOUT 13, 650       ' causing unidirectional motion
    PULSOUT 12, 650
    PAUSE 0
    NEXT
    RETURN
    
    Check_Qtis:
      'Result -> qtis variable. 0 means white surface, 1 means
      'black surface
    
    DIRB = %1111              'P7..P4 -> output
    PAUSE 0                   'delay = 230 us
    DIRB = %0000              'P7..P4 -> input
    PAUSE 0                   'Delay = 230 us
    qtis = INB
    
    RETURN
    



    I hope this more clearly defines my difficulty. Thanks you again for any and all help
  • sailman58sailman58 Posts: 162
    edited 2009-12-13 13:41
    There are probably more elegant methods of doing this, but the obvious one to me would be to set up a counter initialized to zero. Use an if to check the value in your case %1110 statement. The first time through, increment the counter in the sub routine.

    I know this is the condensed version, but if you need more detail and nobody else chimes in before I return from my trip today, I can talk more then.

    Gotta run,

    Ron aka sailman58
Sign In or Register to comment.