Lesson 2: Using the Front Eye Sensor with While Loops
In this Lesson, you will create a project using the Front Eye Sensor with a while loop and not condition to navigate the VR Robot through the Disk Maze Playground. Your project will instruct the VR Robot to turn right when a green disk is detected by the Front Eye Sensor, and left when a blue disk is detected. In the mini challenge, you will apply these skills to drive the VR Robot to each disk in the Disk Maze Playground, and finish at the red disk.
Learning Outcomes
- Identify that the while loop can be used with the Eye Sensors.
- Identify that in order to use Eye Sensor data to make the VR Robot make a decision in a project, a condition must be used.
- Describe how identifying a pattern in a project can be used to simplify your code.
Navigating to the First Four Disks
The Front Eye Sensor can be used to make the VR Robot detect disks and their colors, and to make decisions based on that data. The VR Robot can then navigate the Disk Maze using sensor feedback related to the colors of the objects that are detected, to determine the robot's movement. Essentially, the Front Eye Sensor data will be used to 'color code' the VR Robot's turns, so when one color is detected it turns left, and when another is detected it turns right.
To begin, let's look at the path the VR Robot will travel to navigate the first portion of the Disk Maze.
For Your Information
The Disk Maze Playground contains colored markings on the floor of the Playground at each of the raised colored disk locations.
We will use the Front Eye Sensor to detect the disks in this Unit, but the same logic and project could be used with the Down Eye Sensor as well. Instead of detecting the raised disks, the Down Eye Sensor would detect the colors on the floor of the Playground, and make decisions with that data. The Down Eye Sensor does not detect the floor of the Playground as an object, but it will detect the colored spaces.
Using a While Loop with Eye Sensor Data
In previous Units, the while loop was used to make the VR Robot drive while a condition was met, or a while loop with a not condition to drive while a condition was not met. Data from the Eye Sensors can be used in the same way. Let's look at the first step in the Disk Maze path.
The VR Robot should drive forward while the Front Eye Sensor does not detect green. When the Front Eye Sensor does detect green, the VR Robot should turn right 90 degrees to face the next disk in the maze.
Name and Save the Project
Let's begin building the project to use the while loop to instruct the VR Robot to turn when the Front Eye Sensor detects the first (green) colored Disk on the Disk Maze Playground.
- Start a new project and select the Disk Maze Playground when prompted.
-
Name the project Unit7Lesson2.
- To begin, add a comment to describe the VR Robot's behavior. This project will use comments to remind the user what the intention is for the behaviors of the VR Robot during the following section of the project. Remember that comments in VEXcode VR Python start with a # (pound) symbol.
def main():
# Drive to 1st disk (green), turn right
- The VR Robot will need to drive toward the first disk on the Disk Maze Playground. Drag in or type a while loop with a not condition in the workspace. Remember to include the wait command as part of the while loop, to ensure that the VR Robot executes the project correctly.
def main():
# Drive to 1st disk (green), turn right
while not condition:
wait(5, MSEC)
- Set the condition of the loop to the Front Eye Sensor's detect command, and set the parameter to "GREEN."
def main():
# Drive to 1st disk (green), turn right
while not front_eye.detect(GREEN):
wait(5, MSEC)
- Inside the while loop, drag or type the non-waiting drive command so your project looks like this:
def main():
# Drive to 1st disk (green), turn right
while not front_eye.detect(GREEN):
drivetrain.drive(FORWARD)
wait(5, MSEC)
- Outside the while loop, drag or type a turn_for command to instruct the VR Robot to turn right 90 degrees when it detects a green disk. Your project should look like this:
def main():
# Drive to 1st disk (green), turn right
while not front_eye.detect(GREEN):
drivetrain.drive(FORWARD)
wait(5, MSEC)
drivetrain.turn_for(RIGHT, 90, DEGREES)
- Open the Playground Window if it is not already open. Be sure the Disk Maze Playground opens, and run the project.
-
Watch the VR Robot drive forward to the first green disk then turn right.
- Notice that the VR Robot drives forward while the condition of the Front Eye Sensor color reports False. Once the green disk is detected, the Front Eye Sensor color reports True and exits the loop. Then, it executes the next command, which is to turn right for 90 degrees.
Select the Next button to continue with the rest of this Lesson.