Skip to main content

Lesson 2: Drive to the Letter 'A'

Using the Distance Sensor to Avoid Walls 

Now that you have learned about what comparison operators are and how they can be used in a project, you will apply that learning to drive the VR Robot to the letter 'A' in the Wall Maze Playground. This project will use the Distance Sensor to navigate through the Wall Maze Playground without bumping into walls.

Name and Save the Project

  • Start a new text project and select the Wall Maze Playground when prompted. Image of the Wall Maze Playground selection tile
  • Name the project Unit5Lesson2.
    rename project Unit5Lesson2
  • Drag in or type a while loop and remove the Drivetrain command that populates as part of the new Text project template. Notice the command will populate a while loop with a wait command inside. Do not remove the wait command, it will ensure the VR Robot executes the project correctly.

while loop

  • Drag or type the non-waiting drive command inside the loop.
    • Check that the indentation is correct and is aligned with the wait command.

drive forward command in while loop

Add the get_distance function and the greater than operator

  • Drag or type the get_distance command in the while loop's 'condition'. Type the greater than operator and the second operand as '50. while distance in mm greater than 50
  • Add a stop command outside the while loop. A stop command is needed after the while loop because the VR Robot will continue to drive until it is instructed to stop.
    stop command
  • Open the Playground Window if it is not already open, be sure the Wall Maze Playground opens, and run the project.
  • Watch the VR Robot drive forward until the Distance Sensor reports the distance between the VR Robot and the wall is not greater than 50 millimeters (mm).Distance less than 50
  • Reset the Playground to move the VR Robot back to the starting position.

Continue the Project to Drive to the Letter ‘A’

Using the greater than (>) operator with the get_distance command as the condition of the while loop instructs the VR Robot to drive forward as long as the distance between the VR Robot and the wall is greater than 50 millimeters (mm). Once the Distance Sensor reports a distance from the VR Robot to the wall that is not greater than 50 millimeters, the VR Robot will stop driving. To continue on to the letter 'A', the VR Robot needs to turn when it approaches the wall. 

To drive to the letter ‘A’, the VR Robot will need to detect two walls, and turn left when these walls are detected. Then, the VR Robot will stop when it detects the third wall, located behind the letter ‘A.’
How to get to A

  • Replace the stop command with a turn_for command. The turn_for command is needed because once the Distance Sensor is within 50 millimeters (mm) of a wall, the VR Robot will turn left to continue toward the letter ‘A.’
    • Set the parameters of the turn to "LEFT, 90, DEGREES."

    image of the existing project with a turn for command replacing the stop command

    • Add another while loop with a drive command inside. Set the condition using the get_distance command as the first operand, the greater than operator and the constant '50' as the second operand.

    additional while loop with drive command

    • Finally, add the following commands to instruct the VR Robot to detect the final wall behind the letter ‘A’ then stop driving.

    turn for and drive commands

    • 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 to the letter ‘A,’ and then stop.

    Move to A

    • Notice that the VR Robot does not bump into any walls as it drives to the letter ‘A.’ When the Distance Sensor value reports that the VR Robot is not greater than 50 millimeters (mm) from the wall, the VR Robot is instructed to turn or stop in the project.

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