Help with nested loops
Carol Hazlett
Posts: 312
I have built a classic 6 legged walker using three servos. All my code works well except for one thing. I need the walker to execute a turn sequence after doing the backing up sequence. What is happening is the robot does the back up and then jumps back to the main program without doing the turn, I need to add another loop to grt it to execute both behaviours before going back to the main program. I am not sure what type of loop and what structure to use to accomplish this. I am putting a copy of one of the subroutine here so you can see one of the ways I tried. I have already tried doing another do....loop around the whole routine but that would not go back to the main program, it just kept repeating itself. I thought of a for.....next loop, but am not sure how to do that and only get it to loop once. As you can see from this code snippet there are already several nested loops in the code. All help and suggestions would be greatly appreciated.
Back_Up_Around: ' Back up and turn around 180 degrees.
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' left side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PULSOUT 12, 500 ' Right legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down
PULSOUT 13, 300 ' left legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20 ' Left legs swing backwards.
PULSOUT 13, 980
PAUSE 20
NEXT
LOOP UNTIL (reps >= 8)
RETURN
Back_Up_Around: ' Back up and turn around 180 degrees.
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' left side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PULSOUT 12, 500 ' Right legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down
PULSOUT 13, 300 ' left legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20 ' Left legs swing backwards.
PULSOUT 13, 980
PAUSE 20
NEXT
LOOP UNTIL (reps >= 8)
RETURN
Comments
' {$STAMP BS2}
' {$PBASIC 2.5}
' Program for Minds-i Basic Walker
' Built and programmed by Carol Lynn Hazlett
' August 7, 2012
'
[ Variables ]
counter VAR Byte ' FOR...NEXT loop counter.
reps VAR Nib ' Holds number for timing loops.
'
[ Initialization ]
FREQOUT 2, 2000, 3000 ' Signal program start/reset.
'
[ Main Routine ]
Main:
DO
IF (IN9 = 0) AND (IN10 = 0) THEN ' Both whiskers detect obstacle
HIGH 1 ' Turn on right LED.
HIGH 11 ' Turn on left LED.
GOSUB Back_Up_Around ' Back up & turn around
ELSEIF (IN10 = 0) THEN ' Left whisker contacts
HIGH 11 ' Turn on left LED.
GOSUB Back_Up_Right ' Back up & turn right.
ELSEIF (IN9 = 0) THEN ' Right whisker contacts
HIGH 1 ' Turn on right LED.
GOSUB Back_Up_Left ' Back up & turn left.
ELSE ' Both whiskers 1, no contacts
LOW 11 ' Turn off left LED.
LOW 1 ' Turn off right LED.
GOSUB Forward_Walk ' Walk forward when nothing is touching
ENDIF ' whiskers.
LOOP
'
[ Subroutines ]
Forward_Walk: ' Walk forward when nothing is in way.
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 810 ' Left side of middle leg is down
PULSOUT 13, 300 ' Left legs swing forwards.
PULSOUT 12, 500 ' right legs swing backwards.
PAUSE 20
NEXT
RETURN
Back_Up_Around: ' Back up and turn around 180 degrees.
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' left side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PULSOUT 12, 500 ' Right legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down
PULSOUT 13, 300 ' left legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20 ' Left legs swing backwards.
PULSOUT 13, 980
PAUSE 20
NEXT
LOOP UNTIL (reps >= 8)
RETURN
Back_Up_Right: ' Back up and turn right.
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' left side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PULSOUT 12, 500 ' Right legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 13, 980 ' Left legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
RETURN
Back_Up_Left: ' Back and turn left.
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' left side of middle leg is down.
PULSOUT 13, 980 ' Left legs swing backwards.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 14, 645 ' Right side of middle leg is down.
PULSOUT 13, 300 ' Left legs swing forward.
PULSOUT 12, 500 ' Right legs swing backwards.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
DO
reps = reps + 1
FOR counter=1 TO 20
PULSOUT 14, 810 ' Left side middle leg down.
PULSOUT 12, 1000 ' Right legs swing forward.
PAUSE 20
NEXT
FOR counter=1 TO 20
PULSOUT 12, 500 ' right legs swing back.
PAUSE 20
NEXT
LOOP UNTIL (reps >= 5)
RETURN
Here's an example of using nested FOR/NEXT loops. Notice how indenting helps keep straight what sections of the program are doing. Look at this thread on posting code ..