Lesson 3: When Color Red Detected
There are no commands to tell the VR Robot what to do once it detects the color red. In order to avoid the red border, the VR Robot should reverse and turn once it detects the color red, like it does in this video.
-
Add a comment to the end of the while loop to indicate what should happen when the Down Eye Sensor detects the red border. 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) # Reverse and turn when red border detected
-
Add a drive_for and turn_for commands beneath the comment to allow the VR Robot to reverse and turn once the Down Eye Sensor detects the color red.
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) # Reverse and turn when red border detected drivetrain.drive_for(REVERSE, 300, MM) drivetrain.turn_for(RIGHT, 90, DEGREES)
- 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, there are two conditions that are checked. The first is the condition of the Down Eye Sensor detecting the color red inside of the while loop. Note that the project flow moves onto the next condition of an object being detected ONLY if the Down Eye Sensor does not see the color red. If the Down Eye Sensor does detect red, the project flow skips the if else statement and jumps to driving in reverse and turning.
- The second condition that is checked is the if else statement. While the Down Eye Sensor does not detect red, the project will continue in the while loop to check the condition of the Distance Sensor. The VR Robot will use the data from the Distance Sensor to make a decision to drive forward or turn right.
-
However, once the Down Eye Sensor detects the color red, the VR Robot will drive in reverse, turn, then stop since that is the end of the project flow. There is no loop used to repeat the behaviors again.
Adding an infinite while loop
-
An infinite while loop is needed in order for the behaviors to repeat on a loop. Drag in or type a while loop at the top of the project, and set the condition to True to create an infinite loop. Then, copy and paste the previous code inside the infinite while loop. Ensure that the commands are properly indented under the correct headers, otherwise the project may not run as expected. Your project should now look like this:
def main(): while True: # 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) # Reverse and turn when red border detected drivetrain.drive_for(REVERSE, 300, MM) drivetrain.turn_for(RIGHT, 90, DEGREES)
- Open the Dynamic Castle Crasher Playground if it is not already open, and run the project. Does the VR Robot behave as you intended?
-
When this project is run, the VR Robot will drive toward a detected castle and turn right if no castles are reported. Once the Down Eye Sensor detects the color red, the VR Robot drives in reverse, turns, and continues driving toward another castle.