Problems in pbasic. Simple boe-bot roaming routine fail
I'm trying to make my boe-bot go forward 5 seconds, do a 135, 5 seconds straight again and 135 in opposite direction from previous, unless the IR sensors detect something. The last routine was just to roam straight if the sensors were not detecting, and that worked fine. Now the sensors will not detect while the "else" part of the loop is being carried out. I have tried a million different things to fix it, but none of them worked, so i'm just going to post the basic code of what needs to happen, the best that I can understand it right now.
I tried making the main part inside the DO LOOP the routines, and then added the checks for the sensors in the actual routines, but that wasn't working either. Thanks in advance
' {$STAMP BS2}
' {$PBASIC 2.5}
irdetectleft VAR Bit
irdetectright VAR Bit
pulsecount VAR Byte
FREQOUT 5,2000,3000
DO
FREQOUT 8,1,45500
irdetectleft=IN9
FREQOUT 2,1,45500
irdetectright=IN0
IF(irdetectleft=0) AND (irdetectright=0) THEN
GOSUB back_up
GOSUB turn_left
GOSUB turn_left
ELSEIF (irdetectleft=0) THEN
GOSUB back_up
GOSUB turn_right
ELSEIF (irdetectright=0) THEN
GOSUB back_up
GOSUB turn_left
ELSE
GOSUB forward_pulse
GOSUB onethirtyfive
GOSUB forward_pulse
GOSUB onethirtyfive2
ENDIF
LOOP
forward_pulse:
FOR pulsecount=0 TO 200
PULSOUT 13,650
PULSOUT 12,850
PAUSE 20
NEXT
RETURN
turn_left:
FOR pulsecount=0 TO 20
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
turn_right:
FOR pulsecount=0 TO 20
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
RETURN
back_up:
FOR pulsecount=0 TO 40
PULSOUT 13, 850
PULSOUT 12, 650
PAUSE 20
NEXT
RETURN
onethirtyfive:
FOR pulsecount=0 TO 30
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
onethirtyfive2:
FOR pulsecount=0 TO 30
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
RETURN
I tried making the main part inside the DO LOOP the routines, and then added the checks for the sensors in the actual routines, but that wasn't working either. Thanks in advance

