Skip to main content

Programming with Conditionals - Blocks-based

Materials Required:
Quantity Materials Needed
1

VEX V5 Classroom Starter Kit (with up-to-date firmware)

1

VEXcode V5 (latest version, Windows, macOS, Chromebook)

1

Engineering Notebook

1

Clawbot (Drivetrain 2-motor, No Gyro) Template

The Clawbot is ready to make decisions!

This activity will give you the tools to program your robot with conditional behaviors.
The [If then] and [If then else] blocks are the main focus within the activity but Operators and Sensing blocks are also used.

Four VEXcode Blocks are shown. Clockwise from the top left they are: Screen pressed hexagonal block; not operator block; If then C-block; and If then else C-block.

You can use the Help information inside of VEXcode V5 to learn about the blocks. For guidance in using the Help feature, see the Using Help tutorial.

VEXcode V5 Toolbar with the Tutorials icon called out with a red arrow, to the right of the File menu.

 

Step 1: Let's start with an understanding of conditional statements 

Before you begin programming with conditionals, first watch the If-Then-Else tutorial video below. It can also be found as a Tutorial video in VEXcode V5.

 

Step 2: Let's start programming with conditional statements 

Open the Clawbot (Drivetrain 2-motor, No Gyro) template example project.

Template project icon reads Clawbot (Drivetrain 2-motor, No Gyro) at the bottom with a gray robot diagram above.

Build the project below.

VEXcode V5 project starts with a when started block followed by a forever loop. Inside the loop, there is a drive forward block followed by an if then block. The if then block reads If screen pressed then stop driving, wait until not screen pressed.

Do the following in your engineering notebook:

  1. Explain what the project has the Clawbot do. You will need to explain more than the fact that it creates a stop button. Explain which blocks make the Clawbot do what.
  2. Write a one sentence summary that captures what the project does.

Project name dialog box in VEXcode V5 reads Creating a Stop Button. To the left slot 1 is selected, and to the right the Toolbar reads Saved.

  • Test to see if your prediction of what the project has the Clawbot do is correct.
  • Save and download the project as Creating a Stop Button to Slot 1 on the Clawbot, and then run it.
  • For help downloading a project, see the tutorial in VEXcode V5 that explains how to Download and Run a Project.
  • Check your explanations of the project and add notes to correct them as needed.

Step 3: Understanding the wait until block

Notice that if the Brain's screen is pressed, the flow of the project moves so quickly that the project will move to the next block, which is the [Stop driving] block.

Thus, the project needs a [Wait until] block that tells the robot to remain stopped until the Brain's screen is released. Otherwise, the [Forever] block would cause the project to begin again with the [Drive] block.

Wait until block with a nested condition. The screen pressed block is nested in the not operator, so the whole block reads wait until not screen pressed.

The [Wait until] block is necessary because of the speed of the project's flow. If it was not there, the project would move to the next block before the robot ever had time to respond

 

Step 4: Change the project

Our next step is changing the [If then] block to an [If then else] block.

Side by side VEXcode blocks - on the left is the Creating a Stop button project. On the right is an If then else block from the Toolbox.

  • Start by saving Creating a Stop Button as the new project, StopOrDrive.
  • If you need help saving a project, see the Naming and Saving Your Project tutorial in VEXcode V5.
  • Then build the StopOrDrive project shown below.

VEXcode V5 project starts with a when started block with a forever block attached. Inside the forever block is an if then else block. The if branch reads if screen pressed, then stop driving, wait until not screen pressed. The else branch contains a drive forward block.

  • Download StopOrDrive to Slot 2 on your Clawbot.
  • For help downloading a project, see the tutorial in VEXcode V5 that explains how to Download and Run a Project.
  • Test Creating a Stop Button (Slot 1) and then test StopOrDrive (Slot 2) and compare them to see if there are any difference in the robot's behavior. Note any differences in your engineering notebook.

The two projects have the Clawbot behave the same way. The only difference is the use of the [If then else] block in the StopOrDrive project.

Using the [If then else] block will allow you to add additional buttons to the screen in upcoming activities.