VEX Example Program with Controller - C++ - Arcade Control

This example program shows how to program your robot to use remote control values to steer your robot.

ArcadeDrive.vex

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

    while (true) {          
        LeftMotor.spin(directionType::fwd, (Controller1.Axis3.value() + Controller1.Axis4.value())/2, velocityUnits::pct); //(Axis3+Axis4)/2;
        RightMotor.spin(directionType::fwd, (Controller1.Axis3.value() - Controller1.Axis4.value())/2, velocityUnits::pct);//(Axis3-Axis4)/2;

        task::sleep(20);
    } 
}
How it works

First, the main function is declared and a repeating while loop pulls remote control values during every iteration. This loop causes the program to run forever.

Next, the leftMotor and rightMotor are set to spin forward using the data from the joystick's Axis values to calculate the velocity.

Then, it is necessary to calculate the final velocity for each motor independently because only one joystick is used to move the robot in Arcade mode.

Axis3 corresponds to the y-coordinate value of the joystick. So whether you push the joystick all the way up to +100 or all the way down to -100, it spins both motors the same direction.

Axis4 corresponds to the x-coordinate value of the joystick and so it works differently. If you push the joystick all the way to the right to +100, then the left motor should have a positive velocity and the right motor should have a negative velocity so that the robot turns to the right. If you push the joystick all the way to the left to -100, then the left motor should have a negative velocity and the right motor should have a positive velocity so that the robot turns to the left. 

The sleep task of 20 milliseconds between each loop or iteration sets a pace for checking the joystick's coordinates.

Still need help? Contact Us Contact Us