Skip to main content

Lesson 2: Using Multiple While Loops

Continuing through the Wall Maze

Now we've used a while loop with the Bumper Sensor to make the VR Robot stop driving when it reaches the wall. However, to continue through the Wall Maze, the VR Robot needs to turn when it reaches a wall, so that it can change direction and continue on. 

  • To begin, remove the stop command outside the while loop, and replace it with a turn_for command. Set the parameters to have the VR Robot turn left for 90 degrees. 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)
  • Launch the Wall Maze Playground if it is not already open and run the project.
  • Watch the VR Robot drive from the start of the Wall Maze, stop when the Bumper Sensor is pressed by the wall, and turn left 90 degrees.

    A top down view of the Wall Maze Playground. The VR Robot has moved forward from the green starting arrow at the bottom of the playground until it hit a wall in front of it. It is now facing 90 degrees to the left, so it's facing the wall to the left of it.

Multiple Loops and the Wall Maze Problem

Now the VR Robot has reached a wall, and turned so that it can continue driving through the maze. The same way the Bumper Sensor data was used in a while loop for this first step in the maze, the same structure can be used several times in the same project to make the VR Robot continue through the maze. Multiple while loops can be used in the same project.

  • Starting from the current position of the VR Robot, the next step in solving the Wall Maze would be to drive forward to the next wall and turn right 90 degrees. The existing commands can be copied and pasted below the previous while loop and turn_for command. Remember to be mindful of indentation, and to change the new turn_for command parameters to turn right 90 degrees. 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)
  • Reset the Wall Maze Playground and run the project again.
  • Watch the VR Robot drive from the start of the Wall Maze, stop when the Bumper Sensor is pressed by the wall, turn left 90 degrees, drive forward until the Bumper Sensor is again pressed by a wall, then turn right 90 degrees and stop.

    A top down view of the Wall Maze Playground. The VR Robot has moved forward from the green starting arrow until it has gone forward, turned left, and gone forward again until it hit the next wall. It is now facing 90 degrees to the right after its movement, which has it facing the top wall.

Mini Challenge

For this mini challenge, create a project where the VR Robot drives from start to the letter ‘A’ on the Wall Maze Playground using multiple while loops and the Bumper Sensor!

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.

Follow these steps to complete the mini challenge:

  • Watch the solution video below to see how the VR Robot should drive in order to complete the mini challenge. In this video clip, the VR Robot begins at the start in the bottom center of the Playground. It drives forward until the Bumper Sensor is pressed by the wall opposite, then turns left, and drives to the next wall in front of it. When the Bumper is pressed at that wall, the robot turns left again and drives forward until it reaches the final wall and the letter A.

  • Create a project by adding or removing the necessary commands to the Unit4Lesson2 project.
  • Start the project to test it.
  • If the project is not successful, edit and try again. Continue to revise and run the project until the VR Robot successfully drives from start to the letter ‘A.’
  • Save the project once the VR Robot successfully drives from start to the letter ‘A.’

Congratulations! You solved the mini challenge!

Questions

Please select a link below to access the lesson quiz.

Google Doc / .docx / .pdf