Skip to main content

Lesson 2: Drive to Number ‘5’

In this Lesson, the VR Robot will drive to the number ‘5’ and back to the number ‘1’ on the Number Grid Map Playground!

A top-down view of the Number Grid Map playground, with the number 5 highlighted by a red box. The VR robot starts on number 1, and the number 5 square is four spaces to the right.

Notice that the VR Robot will be traveling along the X axis with X values increasing to move to the number ‘5’ on the Number Grid Map Playground.

A top-down view of the Number Grid Map playground, with the number 5 space highlighted by a black box. A line indicates measurements across the X axis, with the first space being -900 millimeters on the X axis, and the next being -700 continuing to count by 200 each time. The fifth space is at a X location of -100 millimeters.

The VR Robot will drive to the location of the number ‘5’ on the Number Grid Map Playground. However, before the VR Robot can navigate to that number, the VR Robot has to be told where that number’s location is. Beginning at the center of the Playground, the coordinates of number ‘5’ are (-100, -900).

A top-down view of the Number Grid Map playground, with two axis indicators marking the fifth spaces' location. The VR Robot starts at -900 X and -900 Y, while the fifth space is at -100 X and -900 Y.
  • To begin, the VR Robot needs to be facing the number ‘5.’ Drag in a [Turn for] block to the workspace.

    VEXcode VR blocks project that begins with a When Started block followed by a Turn For block to turn right for 90 degrees.
  • Drag the [Drive] non-waiting block into the workspace.

    A continuation of the VEXcode VR blocks project, now with a Drive Forward block added after the Turn For block. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward.
  • Attach a [Wait until] block beneath the [Drive] block.

    A continuation of the VEXcode VR blocks project, now with a Wait Until block added after the Drive Forward block. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward. Lastly there is now a Wait Until block with an empty boolean parameter.
  • The [Wait until] block accepts Boolean conditions. Just like the Distance Sensor Unit, this project will use the conditionals to drive through the Number Grid Map Playground. Drag the <Greater than> Boolean reporter block into the [Wait until] block.

    A continuation of the VEXcode VR blocks project, now with a Greater Than block inserted into the Wait Until block. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward. Lastly there is now a Wait Until block with a Greater Than block in it that reads 'blank is Greater Than 50'.
  • Note that the <Greater than> block is used instead of the <Less than> block because the VR Robot is driving to the right from the left side of the Playground. The VR Robot is starting at a X-value of -900 millimeters (mm). As the VR Robot drives forward, the X-values increase.

    The same top-down view of the Number Grid Map playground with two axis indicators marking the fifth spaces' location from earlier. The VR Robot starts at -900 X and -900 Y, while the fifth space is at -100 X and -900 Y.
  • Drag the (Position of Robot) block into the <Greater than> block.

    A continuation of the VEXcode VR blocks project, now with a Position of Robot block inserted into the Greater Than block. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward. Lastly Wait Until X Position of Robot in millimeters is Greater Than 50.

    For Your Information

    The (Position of Robot) block is a numeric reporter block that reports the X or Y coordinate position of the center of the VR Robot. Choose whether the X or Y coordinate position is reported.

    The Position of Robot block with the axis dropdown menu opened, changing the axis of measurement from X to Y.

    Using Switch Blocks 

    This is the Switch (Position of Robot) block. To change the (Position of Robot) block's parameter to report the Y coordinate, simply type "Y" for the coordinate value or select the coordinate suggestion that appears. 

    A Position of Robot switch block to demonstrate the auto-complete feature. The block reads 'location.position(X, MM)', and the user is changing the X parameter to Y using auto-complete.

    The (Position of Robot) block can report values in millimeters (mm) or inches.

    The Position of Robot block with the measurement unit dropdown menu opened, changing the unit of measurement from millimeters to inches.

    You can change the parameter for the unit of measurement while using the Switch (Position of robot) block by simply typing "INCHES" or selecting the suggestion that appears. Be sure to use all capital letters while typing parameters for units of measurement.

    A Position of Robot switch block to demonstrate the auto-complete feature. The block reads 'location.position(X, MM)', and the user is changing the millimeter parameter to inches using auto-complete.

  • Set the parameter of the <Greater than> block to -100.

    A continuation of the VEXcode VR blocks project, now with the Greater Than block's second parameter changed from 50 to -100. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward. Lastly Wait Until X Position of Robot in millimeters is Greater Than -100.
  • Drag in a [Stop driving] block and add it to the project.

    A continuation of the VEXcode VR blocks project, now with a Stop Driving block added below the Wait Until block. The whole project now reads When Started, Turn Right for 90 degrees and then Drive Forward. Lastly Wait Until X Position of Robot in millimeters is Greater Than -100 and then Stop Driving.
  • Open the Number Grid Map Playground if it is not already open and run the project.
  • Watch the VR Robot drive to number ‘5’ on the Number Grid Map Playground.

    A top-down view of the Number Grid Map playground, with the VR robot having driven to the fifth space.
  • In this project, the VR Robot drives to the number ‘5’ on the Number Grid Map Playground. The [Wait until] block is used so that the VR Robot will continue to drive forward until the X-value is greater than the X-value of the coordinate that the indicated number is on.
  • Once the X-value is greater than -100, the project moves to the next block in the stack, which is a [Stop driving] block. Since the X-value of the number ‘5’ is -100, the VR Robot will stop driving once the X-value is greater than -100.

A diagram visualizing the flow of logic in the VEXcode VR Blocks project. The project starts by turning right for 90 degrees then driving forwards, and holds that command until the Position of Robot sensor's X Position in millimeters is greater than -100, after which a Stop Driving block ends the Drive Forward command.

Using Switch Blocks 

In this Lesson, you learned how to use the VEXcode [Wait until] block with a Boolean condition to command the robot to wait until the robot reaches a position of greater than -100 on the X-axis before moving to the next behavior.

VEXcode VR block that reads Wait Until X Position of Robot in millimeters is greater than -100.

The image below shows the Switch [Wait until] block with the same commands in Python. Within the Switch block, while not location.position(X, MM) > -100: is the first Python command that checks whether the position of the robot along the X-axis is greater than -100.

The indented command, wait (5, MSEC) pauses the execution of the check for a specific amount of time. In this example, the project checks whether or not the robot's position is greater than an X value of -100 every 5 MSEC. This line of code is indented underneath the first line of code because this command is the behavior that will repeat until the condition (an X coordinate value of greater than -100) is met. 

VEXcode VR switch block with Python code that reads 'while not location.position(X, MM) > -100: wait(5, MSEC)'.

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