Skip to main content

Lesson 4: Project Flow with Nested Loops

Collect the next two disks

  • Now that the VR Robot is facing the red disks, the VR Robot will need to repeat the same behaviors that were used to collect the blue disk, in order to pick up and move the red disk. The behaviors of driving forward to pick up the disk, turn around, and drive back to the same colored goal to drop off the disk.

    Top down view of the Disk Mover Playground with dotted arrows indicating the path of the VR Robot from the red goal to collect the red disk and drop it in the goal, then pointing to the right to move to the green goal.
  • Once the first red disk is moved, the VR Robot will then need to drive to the green goal and perform the same actions that were used to move the blue and red disk, to move the first green disk.

    Top down view of the Disk Mover Playground at the start, with dotted arrows indicating the path the robot needs to move to collect each Disk. Arrows point to and from each goal to the first disk, and additional right arrows point between each of the goal areas.
  • In order to repeat behaviors multiple times, a for loop can be used.
    VEXcode VR Python for loop command within the main definition. The command reads for value in range with a 10 in parentheses colon on the first line, then indented beneath a wait command reads wait with 5, MSEC in parentheses.
  • Add a for loop to the top of the project, above the first comment, and set the range function parameter to ‘3’. Be sure the for loop is indented within the main definition.

    def main():
        for repeat_count in range(3):
            wait(5, MSEC)
  • To copy the code into the for loop, careful attention will need to paid to the indentation. First, add space before the wait command, so the loop looks like this:

    def main():
        for repeat_count in range(3):
    
    
            
            wait(5, MSEC)
  • Next, select the commands to put into the for loop, and copy and paste them above the wait command. Notice that the indentation will be incorrect after simply copying and pasting. The gray indicator line of the for loop is broken, and disconnected from the wait command. This is a visual cue that your project will not execute as intended.The project from above, copy pasted into the for loop, with incorrect indentation. Correct indentation is shown in the next code block.
  • Select the commands inside the for loop, and use 'Tab' to correctly indent the commands within the loop. When correctly indented, the gray indicator line should connect the for loop with the wait  command to show the bounds of the outer loop. When complete, your project should look like this: 

    def main():
        for repeat_count in range(3):
            # 1. Drive to disk using Down Eye Sensor
            while not down_eye.near_object():
                drivetrain.drive(FORWARD)
                wait(5, MSEC)
            drivetrain.stop()
    
            # 2. Pick up disk
            magnet.energize(BOOST)
    
            # 3. Drive to goal using Distance Sensor
            drivetrain.turn_to_heading(180, DEGREES)
            while front_distance.get_distance(MM) > 200:
                drivetrain.drive(FORWARD)
                wait(5, MSEC)
            drivetrain. stop()
    
            # 4. Drop disk in goal
            magnet.energize(DROP)
            drivetrain.drive_for(REVERSE, 100, MM)
    
            # 5. Turn and drive to next goal
            drivetrain.turn_to_heading(90, DEGREES)
            drivetrain.drive_for(FORWARD, 800, MM)
    
            # 6. Turn to disks
            drivetrain.turn_to_heading(0, DEGREES)  
    
        wait(5, MSEC)
  • Watch the video below to see how to move the commands and adjust indentation as described above. In the video clip, the six comment sections of the project are highlighted, and the Context Menu is opened. The ‘Cut’ option is selected, and then the cursor is placed below the for loop command. The Context Menu is opened again, and ‘Paste’ is selected. The commands are then within the for loop, but incorrectly indented. The six comment sections are highlighted again and ‘Tab’ on the keyboard is pressed to correctly indent the project within the loop.
Video file
  • Open the Disk Mover Playground if it is not already open, and run the project.
  • When this project is run, the VR Robot picks up and drops one disk of each color into its corresponding colored goal.

    A top down view of the VR Robot at the end of the project, with one of each color disk in the matching goal, and the VR Robot facing the right side wall at the edge of the green goal in the lower right corner.

For Your Information

Note that the VR Robot continued to drive into the wall. This behavior is expected since the project instructed the VR Robot to turn right and drive toward the next goal, three times. The first time is driving from blue to red. The second is from red to green, and the third is from green into the wall, since there is not another colored goal. Stop the project once the VR Robot hits the wall. The project could be improved so that the VR Robot stops after it picks up and moves the green disk, but this would increase the overall complexity of the project.

A top down view of the VR Robot at the end of the project, with the Stop button in the Playground window highlighted in the left hand corner. On the Playground, one of each color disk in the matching goal, and the VR Robot facing the right side wall in the center of the green goal, when the stop button was pressed.

 

 

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf