Lesson 3: Adding the Down Eye Sensor
In the previous Lesson, the Distance Sensor was used to determine if the VR Robot detected an object or not, and if so, drove forward toward the object. However, no sensor values were used to determine if the VR Robot was close to the edge of the Playground to stop it. Stopping the VR Robot when it was close to the edge of the Playground would prevent it from driving off of the side.
As is, the project repeats the behaviors of driving forward and turning, forever. However, there is no condition to check for the red border around the Playground. Thus, the VR Robot can fall off the Playground when driving forward.
Adding the Down Eye Sensor
Another sensor is needed for the VR Robot to be able to determine if it is close to the edge of the Playground or not. The Down Eye Sensor can be used to determine if the VR Robot is close to the edge of the Playground by detecting the red border around the Playground.
The behaviors of driving forward and turning only need to repeat until the Down Eye Sensor reports that the red border is detected. Thus, the infinite while loop can be replaced with a while loop with a not condition, in order to repeat the behaviors of driving forward and turning until the condition of detecting the red border is met.
-
Load the Unit9Lesson2 project from the previous Lesson.
def main(): while True: # Does the Distance Sensor detect a castle? if front_distance.found_object(): # Crash castle detected by Distance Sensor drivetrain.drive(FORWARD) else: # Turn to find a castle using the Distance Sensor drivetrain.turn(RIGHT) wait(5, MSEC)
- Rename the project Unit9Lesson3.
-
Add a comment to note how the while loop is going to be used with the Down Eye Sensor. Your project should now look like this:
def main(): # Look for building while the red border is not detected while True: # Does the Distance Sensor detect a castle? if front_distance.found_object(): # Crash castle detected by Distance Sensor drivetrain.drive(FORWARD) else: # Turn to find a castle using the Distance Sensor drivetrain.turn(RIGHT) wait(5, MSEC)
-
A command is needed to continuously check the Down Eye Sensor. The VR Robot will need to repeat these behaviors while the Down Eye Sensor does not detect the color red. Change the while loop condition by putting the not keyword in front of the condition. Your project should now look like this:
def main(): # Look for building while the red border is not detected while not True: # Does the Distance Sensor detect a castle? if front_distance.found_object(): # Crash castle detected by Distance Sensor drivetrain.drive(FORWARD) else: # Turn to find a castle using the Distance Sensor drivetrain.turn(RIGHT) wait(5, MSEC)
-
Then, change the condition from True to the down_eye.detect command. Set the parameter to 'RED'. Your project should now look like this:
def main(): # Look for building while the red border is not detected while not down_eye.detect(RED): # Does the Distance Sensor detect a castle? if front_distance.found_object(): # Crash castle detected by Distance Sensor drivetrain.drive(FORWARD) else: # Turn to find a castle using the Distance Sensor drivetrain.turn(RIGHT) wait(5, MSEC)
- Open the Dynamic Castle Crasher Playground if it is not already open, and run the project. Does the VR Robot behave as you intended it to?
-
When this project is run, the VR Robot will drive toward a detected object, and turn right otherwise. However, even though the Down Eye Sensor is looking for the red border, the VR Robot is not told what to do once it detects red, so it continues driving and eventually falls off of the Playground.
Once the condition of the while loop is met, the project flow will execute outside of the while loop.
Select the Next button to continue with the rest of this Lesson.