Skip to main content

Lesson 4: Move a Red and Green Disk

  • Next, the VR Robot will need to drive forward to the next colored goal. Note that the VR Robot will have to drive forward four grid squares, or 800 millimeters (mm) in order to drive to the center of the next colored goal.

    Top down view of the Disk Mover Playground, with the VR Robot positioned at the edge of the blue goal after dropping off the first blue disk, facing the red goal to the right. A dotted arrow points from the front of the VR Robot to the red goal, indicating the next intended movement.
  • Add a [Drive for] block to the code and set the parameter to 800 millimeters (mm).

    The same VEXcode VR project from the previous page, with a comment and Drive for block added to the end of the stack. The two additional blocks read Drive to next goal, then drive forward for 800 mm.
  • The VR Robot will now need to turn to face the colored disks.

    Top down view of the Disk Mover Playground, with the VR Robot positioned red goal area. A curved arrow points from the front of the VR Robot toward the red disks, indicating the necessary turn to face the red Disks.
  • Add a second [Turn to heading] block to the code and set the parameter to zero degrees. This will turn the VR Robot to face the colored disks.

    The same VEXcode VR project, with a Comment block and Turn to heading block added to the end of the stack. These two blocks read Turn to disks, then Turn to heading 0 degrees.
  • Open the Disk Mover Playground and run the project.
  • The VR Robot will pick up and move the first blue disk into the blue goal, and then drive over the red goal. The VR Robot will also turn to face the red disks.

    A top down view of the Disk Mover Playground with the VR positioned at the front edge of the red goal, facing the red 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.

    A top down view of the Disk Mover Playground with the VR positioned at the front edge of the red goal, facing the red disks. Dotted arrows indicate the necessary movements to drive to pick up the first red disk, deliver it to the red goal, then turn and drive toward 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.

    A top down view of the Disk Mover Playground in the starting configuration, with the robot in the blue goal starting location. Dotted arrows show the intended repeated behaviors to have the robot drive to pick up and deliver the first of each disk to the respective goals.
  • In order to repeat behaviors multiple times, a [Repeat] block can be used.

    A VEXcode VR Repeat block from the Toolbox, with the parameter set to the default of 10.
  • Add a [Repeat] block to the code and set the parameter to ‘3.’

    An overview of adding a Repeat block to the existing VEXcode VR project, so that all the blocks needed to pick up and move the first blue disk to the blue goal, turn to drive to the next goal and then turn to face the disk are wrapped within the C of the repeat block. The repeat block parameter is set to 3.
  • Open the Disk Mover Playground 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 Disk Mover Playground showing the final state of the Disks and robot after the project is run. The first of each color disk is in the matching color goal, and the VR Robot is at the Playground wall at the edge of the green goal.
  • 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.

    The Playground window showing the VR Robot within the green goal area at the end of the project, with the Stop button highlighted by a red box indicating stopping the project before the robot runs into the wall.

For Your Information

Placing one loop inside of another loop is called nesting. When you “nest” loops, the outer loop controls the number of times the inner loop executes. There are many different types of loops that can be nested. For example, in the previous code above, the [Repeat until] blocks are nested inside of a [Repeat] block.

The same VEXcode VR project from before, showing how the Repeat until C blocks are nested within the Repeat block, enabling the robot to move three disks successfully.

A [Repeat] block can also be nested inside another [Repeat] block. In the following example, since the outer [Repeat] block is set to ‘3,’ the VR Robot will draw three squares on the Playground.

To the left, a project shows a Repeat block set to 3, with the Repeat block needed to code the VR Robot to draw a square nested within it, illustrating the concept of nested loops. To the right, the Playground window shows the three squares drawn by the robot from the project.

Nested loops are helpful in projects where a VR Robot repeats the same behaviors more than once. Nesting loops organizes and condenses a project. This can be helpful to better understand the context and purpose of the code, as well as making it easier to find errors.

Select the Next button to continue with the rest of this Lesson.