VCS Example Program - VEX C++ - V5 Clawbot - Driving Forward and Backward

This example program shows how to program the V5 Clawbot to move forward and backward using VEX C++.


vex::brain Brain;
vex::motor LeftMotor  (vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor RightMotor (vex::PORT10, vex::gearSetting::ratio18_1, true);
#include "robot-config.h"

vex::rotationUnits rotations = vex::rotationUnits::rev;

int main() {
    int howMany = 1;
    LeftMotor.startRotateFor(howMany, rotations);
    RightMotor.rotateFor(howMany, rotations);
    howMany = -2;
    LeftMotor.startRotateFor(howMany, rotations);
    RightMotor.rotateFor(howMany, rotations);
How it works

First, for convenience, a variable named rotations is declared. This is a shorthand to make the program easier to read.

Next, the main function is declared. An integer variable, howMany is declared and assigned to 1.

Then, the left motor is set to start rotating, using the two variables. Since startRotateFor is used, the motor will begin rotating and the program will continue immediately.

Next, the right motor is set to rotate as well, using the two variables. Since rotateFor is used, the program will not continue until the motor completes its rotation.

Then, when the right motor has finished rotating, the program will pause for 1 second ( 1000 milliseconds).

Finally, the howMany variable is set to -2, and the motor rotation commands are repeated. Since a negative number is given to the motors, they will spin in the opposite direction this time.