Skip to main content

Lesson 2: Using Comparison Blocks and Driving Until Near

In this Lesson, you will create a project that navigates the VR Robot to the letter ‘A’ using the Distance Sensor with the <Less than> Boolean reporter block. In the mini-challenge, you will apply these skills to navigate from the beginning of the Wall Maze to the number '1.'

A top-down view of the wall maze with the letter A goal highlighted in a red box, near the VR robot's starting position.

Learning Outcomes

  • Identify that the <Greater than> block is a Boolean block that reports if the first value is larger than the second value.
  • Identify that the <Less than> block is a Boolean block that reports if the first value is less than the second value.
  • Identify that the <Greater than> or <Less than> blocks report a TRUE or FALSE value in a [Wait until] block.
  • Describe how to create a project that has a VR Robot drive forward until the value of the Distance Sensor is less than a threshold value of 50 millimeters (mm).

Name and Save the Project

  • Start a new project in VEXcode VR and name the project Unit5Lesson2. Remember that you may use VEXcode blocks, Switch blocks, or a combination of both block types for your project. 
VEXcode VR Toolbar with the 'Project Name' button highlighted in a red box, to the left of the Select Playground button. The project name is set to Unit 5 Lesson 2.

Use the Distance Sensor to Avoid Walls

  • Drag the [Drive] block into the workspace.
    VEXcode VR blocks project, beginning with a When Started block with a Drive Forward block below it. There is a red box highlighting the Drive Forward block.
  • Attach a [Wait until] block beneath the [Drive] block.
    The same VEXcode VR blocks project as before, but now with a Wait Until block at the bottom highlighted with a red box. The full project reads When Started, Drive Forward and Wait Until. The Wait Until block's parameter is empty.

This project will use the Distance Sensor to navigate through the Wall Maze Playground without bumping into walls. You will use the (Distance from) block inside of the <Less than> Boolean block in this project.

The <Less than>, <Greater than>, and <Equal to> are comparison blocks - they compare values. They are also Boolean reporter blocks that report a TRUE value when the comparison condition is met, and FALSE when the condition is NOT met.

  • <Less than> reports TRUE when the first value is smaller than the second value and FALSE when the first value is greater than or equal to the second value.
    VEXcode VR Less Than boolean block, reading '25 less than 50'.
  • <Greater than> reports TRUE when the first value is larger than the second value, and FALSE when the first value is smaller than or equal to the second value.
    VEXcode VR Greater Than boolean block, reading '100 greater than 50'.
  • <Equal to> reports TRUE when the two values are exactly the same, and FALSE when they are not.
    VEXcode VR Equal To boolean block, reading '50 equal to 50'.

Comparison blocks can accept decimals, integers, or numeric blocks. They also accept round reporter blocks, such as the (Distance from) block. The (Distance from) block can be with a comparison block when using the Distance Sensor in a project.

In this example, the block will report TRUE when the distance from an object and the Distance Sensor on a VR Robot is less than 50 millimeters (mm).

VEXcode VR Less Than boolean block with a Distance From block inside of it, reading 'Front Distance in millimeters less than 50'.

  • Drag the <Less than> Boolean block into the [Wait until] block.
  • Place the round (Distance from) block inside the Boolean reporter block
    VEXcode VR Blocks project that puts together the previous steps. The whole project now reads When Started, Drive Forward and Wait Until Front Distance in millimeters is less than 50. A red box highlights the Less Than boolean block.
  • The [Wait until] block will keep the VR Robot driving forward until the condition is met, and the distance reported is less than 50 millimeters (mm). Then, the project will move on to the next block in the stack, which will be the [Stop driving] block.
    A diagram visualizing the flow of logic in the VEXcode VR Blocks project. The project starts by driving forwards, and holds that command until the Front Distance sensor's distance in millimeters is less than 50, after which a Stop Driving block ends the Drive Forward command.
  • A [Stop driving] block is needed after the [Wait until] block because the VR Robot will continue to drive until it is instructed to stop. Add a [Stop driving] block to the project.
    The VEXcode VR blocks project from before with a Stop Driving block added after the Wait Until block. The whole project now reads, When Started Drive Forward and Wait Until Front Distance in millimeters is less than 50, after which Stop Driving.
  • 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 and stop when the Distance Sensor reports that it is less than 50 millimeters (mm) from the wall.
    A top-down view of the wall maze with the letter A goal near the VR robot's starting position. The VR robot has now moved forward as a result of the blocks project.
  • Using the <Less than> block with the (Distance from) block inside of the [Wait until] block, instructs the VR Robot to wait until it is less than 50 millimeters (mm) from the wall before stopping.
  • Reset the Playground to move the VR Robot back to the starting position.

Using Switch Blocks

In this lesson, you learned about the <Greater than>, <Less than>, and <Equal to> Boolean blocks. The image below shows the Switch Boolean comparison blocks. For a Switch Boolean block to function as intended, the Switch Python functions must be entered with the correct spelling and punctuation. 

Note that the Switch <Equal to> block uses two equal signs (==). This is because in Python, one equal sign (=) represents an assignment of a value (for example, x = 10), while two equal signs (==) check whether two values are equal. Be sure to use two equal signs when using the Switch <Equal to> block. 
A comparison of three VEXcode VR boolean blocks and their corresponding switch blocks. The Less Than block is written in Python as '25 < 50', the Greater Than block is written in Python as '100 > 50', and the Equal To block is written in Python as '50 == 50'. A red box highlights the double equal signs in the last example, demonstrating how to compare two values as equal in Python.

You also saw an example project in which the VR Robot will drive forward until the distance reported is less than 50 millimeters (mm) from an object. Then, the VR Robot will stop. The project on the right shows the corresponding Python commands inside a Switch block. The command, while not front_distance.get_distance(MM) < 50 tells the robot to check whether the distance reported is less than 50 MM from an object.

The next indented line of code, wait (5, MSEC), pauses the code for 5 milliseconds. The command that checks whether the distance reported is less than 50 MM from an object will wait for 5 MSEC before checking the condition again. 

A comparison of our VEXcode VR project, with the Wait Until block converted to a switch block on the right side. The new switch block reads in Python 'while not front_distance.get_distance(MM) < 50: wait(5, msec)'.

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