Programming with Conditionals - C++
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.
Make sure you have the hardware required, your engineering notebook, and VEXcode V5 ready.
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 |
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.
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.
Step 2: Let's start programming with conditional statements.
-
Open the Clawbot (Drivetrain 2-motor, No Gyro) template example project.
- 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.
- 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.
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.
- 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.
- 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
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.