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. 
    code to turn left after bumper is pressed
  • 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.
    Wall Maze

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.
    code to turn left and right after bumper is pressed
  • 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.
    turn right vr

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!

Letter A Callout

Follow these steps to complete the mini challenge:

  • Watch the solution video to see how the VR Robot should drive in order to complete the mini challenge.

  • 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 the link below to access the lesson quiz.

Open Lesson Quiz   >