PDA

View Full Version : how do i?



khrishnabrown
11-03-2009, 01:24 PM
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?

Mike Green
11-03-2009, 01:33 PM
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.

khrishnabrown
11-07-2009, 02:26 PM
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

W9GFO
11-07-2009, 03:08 PM
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. (http://www.gadgetgangster.com/206)

khrishnabrown
11-09-2009, 08:28 AM
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 Green
11-09-2009, 10:04 AM
Please re-read my message from 11/2

khrishnabrown
11-09-2009, 11:03 AM
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 Green
11-09-2009, 12:07 PM
"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.

khrishnabrown
11-12-2009, 01:51 AM
I got it!