Shop OBEX P1 Docs P2 Docs Learn Events
Help with nested loops — Parallax Forums

Help with nested loops

Carol HazlettCarol Hazlett Posts: 312
edited 2012-08-09 14:32 in General Discussion
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

Comments

  • Carol HazlettCarol Hazlett Posts: 312
    edited 2012-08-09 11:15
    In case you need the whole thing in order to help me.

    ' {$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
  • Mike GreenMike Green Posts: 23,101
    edited 2012-08-09 11:50
    Where do you reinitialize reps? Is there some reason why you're not using a FOR / NEXT loop for reps instead of DO / LOOP?
  • Carol HazlettCarol Hazlett Posts: 312
    edited 2012-08-09 12:30
    I did not know I neede to initialize reps. I am a bit of a tyro at this. Can use a for next....next on top of each other? Would I then have to name the second "counter" variable a different name?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-08-09 12:38
    You always need to initialize variables. By default, they're initialized to zero when the program starts, but that happens only once. What value does reps have when your subroutines are called?

    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 ..

    attachment.php?attachmentid=78421&d=1297987572
    Back_Up_Around: ' Back up and turn around 180 degrees.
    FOR reps=1 to 5
      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
    NEXT
    FOR reps=1 to 5
      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
    NEXT
    RETURN
    
  • Carol HazlettCarol Hazlett Posts: 312
    edited 2012-08-09 12:45
    Thank you, I'll give that a try. My code is indented in the IDE, I truly believe in indenting and commenting code, it just did not cut and paste that way. And thanks for the little posting code critter I am going to click on him now and learn how to post code properly. I am also re-reading everything on variables in the Basic Stamp Reference manual.
  • Carol HazlettCarol Hazlett Posts: 312
    edited 2012-08-09 14:32
    Thank you very much Mike! I changed the code and now everything works perfectly. Even the number of reps you chose was right on. There will be a video of the robot on the Robothon Facebook page. It was a project I did just for fun using leftover parts from a Minds-i (mymindsi.com) kit I have.
Sign In or Register to comment.