Skip to main content
Teacher Portal

Programming with Conditionals - C++

Teacher Toolbox icon Teacher Toolbox - The Purpose of this Activity

This activity will introduce students to using while and if then statements to have the robot drive forward unless the screen is pressed.

  • This activity will introduce students to conditionals and the instructions that have the robot drive forward unless the screen is pressed.

  • For more information about if then and if then else statements or others used in this activity, visit the help information within VEXcode V5. For more information about this built in help tool, check out these articles about help in C++ .

Teacher Toolbox icon Teacher Toolbox

For suggestions on teaching strategies for this section, review the Delivery column of the To Do or Not to Do Pacing Guide! (Google Doc / .docx / .pdf)

The Clawbot is ready to make decisions!

This exploration will give you the tools to be able to start creating some cool projects that use if then else statement.

  • VEXcode V5 C++ commands that will be used in this exploration: 
    • Drivetrain.drive(forward);
    • Brain.Screen.pressing()
    • Drivetrain.stop();
    • if (condition){}
    • waitUntil();
    • while (condition){}  
    • wait(1, seconds);

You can use the Help information inside of VEXcode V5 to learn about the C++ instructions.

VEXcode V5 with a drive for command typed in the workspace on the left, and the Help information open on the right. The Help shows a definition of the command and information about how it is used.

Make sure you have the hardware required, your engineering notebook, and VEXcode V5 ready.

Materials Required:
Quantity Materials Needed
1

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

1

VEXcode V5 (latest version, Windows, macOS)

1

Engineering Notebook

1

Clawbot (Drivetrain 2-motor, No Gyro) Template

Teacher Tips icon Teacher Tips

If this is the student’s first time using VEXcode V5, they can reference the Tutorials at any time during this exploration. The Tutorials are located in the toolbar.

VEXcode V5 Toolbar with the Tutorial icon highlighted in a red box. From the left the Toolbar shows the V5 icon, a globe icon, File, then Tutorials. To the right are additional icons and functionality.

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

Before you begin programming with conditionals, read the VEX Library Article explaining If Then Else Statements. The article can be found here.

Screenshot from the VEX Library, showing an article titled Using if-else statements in VEXcode V5.For a list of operators to use in the If Then Else statements, read the VEX Library Article explaining Booleans. This article can be found here.Screenshot from the VEX Library, showing an article titled Using Booleans in VEXcode V5.

Step 2: Let's start programming with conditional statements.

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

    Example project selection in VEXcode V5 with a red box around the Templates filter at the top and the Clawbot drivetrain 2 motor no gyro project, indicating which template to open.

     

  • Build the project below.
// Begin project code
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  while (true) {
    Drivetrain.drive(forward);
    if (Brain.Screen.pressing()) {
      Drivetrain.stop();
      waitUntil(!Brain.Screen.pressing());
    }    
  }  
}  

Do the following in your engineering notebook:

  • 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 instructions make the Clawbot do what.
  • Write a one sentence summary that captures what the project does.
  • Test to see if your prediction of what the project has the Clawbot do is correct.

Project Name Dialog box in the VEXcode V5 Toolbar reads Creating a Stop Button and shows that slot 1 is selected.

  • Save and download the project as CreatingAStopButton 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 (C++).
  • Check your explanations of the project and add notes to correct them as needed.

Teacher Toolbox icon Teacher Toolbox - Answers

This project has the robot continuously drive forward but stop if the screen is pressed. It does this by using a forever statement to drive and check continuously. If the screen is being pressed (TRUE), then the Clawbot stops driving.The Creating a Stop Button project is shown with commands labeled with their purpose. The if command is circled in red and labeled Checks if condition is True - the screen is pressed. The Drivetrain dot stop command is labeled Runs the block if true. The wait until command is labeled controls the speed of processing.

Students are not expected to understand why the waitUntil () instruction is used. Explain that the waitUntil () instruction is necessary because of the speed of the robot's program flow. If it was not there, the Clawbot's motors would behave as though the user is pressing the screen again and again as it loops through the project. Instead, the waitUntil() instruction stops the program flow and does not have the project start the forever loop again until the user stops pressing the screen.

