Getting started
Create a VEXcode V5 C++ project and check the hardware before writing autonomous code.
A V5 Brain, two drive motors, an inertial sensor, and a USB cable. Shared configuration ↗
On this pageInstall VS Code and the VEX extension
Install VS Code and the VEX extension
Use the desktop version of Visual Studio Code with the official VEX Robotics extension. This keeps the VEX C++ API used throughout these guides; you do not need PROS for these examples.
- Download Visual Studio Code for your operating system from Microsoft, run the installer, and open the application.
- Open Extensions in the left Activity Bar. Search for VEX Robotics and install the extension published by VEX Robotics (extension ID: VEXRobotics.vexcode).
- In Extensions, search for C/C++ and install the extension published by Microsoft. It provides completion and error checking for C++.
- Open the VEX icon in the Activity Bar. If it is missing, confirm the VEX extension is enabled and reload VS Code.
References: Microsoft: Download and install Visual Studio Code (opens in a new tab) · VEX Robotics: Install the VEX and C/C++ extensions (opens in a new tab)
Create the project
In the VEX extension, select New Project under Project Actions. Choose V5, then C/C++, then an empty template. Enter a project name and location and select Create. Open src/main.cpp in the Explorer.
These examples define their own devices in main.cpp. Retain the generated vex.h and robot-config files, and avoid declaring another set of devices on the same ports. Replace the default main.cpp with the shared configuration below.
If you already use the standalone VEXcode V5 editor, an empty C++ text project works too. In VS Code, use the VEX extension’s build and download controls, rather than the generic Run Code command. Connect and power on the Brain, confirm it appears in the VEX extension, and select the intended program slot before downloading.
References: VEX Robotics: Creating VS Code projects for V5 (opens in a new tab) · VEX Robotics: Motor and Motor Group — C++ API (opens in a new tab)
Video walkthroughs by CoderSponge
I’m CoderSponge, the creator of this website. These two videos cover the initial setup and basic robot code. You can follow the written guide on its own or use the videos alongside it.
Configure the hardware
The example uses one motor per side, green 18:1 cartridges, and direct-drive 3.25-inch wheels. Change these values to match your robot. Port numbers and reversed flags are examples, not a wiring standard.
With the robot lifted, a positive command must move both wheels in the robot’s forward direction. Check each motor independently. A reversed flag fixes mounting direction; it does not describe where the port is on the Brain.
| Device | Example port | Configuration |
|---|---|---|
| Left drive motor | 1 | 18:1 cartridge, not reversed |
| Right drive motor | 10 | 18:1 cartridge, reversed |
| Inertial sensor | 5 | Clockwise-positive rotation |
References: VEX Robotics: Motor and Motor Group — C++ API (opens in a new tab) · PROS: Motors tutorial (opens in a new tab)
Shared main.cpp
Use this file as the base for later snippets. It defines the named devices, math helpers, autonomous callback, and arcade driver control. Later guides tell you which functions to add or replace; do not paste a second main() into the project.
The competition object dispatches the autonomous and driver-control functions. A callback is a function registered to run when the field control changes modes.
#include "vex.h"
#include <cmath>
#include <algorithm>
using namespace vex;
motor leftDrive(PORT1, ratio18_1, false);
motor rightDrive(PORT10, ratio18_1, true);
inertial imu(PORT5);
controller driver(primary);
competition match;
bool sensorsReady = false;
constexpr double pi = 3.141592653589793;
double limit(double value, double low, double high) {
return std::max(low, std::min(high, value));
}
double wrap180(double angle) {
while (angle > 180) angle -= 360;
while (angle < -180) angle += 360;
return angle;
}
void stopDrive() {
leftDrive.stop(brake);
rightDrive.stop(brake);
}
void preAuton() {
vexcodeInit();
if (!imu.installed()) return;
imu.calibrate();
timer calibration;
calibration.reset();
while (imu.isCalibrating() && calibration.time(msec) < 5000)
wait(20, msec);
sensorsReady = !imu.isCalibrating();
}
void autonomous() {
if (!sensorsReady) { stopDrive(); return; }
}
void driverControl() {
while (true) {
double drive = driver.Axis3.position(percent);
double turn = driver.Axis1.position(percent);
leftDrive.spin(forward, limit(drive + turn, -100, 100), pct);
rightDrive.spin(forward, limit(drive - turn, -100, 100), pct);
wait(20, msec);
}
}
int main() {
match.autonomous(autonomous);
match.drivercontrol(driverControl);
preAuton();
while (true) wait(100, msec);
}References: VEX Robotics: Competition — C++ API (opens in a new tab) · VEX Robotics: Motor and Motor Group — C++ API (opens in a new tab)
Calibrate the inertial sensor
Keep the robot still while preAuton() calibrates the inertial sensor. The example waits for calibration to finish and stops waiting after five seconds. A disconnected sensor or unfinished calibration leaves sensorsReady false, so the autonomous examples will not drive.
Heading wraps from 359.99° back to 0°. Rotation can continue through multiple turns. This distinction matters later: turning uses a wrapped heading error, while odometry uses changes in continuous rotation.
References: VEX Robotics: Inertial Sensor — C++ API (opens in a new tab)
Run the first movement
Replace autonomous() with the short test below. Lift the wheels clear of the table, download, run the project, and use a competition switch or controller competition mode to start autonomous. Both sides should turn forward for 300 milliseconds.
For a temporary bench test without competition control, insert autonomous(); immediately after preAuton(); in main(). Remove that call after testing so starting the project does not trigger a movement outside autonomous mode. Test on the floor only after confirming directions.
void autonomous() {
if (!sensorsReady) { stopDrive(); return; }
leftDrive.spin(forward, 20, pct);
rightDrive.spin(forward, 20, pct);
wait(300, msec);
stopDrive();
}References: VEX Robotics: Competition — C++ API (opens in a new tab) · VEX Robotics: Motor and Motor Group — C++ API (opens in a new tab)
If the robot does not move
Once direction and calibration are correct, continue to Simple autonomous. Do not try to tune PID around an incorrect motor direction.
- Check the downloaded program slot, Brain battery, motor cables, and configured ports.
- Confirm the autonomous callback is actually being started. Pressing Run starts main(), not automatically the competition autonomous period.
- Check sensorsReady and the inertial cable. Do not move the robot during calibration.
- If one side drives backward, correct that motor’s reversed flag, download again, and repeat the lifted-wheel test.
Sources for this guide
Examples are written for VEXcode C++. PROS and LemLib references explain related concepts; their APIs are different.
- MicrosoftDownload and install Visual Studio Code (opens in a new tab)
Install the desktop editor for your operating system.
- VEX RoboticsInstall the VEX and C/C++ extensions (opens in a new tab)
Official extension installation instructions. These guides use C++, so the Python extension is not needed for the examples.
- VEX RoboticsCreating VS Code projects for V5 (opens in a new tab)
Choose V5, C/C++, and an empty template; open src/main.cpp.
- VEX RoboticsMotor and Motor Group — C++ API (opens in a new tab)
VEXcode motor construction, encoder position, signed spin commands, and braking.
- VEX RoboticsInertial Sensor — C++ API (opens in a new tab)
Calibration, continuous rotation, heading wraparound, and the default clockwise-positive convention.
- VEX RoboticsCompetition — C++ API (opens in a new tab)
Registering autonomous and driver-control callbacks.
- PROSMotors tutorial (opens in a new tab)
Comparison of motor configuration, relative/absolute positioning, and firmware motion control. PROS code uses a different API.