Skip to main content

Lesson 3: Drive to Number ‘1’

  • Reset the Playground to move the VR Robot back to the starting position.
  • Now, the VR Robot will turn around and return to the number ‘1’ once it reaches number ‘31’ on the Number Grid Map Playground.

    A top-down view of the Number Grid Map playground, with two axis indicators marking the location of the number 31 space. The VR Robot is on the number 31 space at -900 X and -300 Y.
  • Remove the [Stop driving] block and replace it with the following blocks.

    An overview of the blocks that need to be added to our project from earlier to drive the robot from space 1, to space 31, and back to space 1. This is done by adding turn, drive, and wait blocks before the Stop Driving block. The full project now reads When Started, Drive Forward and Wait Until Y Position of Robot in millimeters is Greater Than -300. Next Turn Right for 180 degrees and then Drive Forward. Lastly Wait Until Y Position of Robot in millimeters is Less Than -900 and then Stop Driving.
  • Note that the second [Wait until] block contains a <Less than> block instead of a <Greater than> block. This is because the VR Robot is now driving down the Y axis and the numbers are becoming more negative. The VR Robot will stop once the Y-values are LESS than -900.

    The same VEXcode VR blocks project to drive the VR robot from space 1, to space 31, to space 1, with the second Wait Until block highlighted to draw attention to the use of a Less Than block instead of a Greater Than block. The whole project reads When Started, Drive Forward and Wait Until Y Position of Robot in millimeters is Greater Than -300. Next Turn Right for 180 degrees and then Drive Forward. Lastly Wait Until Y Position of Robot in millimeters is Less Than -900 and then Stop Driving.
  • Launch the Number Grid Map Playground if it is not already open, and run the project.
  • Watch the VR Robot drive to number ‘1’ on the Number Grid Map Playground.
  • In this project, the VR Robot drives to the number ‘1’ on the Number Grid Map Playground. Since the Y-values are decreasing as the VR Robot drives to the number ‘1,’ the project uses a <Less than> block.
  • The VR Robot will stop once its Y-value is less than the Y-value of the coordinate that the indicated number is on. Since the Y-value of the number ‘1’ is -900, the VR Robot will stop driving once the Y-value is less than -900.

    A diagram visualizing the flow of logic in the VEXcode VR Blocks project we've been working on. The project starts by driving forwards, and holds that command until the Position of Robot sensor's Y Position in millimeters is greater than -300. Next turn right for 180 degrees and drive forwards, holding that command until the Position of Robot sensor's Y Position in millimeters is less than -900, after which a Stop Driving block ends the Drive Forward command.

    For Your Information

    Comments are usually added to projects to explain what a programmer wants parts of a project to do. Comments are helpful when collaborating and troubleshooting, as they provide context and overall meaning to the code. Using comments allows the programmer to think conceptually about the overall goal and intention of the project, instead of trying to “guess and check.” Do you want to remember what coordinates go with a certain number? Add a comment saying “Drive to 81 located at (-900, 700).” This helps to communicate different sections and blocks of the project.

    An example VEXcode VR blocks project to drive the robot from space 1, to space to 81, back to space 41. Comments have been added to make the code more readable and list the coordinates of each location to help with debugging. The project begins with a When Started block followed by a comment that says 'Drive to 81 located at (-900,700)'. Next Drive Forward and Wait Until Y Position of Robot in millimeters is greater than 700. Next a comment reads 'Turn around' followed by a block to Turn Right for 180 degrees. Next is another comment reading 'Drive to 41 located at (-900, -100)' followed by a Drive Forward block. Lastly, Wait Until Y Position of Robot in millimeters is less than -100 and then Stop Driving.

    For more information on comments, view the Comments - VR Blocks article.

Using Switch Blocks 

The image below shows the project above converted into a Switch block. Note that comments in Python are written in green. You can write comments in Python by entering a pound sign (#) followed by your comment. 

A VEXcode VR switch block implementation of the VR blocks code to travel from space 1, to space 81, to space 41. The switch code is 11 lines long, as follows: '# Drive to 81 located at (-900, 700), drivetrain.drive(FORWARD), while not location.position(Y, MM) > 700: wait(5, MSEC), # Turn around, drivetrain.turn_for(RIGHT, 180, DEGREES), # Drive to 41 located at (-900, -100), drivetrain.drive(FORWARD), while not location.position(Y, MM) < -100: wait(5, MSEC), drivetrain.stop()'.

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