The line of pseudocode could be as simple as: Drive forward until the screen is pressed.

Students' engineering notebooks can be maintained and scored individually (Google Doc / .docx / .pdf) or as a team (Google Doc / .docx / .pdf). The previous links provide a different rubric for each approach. Whenever a rubric is included in educational planning, it is good practice to explain the rubric or at least give copies to students before the activity begins.

Step 3: Understanding the wait until() statement.

Notice that if the Brain's screen is pressed, the flow of the project moves quickly and the project will move to the next instruction, which is the Drivetrain.stop() instruction.

Thus, the project needs a waitUntil() instruction that tells the robot to remain stopped until the Brain's screen is released. Otherwise, the forever statement would cause the project to begin again.

waitUntil(!Brain.Screen.pressing());

The waitUntil() instruction is necessary because of the speed of the project's flow. If it was not there, the project would move to the next instruction before the robot ever had time to respond.

Step 4: Change the project.

Our next step is changing the if then statement to an if then else statement.

Project name dialog box in the VEXcode V5 Toolbar reads Stop or Drive and shows that slot 1 is selected.

  • Start by saving CreatingAStopButton as the new project, StopOrDrive.
  • If you need help saving a project, click here for C++.
  • Then build the StopOrDrive project shown below.
// Begin project code
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  while (true) {
    Drivetrain.drive(forward);
    if (Brain.Screen.pressing()) {
      Drivetrain.stop();
      waitUntil(!Brain.Screen.pressing());
    } 
    else {
      Drivetrain.drive(forward);
    }   
  }  
}  
  • Download StopOrDrive to Slot 2 on your Clawbot.

Slot selection in the VEXcode V5 Toolbar is open and slot 2 is selected and highlighted with a red box. The project name reads Stop or Drive.

  • For help downloading a project, see the tutorial in VEXcode V5 that explains how to Download and Run a Project (C++).
  • Test CreatingAStopButton (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

Teacher Toolbox icon Teacher Toolbox - Answer

In regard to the robot's behavior, there should not be a difference between the CreatingAStopButton and StopOrDrive projects.

If the students need further assistance with understanding If-Else Statements, click here.

The two projects have the Clawbot behave the same way. The only difference is the use of the if then else statement in the StopOrDrive project.

Using the if then else statement will allow you to add additional buttons to the screen in upcoming activities.

Extend Your Learning icon Extend Your Learning

As this STEM Lab proceeds, students will develop projects that allow the Clawbot's screen to function as a user interface. In preparation for that, challenge students to figure out how to draw a colored rectangle on the screen that functions like a button. Because pressing anywhere on the screen is currently the condition, the button should take up the entire screen. Students should have the project broadcast an event to draw the button when started.
Direct students to first learn how the brain's screen is organized into a coordinate system. They will need to understand this in order to set parameters within the instructions they will be using. The coordinates used correspond to the number of pixels not the number of the column or row. Here is the layout of the screen:

Pixel grid of the V5 Brain screen shows 12 numbered rows along the lefthand side, with the top row labeled Row 1 and the bottom labeled Row 12. Across the top are 48 numbered columns, with Column 1 labeled on the far left and Column 48 labeled on the far right. The total pixel measurements are 480px wide by 240 px tall.

Students should plan, test, and refine these changes to the project while documenting it within their engineering notebooks. For the individual engineering notebook rubric, click one of the following links (Google Doc / .docx / .pdf), or click one of the following links for team notebooks (Google Doc / .docx / .pdf).

Here is an example solution:

// Begin project code
event draw = event();

void drawButton() {
  Brain.Screen.setFillColor(red);
  Brain.Screen.drawRectangle(0, 0, 480, 240);
  wait(1, seconds);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  draw(drawButton);

  while (true) {
    draw.broadcast();
    Drivetrain.drive(forward);
    if (Brain.Screen.pressing()) {
      waitUntil(!Brain.Screen.pressing());
    } 
    else {
      Drivetrain.drive(forward);
    }   
  }  
}  

As you can see, the button is drawn to take up the entire screen starting at the (0, 0) origin and filling all 480 horizontal (x-axis) pixels and 240 vertical (y-axis) pixels.