Skip to main content

Lesson 2: Driving for Distance - Moving Forward and in Reverse

In this Lesson, you will create a VEXcode VR project that uses forward and reverse Drivetrain commands to knock down the first castle in the Castle Crasher Playground!

Castle Crasher Playground with five castles, one in each corner of the playground, and one in the middle. The middle castle is highlighted with a red box. The VR Robot is positioned along the back of the Playground, directly facing the middle castle.

Learning Outcomes

  • Identify how to use the [Drive for] block to drive the VR Robot a specific distance.
  • Identify how to change the parameter in the [Drive for] block to move the VR Robot forward and reverse.
  • Identify how to use the [Set drive velocity] block to set the speed of the drivetrain.
  • Identify the range of values used in the [Set drive velocity] block.

Create a New Project

A VEXcode VR project must be created in order for a VR Robot to move forward in order to collide with the first castle in the Castle Crasher Playground, then drive in reverse to return to the starting position.

Follow these steps to create a new VEXcode VR project.

  • Launch VEXcode VR at vr.vex.com.
  • Open the File Menu and select 'New Blocks Project'.
  • You will be prompted to select a Playground. Select the Castle Crasher Playground. Icon representing the Castle Crasher Playground. The icon shows the VR Robot on the playground and five castles. The words "Castle Crasher" are along the bottom of the icon.
  • The Playground will open in the Workspace. Castle Crasher Playground window open in the VEXcode VR workspace.

Name Your Project

  • To name your project, select the project name box.

    VEXcode VR Workspace with a red box highlighting the project name box in the center of the toolbar.  

     

  • Enter the new project name Unit2Lesson2, and select “Save."

    Project name popup open with the new project name, Unit 2 Lesson 2, typed. A red box calls out the Save option at the bottom of the popup.

Drive Forward and in Reverse

Use the [Drive for] block to program a VR Robot to move forward to knock over the first castle on the Castle Crasher Playground.

  • Drag the [Drive for] block into the workspace and place it under the [When started] block.

    VEXcode VR project that begins with a When started block and has a drive forward block attached underneath. The parameter in the block is set to 200 mm.

  • Select the “Open Playground” button to launch the Castle Crasher Playground if it is not already open.

    VEXcode VR coding environment with a red box calling out the Open Playground button to the top right of the toolbar, to the right of the Select Playground button.

  • Check to make sure that the Castle Crasher Playground is open.

    Top down view of the Castle Crasher Playground, in the Playground Window of VEXcode VR.

  • If a different Playground opens, change to the Castle Crasher Playground. To change Playgrounds, select the Select Playground button.

    VEXcode VR Toolbar with a red box calling out the Select Playground button to the right side of the Toolbar, to the left of the Open Playground button.

  • Select Castle Crasher in the Select Playground menu.
  • Select the “Start” button to test the project.

    VEXcode VR Toolbar with the Start button highlighted by a red box, to the right hand side between the Open Playground button and Step button.

  • The VR Robot will drive forward for 200mm on the Castle Crasher Playground. Note that the VR Robot did not touch the center castle. The parameters in the [Drive for] block need to be adjusted so the VR Robot can drive further.

    VR Robot on the Castle Crasher Playground. There are five castles on the Playground, one in each corner, and one in the middle. The robot is facing the one in the middle and has driven forward for 200 millimeters, placing it about one quarter of the way to the middle castle.

  • Select the “Reset” button to reset the Playground and move the VR Robot back to the starting position.

    Castle Crasher Playground in the Playground window. There are two buttons to the left of the Playground. The one on top showing a black square, is the stop button. The one on the bottom, showing an arrow in the shape of a partial circle, is the reset button. The reset button is highlighted with a red box.

Using Switch Blocks 

So far, you have learned about the [Drive for] block.

VEXcode block reading drive forward for 200 millimeters.

The image below displays the Switch [Drive for] block that includes the command to Drive forward for 200 mm in Python. If you would like to use Switch blocks, simply click on the VEXcode [Drive for] block and select "Convert Block to Switch Block" or drag a Switch drivetrain block into the workspace and type the Python command inside of the block. 

Switch block with the following Python command typed out:  drivetrain.drive_for(forward, 200, mm).

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