Lesson 2: Drive to Green then Blue Disks
Drive to Green then Blue Disks
- The previous project has the VR Robot turn right when the Front Eye Sensor detects a green disk. Now, build a project where the VR Robot drives to four disks on the Disk Maze Playground.
-
The goal is to create a consistent pattern where the VR Robot turns right when the Front Eye Sensor detects a green disk, and left when the Front Eye Sensor detects a blue disk.
-
Add the following block to the existing project to instruct the VR Robot to drive forward after turning right.
-
Add another [Wait until] with the <Color sensing> block set to detect ‘blue,’ since the next disk that the VR Robot will encounter is blue.
-
Add a [Turn for] block and set the parameter to 'left.'
-
Run the project to see how the VR Robot moves through the Disk Maze Playground.
- Notice that the VR Robot turns right when the Front Eye Sensor detects a green disk, and turns left when the Front Eye Sensor detects a blue disk.
-
Continue to build this project to drive the VR Robot to two more disks on the Disk Maze Playground using the Front Eye Sensor. Add the following blocks to navigate the VR Robot to the next two Disks then stop:
-
Notice that there is a pattern. There is a series of blocks that are repeated three times in the project. This section of code can be condensed by using a [Repeat] block.
- Open the Disk Maze Playground if it is not already open, and run the project.
-
Watch the VR Robot navigate to four disks on the Disk Maze Playground.
- In this project, the VR Robot drives forward until the Front Eye Sensor detects the color green.
- The VR Robot turns right 90 degrees and drives forward again until the color blue is detected.
- The VR Robot then turns left and drives forward until the Front Eye Sensor detects blue. Once the color blue is detected, the VR Robot turns left again.
- Finally, the VR Robot drives forward until the Front Eye Sensor detects blue, then stops.
- Notice the pattern that when the Front Eye Sensor detects green, the VR Robot turns right. When the Front Eye Sensor detects blue, the VR Robot turns left in this project.
Using Switch Blocks
So far, you have created a project to drive to the green disk and then the three blue Disks.
The image below shows the entire project using VEXcode blocks, with the repeated behaviors used to drive to the three blue Disks boxed in red.
The project below shows these same VEXcode blocks converted into multiple Switch blocks.
This last project shows the same Switch block commands converted into Python commands within one Switch block.
The for repeat_count in range(3): is the Python command that tells the robot that the next indented lines of code should be repeated three times. Note that you should include a colon (:) after the for loop.
The subsequent lines of code are the behaviors that will be repeated. Note that these behaviors are indented at a default of 4 spaces.
Additionally, note that the conditional includes the command, wait (5, MSEC). In VEXcode VR, a wait command is always added with the for loop. The purpose of the wait command is to ensure that VEXcode VR can properly run the project as intended, due to the web-based nature of the VEXcode VR platform. The wait command should never be deleted when using a for loop, or your project may not run as intended.
The line, wait (5, MSEC) is indented beneath the while not front_eye.detect(BLUE) command because the Front Eye Sensor will check for the color blue, pausing 5 MSEC between each check.
Select the Next button to continue with the rest of this Lesson.