🚴Coding Motors
In this tutorial, we'll show you how to code additional mechanisms on the robot, like an intake or catapult. Specifically, you'll learn how to spin a motor forwards and backwards at the press of a button.
First, add the motor to the devices list. In this example, our motor is named Motor

Next create a function that spins the motor forwards at full voltage:
//Spin the motor forward at full voltage
void spinMotorForward() {
Motor.spin(forward, 11, volt);
}
Now, let's create another function to spin the motor backwards at full voltage:
//Spin the motor backward at full voltage
void spinMotorBackward() {
Motor.spin(reverse, 11, volt);
}
Thirdly, we need a function to stop the motor from spinning when the button on the controller is released:
//Stop the motor
void stopMotor() {
Motor.stop();
}
All of this code is great, but none of it will run unless we set up the proper callbacks. We'll use the buttons L1 and L2 in this example, but feel free to change it to any buttons you would like. Put the callback code in the main function, so that the controller buttons activate the functions that spin or stop the motor.
int main() {
//Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
//Set up the motor callbacks
Controller1.ButtonL1.pressed(spinMotorForward); //when button L1 is pressed, spin the motor forward
Controller1.ButtonL2.pressed(spinMotorBackward); //when button L2 is pressed, spin the motor backward
Controller1.ButtonL1.released(stopMotor); //when button L1 is released, stop the motor
Controller1.ButtonL2.released(stopMotor); //when button L2 is released, stop the motor
//there may be more code below, leave it be
}
Now, download the code and test to see if it works!
Last updated
Was this helpful?