Skip to main content

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.

Video file
  • 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.
    This lesson's Python project code with a diagram showing the project's flow on the side. It reads First condition checked: Down Eye Sensor detects red in orange, representing the start of the loop. There is a Red arrow loop for when the Down Eye Sensor detects red, which will report True and the Project will exit the repeating look. If it reports false, the code will move to the if then statement and continue to repeat until the Down Eye Sensor detects red.
 
  • 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. This lesson's Python project code with a diagram showing the project's flow on the side. It reads Second condition checked: Distance Sensor detects object. It shows what happens if the second condition is checked, and if it reports true, the VR Robot will drive forward, and if the second condition reports false, the VR Robot turns 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.

    A top down view of the Dynamic Castle Crashed playground showing some of the castles being toppled over, with the VR robot having reversed and turned around after it passed the red border, so it faces the center of the playground 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.

    A top down view of the Dynamic Castle Crasher playground showing that the VR Robot has toppled over multiple castles in the playground and is now moving towards the last standing castle.

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf