Skip to main content

Lesson 3: Project Flow with Multiple While Loops

Now that you have created a text project to navigate the VR Robot to the letter ‘A’ on the Wall Maze Playground, you will expand your skills using conditionals and sensor values. In this Lesson, you will navigate the VR Robot to the number ‘2’ in the Wall Maze Playground, then explore why additional commands are needed to drive to the letter ‘B.’ In the mini-challenge, you will apply these skills to navigate from the beginning of the Wall Maze to the number ‘3.’

A top down view of the Wall Maze playground with the VR Robot starting on the green arrow at the bottom center of the playground. The number 2 is highlighted in a red box, and is positioned in the center of the lower left quadrant of the maze, further left beyond the letter A. The number 2 in a corner created by three walls surrounding it.

Learning Outcomes

  • Describe the program flow in a project that has a VR Robot drive forward until the Bumper Sensor is pressed.

Rename the Project

  • If the previous Lesson’s project is not already loaded, load the Unit4Lesson2 project.
  • Rename the project Unit4Lesson3.The VEXcode VR toolbar showing Unit 4 Lesson 3 as the project name. The project name is highlighted with a red box.

     

 

Flow of the Project to Drive to the Letter ‘A’

In the previous lesson, the VR Robot drove to the letter ‘A’ on the Wall Maze Playground. Let's break down the project flow as we prepare to navigate to a new location in the Wall Maze.

A top down view of the Wall Maze Playground with the VR Robot in the starting location in the center bottom of the Playground. The letter A is directly to the left of the robot, separated by a short wall.
  • To better understand the project flow for driving to the letter ‘A’ on the Wall Maze Playground, begin by modifying your previous project or create a new project to match this base project.

    def main():
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    	
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.stop()
  • When a project uses a while loop with a not condition, the program flow of the project still follows the same top-to-bottom sequence. The while loop allows the program flow to "pause" at a certain point, while a condition is met. The not operator allows the project to stay in the loop until the condition is reported False. Then the project will flow to the next command outside of the loop. A Boolean condition, such as the Left Bumper being pressed, will report a True or False value. In this code for driving the VR Robot to the letter ‘A,’ the VR Robot will drive forward until the Boolean condition of the Left Bumper being pressed, is True.
    The project flow of the project to drive to the Letter 'A' would look like this:A project flow diagram describing the flow of the project above to navigate to the letter A, which is shown on the left. To the right, arrows indicate how the project flows. First, a red cyclical arrow illustrates that while the bumper is not pressed, the condition of the while loop returns false, so the robot drives forward. A green downward arrow next illustrates that when the bumper is pressed, the condition reports true so the project breaks out of that loop and the robot turns left 90 degrees. This sequence is repeated for each loop in the project.

The behavior of the VR Robot shows how each while loop is being executed. Broken down alongside the movement of the VR Robot, we can clearly see where the VR Robot has driven to after each while loop in the project was executed.On the left shows Python code that contains three sequential while loops, each instructing the robot to drive forward until the left bumper is pressed. Once the bumper is pressed in each loop, the robot performs a specific action.  In the first loop, the robot drives forward until the bumper is pressed, then turns left by 90 degrees. The image to the right shows the robot's position in a maze after this action, marked by a yellow circle.  In the second loop, the robot again drives forward until the bumper is pressed and then turns left by 90 degrees. The second image shows the robot’s updated position in the maze after this turn.  In the third and final loop, the robot drives forward until the bumper is pressed. When the bumper is pressed, the robot stops moving. The final image shows the robot's stopping position within the maze, again highlighted by a yellow circle.

 

Drive to the Number '2'

In this Lesson, the project will be edited to have the VR Robot drive to the number ‘2’ on the Wall Maze Playground!

A top down view of the Wall Maze playground with the VR Robot starting on the green arrow at the bottom center of the playground. The number 2 is highlighted in a red box, and is positioned in the center of the lower left quadrant of the maze, further left beyond the letter A. The number 2 in a corner created by three walls surrounding it.
  • The project from Lesson 2 has the VR Robot turning left to drive to the letter ‘A.’ In order to navigate to the number ‘2,’ the VR Robot will have to turn right instead, drive forward, and then turn left.

    A top down view of the Wall Maze Playground where the VR Robot is positioned above the A marker, facing the left wall. A red arrow points towards A, and three green arrows show the intended path to number 2. The green arrows point upwards toward the wall opposite A, then to the left toward the next wall to the left of number 2, then downward into the corner holding number 2.
  • Change the direction of the second turn_for command to right instead of left. Your project should now look like this:

    def main():
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    	
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(RIGHT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.stop()
  • Launch the Wall Maze Playground if it is not already open and run the project.
  • Watch the VR Robot begin to drive to the number ‘2,’ but stop at a wall. Thus, the VR Robot needs to turn left once it presses against the wall instead of stopping.

    A top down view of the Wall Maze playground with the VR robot facing the wall opposite the letter A, pointing upwards.
  • Select the “Reset” button to reset the Playground and move the VR Robot back to the starting position.The Wall Maze Playground showing the entire playground interface. The reset button in the bottom left of the playground interface is highlighted with a red box.

     

  • Edit the code by removing the stop command and replacing it with the following commands in order to drive the VR Robot to the number ‘2.’

    def main():
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    	
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(RIGHT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    	
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.turn_for(LEFT, 90, DEGREES)
    
    	while not left_bumper.pressed():
    		drivetrain.drive(FORWARD)
    		wait(5, MSEC)
    	
    	drivetrain.stop()
  • Launch the Wall Maze Playground if it is not already open and run the project.
  • Watch the VR Robot drive to the number ‘2’ on the Wall Maze Playground!

    A top down view of the Wall Maze playground. The VR Robot is now positioned over the 2 marker, facing the bottom wall with its front pressed against the wall.
  • Select the “Reset” button to reset the Playground and move the VR Robot back to the starting position.

Select the Next button to continue with the rest of this Lesson.