Store numbers, give them names, and use them to control the robot.
int, double, and bool are and when to use each.A variable is a labeled box that holds a value. You can read it, change it, do math with it. The label is the name you choose. The value is what's inside.
int driveSpeed = 50; // whole number (percent)
double turnPower = 35.5; // decimal number
bool isHolding = true; // true or false
int, whole numbers. Speed percentages, encoder ticks, time in milliseconds.double, decimal numbers. Sensor readings, distance in inches, PID error.bool, true or false. Flags like "has the robot stopped yet?"Compare these two versions of the same program. Both drive forward at 50% for 1.5 seconds. Which is easier to tweak later?
Bad, magic numbers:
LeftMotor.spin(forward, 50, percent);
RightMotor.spin(forward, 50, percent);
wait(1.5, seconds);
LeftMotor.stop();
RightMotor.stop();
Better, variables with meaningful names:
int main() {
vexcodeInit(); // IDE-supplied setup, leave this alone
int speedPct = 50;
double driveSec = 1.5;
LeftMotor.spin(forward, speedPct, percent);
RightMotor.spin(forward, speedPct, percent);
wait(driveSec, seconds);
LeftMotor.stop();
RightMotor.stop();
}
LeftMotor, RightMotor) are still generated for you by the Robot Configuration menu. You only write what goes after vexcodeInit();.
camelCase: driveSpeed, not drive_speed or DRIVESPEED.x is too vague; distanceInches tells you what and the unit.const and ALL_CAPS: const int MAX_SPEED = 100;Two programs with intentional errors. Spot the bug.
int speedPct = 50;
int driveSec = 1.5;
LeftMotor.spin(forward, speedPct, percent);
RightMotor.spin(forward, speedPct, percent);
wait(driveSec, seconds);
LeftMotor.stop();
RightMotor.stop();
const int MAX_SPEED = 80;
int main() {
MAX_SPEED = 100;
LeftMotor.spin(forward, MAX_SPEED, percent);
}