VCS Example Program with Controller - C++ - Claw and Arm
This example program shows how to program the V5 Clawbot to use remote control values to move your robot's claw and arm.
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,false); vex::motor ClawMotor (vex::PORT3, vex::gearSetting::ratio18_1,true); vex::motor armMotor (vex::PORT8, vex::gearSetting::ratio18_1,true);
main.cpp
#include "robot-config.h" int main() { //Use these variables to set the speed of the arm and claw. int armSpeedPCT = 50; int clawSpeedPCT = 50; while(true) { if(Controller1.ButtonUp.pressing()) { ArmMotor.spin(directionType::fwd, armSpeedPCT, velocityUnits::pct); } else if(Controller1.ButtonDown.pressing()) { ArmMotor.spin(directionType::rev, armSpeedPCT, velocityUnits::pct); } else { ArmMotor.stop(brakeType::brake); } if(Controller1.ButtonA.pressing()) { ClawMotor.spin(directionType::fwd, clawSpeedPCT, velocityUnits::pct); } else if(Controller1.ButtonY.pressing()) { ClawMotor.spin(directionType::rev, clawSpeedPCT, velocityUnits::pct); } else { ClawMotor.stop(brakeType::brake); } task::sleep(20); } }
How it works
First, the main
function is declared. An infinite while
loop is created so that the program can pull the remote control values every iteration. This loop causes the program to run forever.
Next, if ButtonUp on the controller is .pressing
, the arm motor will spin forward.
If ButtonDown on the controller is .pressing
, the arm motor will spin backward.
If neither button is being pressed, the armMotor will stop
.
Then, if ButtonA is .pressing
, the arm motor will spin forward.
If ButtonY on the controller is .pressing
, the arm motor will spin backward.
If neither button is being pressed, the armMotor will stop
.
Then, add a sleep
task for a short amount of time to prevent wasted energy.