Shop OBEX P1 Docs P2 Docs Learn Events
how do i? — Parallax Forums

how do i?

khrishnabrownkhrishnabrown Posts: 7
edited 2009-11-11 17:51 in BASIC Stamp
I'm trying to make my bot turn right then left after the same event.

I have sub routines that make it go right, left and forward. I’m using inferred to detect something in front of it. So it should be moving forward and when it sees the thing in front it should turn right and keeps moving. If it sees something in front of it again, it should turn left and keep moving. Any help on what to put in the do loop?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-03 05:33
    Use a variable (like a BIT) to remember what to do if the IR detection code sees something. Say you initialize it to zero. If this variable is zero and an object is detected, change the variable to one, call the turn right routine, then continue. If this variable is a one, change the variable to zero, call the turn left routine, then continue.
  • khrishnabrownkhrishnabrown Posts: 7
    edited 2009-11-07 06:26
    Ok, I couldn't figure it out. here i smy code so far. I have the left subrutine but don't know how to call it after the second time seeing something.



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

    irdetect VAR Bit
    pulse VAR Byte

    DO
    GOSUB move
    GOSUB forward
    LOOP

    move:

    FREQOUT 4, 1, 38500

    irdetect = IN6

    IF (irdetect = 0) THEN
    GOSUB right
    ENDIF

    forward:

    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    RETURN

    right:
    · FOR· pulse = 0 TO 20
    ·· PULSOUT 13, 850
    ·· PULSOUT 12, 850
    ·· PAUSE 20
    · NEXT
    · RETURN

    left:
    · FOR· pulse = 0 TO 20
    ·· PULSOUT 12, 650
    ·· PULSOUT 13, 650
    ·· PAUSE 20
    · NEXT
    · RETURN
  • W9GFOW9GFO Posts: 4,010
    edited 2009-11-07 07:08
    There is no "Return" in the "move" routine.

    Create another bit VAR called "last" or something. When you have turned right set the bit to 1, when left, set it to zero. Check it each time before deciding which way to turn.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • khrishnabrownkhrishnabrown Posts: 7
    edited 2009-11-09 00:28
    this is as close as i got. Here i created a bit variable called last. By default it starts out as a 0 so it doesn't matter if i set it to zero or not. In this code "last" starts as a 0. then when it sees something it changes to a 1. when it sees something again it turns to 0. But it I make it so it calls the "turn left" ruitine when (last =0) then it is going to keep turning left upon start. which means it's turning counter clockwise continuously. Can someone copy my code and edit it with the correct code?

    I'm not sure if you guys understand what i'm trying to accomplish. The bot will be moving forward...when it sees something, it turns right and continue moving forward... when it sees something for a second time it turns left and continue mocing forward... and it keeps alternating between the two.

    my code so far:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    irdetect VAR Bit
    pulse VAR Byte
    last VAR Bit
    last = 0


    DO
    GOSUB forward
    GOSUB move
    DEBUG DEC last
    LOOP


    move:

    FREQOUT 4, 1, 38500

    irdetect = IN6

    IF (irdetect = 0) THEN
    GOSUB right
    last = last + 1
    ENDIF
    RETURN

    forward:

    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    RETURN

    right:
    FOR pulse = 0 TO 20
    PULSOUT 13, 850
    PULSOUT 12, 850
    PAUSE 20
    NEXT
    RETURN

    left:
    FOR pulse = 0 TO 20
    PULSOUT 12, 650
    PULSOUT 13, 650
    PAUSE 20
    NEXT
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-09 02:04
    Please re-read my message from 11/2
  • khrishnabrownkhrishnabrown Posts: 7
    edited 2009-11-09 03:03
    I did. I'm just not getting it. You said, "If this variable is zero and an object is detected, change the variable to one, call the turn right routine, then continue." thats what i did here:

    IF (irdetect = 0) THEN
    GOSUB right
    last = 1
    ENDIF
    RETURN

    should i keep the RETURN or should I do this:

    IF (irdetect = 0) THEN
    GOSUB right
    last = 1
    ELSE
    GOSUB forward
    ENDIF

    Now what should i do after i do that? you said " If this variable is a one, change the variable to zero, call the turn left routine, then continue." What variable are you talking about? my "last" variable? It got changed to a 1 and returned to the loop. Now what?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-09 04:07
    "If this variable is a one, change the variable to zero, call the turn left routine, then continue" means:
    IF last = 1 THEN
       last = 0
       GOSUB left
    ENDIF
    


    This block of code goes between the "IF (irdetect = 0) THEN" and the matching "ENDIF"

    The other case (variable is zero) is done the same way.

    The BoeBot moves forward, checks for an obstacle, and goes either left or right on alternate attempts.
  • khrishnabrownkhrishnabrown Posts: 7
    edited 2009-11-11 17:51
    I got it!
Sign In or Register to comment.