Skip to main content

Programming with Conditionals in VEXcode IQ

Materials Required:
Quantity Materials Needed
1

VEX IQ Super Kit

1

VEXcode IQ

1

Engineering Notebook

1

Clawbot (Drivetrain) 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] block is the main focus within the activity but other Sensing, Control, and Operator blocks are also used.

Example blocks from the VEXcode IQ Toolbox - Brain button pressed, If then, not operator block, and wait until block.

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

VEXcode IQ Toolbar with the Tutorials icon called out in a red box, to the right of the File menu.

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

Before you begin programming with conditionals, first watch the Using Loops and If-Then-Else tutorial videos in VEXcode IQ.

Start by watching the Using Loops Tutorial video in VEXcode IQ.

Tutorial icon reads Using loops at the bottom and shows an outline of blocks within a loop.

Then watch the If-Then-Else Tutorial video in VEXcode IQ.

Tutorial icon reads If then else blocks at the bottom and shows the outline of if then else block with arrows.

Step 2: Programming with conditionals.

We want to build a project that will raise the arm if the condition of the Brain Up button being pressed is true.
 VEX IQ Brain with the Up button beside the upper left corner of the screen called out with a red box.
If the condition is false, the Arm Motor will stop. Let's explore building a project that uses a [Forever] block and the [If then] conditional block to program the arm.

  • Open the Clawbot (Drivetrain) Template. For help, view the Using Example Projects and Templates tutorial video.
    Example project icon reads Clawbot Drivetrain, to indicate which template to choose for this activity.
  • Build the project below.
    VEXcode IQ project with a when started block with a forever block attached. Inside the Forever block is an If then block that reads If Brain Up button pressed then spin arm motor up. Next is a stop arm motor block.
  • Save the project as ArmUp. If students need help saving the project, refer to the Naming and Saving tutorial video in VEXcode IQ.
    Project name dialog box in the VEXcode IQ Toolbar. Slot 1 is selected and the project name reads Arm Up.
  • Check to make sure the project name ArmUp is now in the window in the center of the toolbar.
  • Predict what the project will have the Clawbot do in your engineering notebook. Explain both the user's and the Clawbot's behaviors.
  • Test to see if your prediction of what the project has the Clawbot do is correct.
    • Download the project to Slot 1 on the Clawbot, and then run it.
    • For help downloading and running a project, see the tutorial in VEXcode IQ that explains how to Download and Run a Project.
      Tutorial icon reads Download and run a project at the bottom and has a download icon and triangle above.
  • Check your explanations of the project and add notes to correct them as needed.

Step 3: Understanding the [Wait until] block.

In the previous step, the project did not raise the arm successfully. View the following flowchart that explains the project flow. Notice that if the Brain Up button is pressed, the flow of the project moves so quickly that the project will move to the next block, which is the [Stop motor] block.

Project flow image indicating how the project is executed to make a decision. "If brain up button pressed' is labeled a the Condition. If pressed (true) then an arrow indicated spin arm motor up. If not pressed, false, a red arrow indicates stop arm motor. All of this is repeated because it is within the forever loop.

Thus, the project needs a [Wait until] block that tells the Arm Motor to keep spinning until the Brain Up button is released.

VEXcode IQ wait until block with the condition set to not Brain up button pressed, by nesting the Brain Up button pressed block inside a not operator block.

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 Arm Motor ever had time to respond. Thus, the blocks would flow down to the [Stop motor] block and then start back at the top of the stack because of the [Forever] block that repeats all blocks contained inside of it.

Let's explore changing the project by adding a [Wait until] block. The Arm Motor will now continue to spin until the Brain Up button is released. Once the Brain Up button is released, the project will continue to the next block, which is the [Stop motor] block.

The project will now first check the condition if the Brain Up button is being pressed. If the Brain Up button is being pressed (TRUE), then the arm will spin up until the Brain Up button is released. Once the Brain Up button is released, the project will move to the [Stop motor] block before returning to the top of the stack to begin again because of the [Forever] block.

If the Brain Up button is not pressed (FALSE), then the project will move to the [stop motor] block before returning to the top of the stack to begin again because of the [Forever] block and the arm will never spin.

Project flow image indicating how the project is executed to make a decision. "If brain up button pressed" is labeled as the condition. If pressed, true, then a green arrow indicates the spin arm motor up block will run followed by the wait until not brain up button pressed block reports true, then the stop arm motor block will run. If the brain up button is not pressed, it's false, and a red line indicates the project moves directly to stop arm motor. Again all of this is continually repeated inside the forever loop.

Step 4: Adding the [Wait until] block.

Let's add the [Wait until] block:

  • Add the [Wait until] block to your ArmUp project so that your project looks like the following:

    VEXcode IQ project with a when started block and a forever block attached. Inside the Forever block is an If then block that reads If brain up button pressed then spin arm motor up, wait until not Brain up button pressed. Beneath the if then block is a stop arm motor block.

  • Save the project as ArmUp2. If students need help saving the project, refer to the Naming and Saving tutorial.

 

Project name dialog box in the VEXcode IQ Toolbar. Slot 2 is selected and the project name reads Arm Up 2.

  • Download the project to Slot 2 on the Clawbot, and then run it.
  •  
  • For help downloading and running a project, see the tutorial in VEXcode IQ that explains how to Download and Run a Project.
    Tutorial icon that reads Download and Run a project on the bottom.
  • Test to see if the arm will now spin up when the Brain Up button is pressed.
  • Verify that when the Brain Up button is not pressed (released) that the Arm Motor stops.
  • Write your observations of how the Clawbot behaved before and after adding the [Wait until] block to your project in your engineering notebook.