Comments
Welcome to the forums.
The ELSE loop onethirtyfive executes for 30 cycles, taking 30 x 20ms = 600 ms + 30 1.5 ms pulses = 645 ms, more than half of a second. The forward_pulse routine does the same for 200 cycles, so you're committed to almost three seconds of straight driving. This could total to a few feet of travel. If you want to check the sensors more frequently you could reduce the number of loops in this routine. Also look at these loops in term of their time commitment.
Let's see what others have to say, too.
Ken Gracey
Welcome, Asabot. FYI, the president of Parallax just answered your question!
Try putting irdetectleft and irdetectRigh checks in the for...net loops or remove the for...next loops all together, and/or create a method where you pass a value to the for...next which tells the loop how many times to execute.
Essentially you need to move the logic around to get the expected behavior.
Edit: Ken beat me to it.
Thank you. I have only been programming for a year and it's been mainly cc,java, and it's harder for me to see how things are working in pbasic.
Thanks for reply. I believe I did try some variation of what you're saying. I had the loop in forward_pulse go from 0 to 20, and then called it 10 times in the do loop, followed by the 135, 10 more times, and 135 again. That didn't work, so I made the forward_pulse routine actually contain the 135's also, and made the forward movement a series of 10, 0 to 20 for loops, but that didn't work either. I think after Mike's response maybe I can go back and make sense of it. I actually did this same routine with whiskers a few months ago, and I had this same exact problem and was able to figure it out. That program is gone now though, and this time i'm having more trouble than the first :blank:
+rep for parallax
' {$STAMP BS2} ' {$PBASIC 2.5} irdetectleft VAR Bit irdetectright VAR Bit pulsecount VAR Byte FREQOUT 5,2000,3000 DO FREQOUT 8,1,45500 irdetectleft=IN9 FREQOUT 2,1,45500 irdetectright=IN0 GOSUB forward_pulse GOSUB onethirtyfive GOSUB forward_pulse GOSUB onethirtyfive2 LOOP ' FORWARD PULSE I HAVE BEEN USING 200 AS MY INITIAL FORWARD VALUE FOR TESTING forward_pulse: IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '20 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '40 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '60 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '80 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 ' 100 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '120 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '140 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '160 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '180 PULSOUT 12,850 PAUSE 20 NEXT ENDIF IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE FOR pulsecount=0 TO 20 PULSOUT 13,650 '200 PULSOUT 12,850 PAUSE 20 NEXT ENDIF 'TURN LEFT turn_left: FOR pulsecount=0 TO 20 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN turn_right: FOR pulsecount=0 TO 20 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURN back_up: FOR pulsecount=0 TO 40 PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20 NEXT RETURN onethirtyfive: FOR pulsecount=0 TO 30 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN onethirtyfive2: FOR pulsecount=0 TO 30 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURN' {$STAMP BS2} ' {$PBASIC 2.5} irdetectleft VAR Bit irdetectright VAR Bit pulsecount VAR Byte FREQOUT 5,2000,3000 DO FREQOUT 8,1,45500 irdetectleft=IN9 FREQOUT 2,1,45500 irdetectright=IN0 GOSUB forward_pulse GOSUB onethirtyfive GOSUB forward_pulse GOSUB onethirtyfive2 LOOP ' FORWARD PULSE I HAVE BEEN USING 200 AS MY INITIAL FORWARD VALUE FOR TESTING forward_pulse: FOR pulsecount=0 TO 200 IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE PULSOUT 13,650 '20 PULSOUT 12,850 PAUSE 20 ENDIF NEXT RETURN 'TURN LEFT turn_left: FOR pulsecount=0 TO 20 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN turn_right: FOR pulsecount=0 TO 20 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURN back_up: FOR pulsecount=0 TO 40 PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20 NEXT RETURN onethirtyfive: FOR pulsecount=0 TO 30 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN onethirtyfive2: FOR pulsecount=0 TO 30 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURNThanks
Rob
Here ya go!
The way it is below is actually my first attempt at this behavior (minus the commented lines obviously). All of the ugly code above was desperate attempts at getting it to work..
' {$STAMP BS2} ' {$PBASIC 2.5} irdetectleft VAR Bit irdetectright VAR Bit pulsecount VAR Byte FREQOUT 5,2000,3000 DO FREQOUT 8,1,45500 irdetectleft=IN9 FREQOUT 2,1,45500 irdetectright=IN0 GOSUB forward_pulse GOSUB onethirtyfive GOSUB forward_pulse GOSUB onethirtyfive2 LOOP forward_pulse: FOR pulsecount=0 TO 200 FREQOUT 8,1,45500 ' irdetectleft=IN9 ' these lines were my whole problem. hours of messing with loops FREQOUT 2,1,45500 ' because of not initializing my sensors inside the routine. irdetectright=IN0 ' IF(irdetectleft=0) AND (irdetectright=0) THEN GOSUB back_up GOSUB turn_left GOSUB turn_left PAUSE 100 ELSEIF (irdetectleft=0) THEN GOSUB back_up GOSUB turn_right PAUSE 100 ELSEIF (irdetectright=0) THEN GOSUB back_up GOSUB turn_left PAUSE 100 ELSE PULSOUT 13,650 PULSOUT 12,850 PAUSE 20 ENDIF NEXT RETURN turn_left: FOR pulsecount=0 TO 20 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN turn_right: FOR pulsecount=0 TO 20 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURN back_up: FOR pulsecount=0 TO 40 PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20 NEXT RETURN onethirtyfive: FOR pulsecount=0 TO 30 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT RETURN onethirtyfive2: FOR pulsecount=0 TO 30 PULSOUT 13, 650 PULSOUT 12, 650 PAUSE 20 NEXT RETURN