If you've been coding in VEXcode Blocks, here's how to make the jump.
Blocks are a great starting point, but every block is just a friendly wrapper around a line of C++. Once you outgrow them, when you want to write your own PID, your own odometry, your own state machines, you need text.
#include "vex.h" and an int main() function. That's where your code goes.Here are the four most common patterns. Read the block on the left, read the C++ on the right, and notice how they match.
int main() {
Drivetrain.drive(forward, 50, percent);
wait(2, seconds);
Drivetrain.stop();
}
if (FrontBumper.pressing()) {
Drivetrain.stop();
}
for (int i = 0; i < 4; i++) {
Drivetrain.driveFor(forward, 12, inches);
Drivetrain.turnFor(right, 90, degrees);
}
int speed = 50;
Drivetrain.drive(forward, speed, percent);
In blocks, you snap things together. In C++, the semicolon is the snap.
LeftMotor.spin(forward); // semicolon = end of statement
What was "everything inside this loop" in blocks is now "everything between { and }".
if (FrontBumper.pressing()) {
// these two lines are inside the if
LeftMotor.stop();
RightMotor.stop();
}
// this line runs no matter what
Brain.Screen.print("done");
In blocks, a variable just exists. In C++, you tell it what kind of value it holds.
int speed = 50; // whole number
double distance = 24.5; // decimal number
bool isHolding = true; // true / false
LeftMotor and leftmotor are two different things in C++. Blocks don't care about case; C++ does.
Forget a semicolon? Wrong type? The compiler will tell you, with a line number. Read the error. It's almost always more helpful than it looks at first.
Head back to the dashboard and start Week 1: Your First C++ Program. The patterns above will come up immediately, and now you know what they mean.