Here is some example code. It’s from VEXcode but should work in VCS as well.
It uses additional tasks for drive and controlling a motor with one button.
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
// A global instance of vex::competition
vex::competition Competition;
// define your global instances of motors and other devices here
vex::motor leftMotor( vex::PORT1 );
vex::motor rightMotor( vex::PORT2 );
vex::motor puncher( vex::PORT3 );
vex::controller Controller1;
vex::task dtask; // variable to hold drive task instance
vex::task ptask; // variable to hold puncher task instance
void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}
int puncherTask() {
while(1) {
if( Controller1.ButtonB.pressing() ) {
// send motor around once
puncher.rotateFor(fwd, 1, rotationUnits::rev, 70, rpm );
// wait for button release
while( Controller1.ButtonB.pressing() ) {
vex::task::sleep(10);
}
}
vex::task::sleep(20);
}
return(0);
}
int driveTask() {
while(1) {
leftMotor.spin( fwd, Controller1.Axis3.position(),velocityUnits::pct );
rightMotor.spin( fwd, Controller1.Axis2.position(),velocityUnits::pct );
vex::task::sleep(20);
}
return(0);
}
void autonomous( void ) {
int count = 0;
// make sure tasks are not running in auton
dtask.stop();
ptask.stop();
// make sure motors are stopped
leftMotor.stop();
rightMotor.stop();
puncher.stop();
while(1) {
// just show we are running
Brain.Screen.printAt( 10, 140, "Auton running %d", count++ );
vex::task::sleep(100);
}
}
void usercontrol( void ) {
int count = 0;
dtask = vex::task( driveTask );
ptask = vex::task( puncherTask );
while (1) {
// just show we are running
Brain.Screen.printAt( 10, 40, "Driver running %d", count++ );
Brain.Screen.printAt( 10, 60, "Left %7.2f", leftMotor.velocity(pct) );
Brain.Screen.printAt( 10, 80, "Right %7.2f", rightMotor.velocity(pct) );
Brain.Screen.printAt( 10, 100, "Punch %7.2f", puncher.rotation(rotationUnits::rev) );
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}
//
// Main will set up the competition functions and callbacks.
//
int main() {
int count = 0;
//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
//Run the pre-autonomous function.
pre_auton();
//Prevent main from exiting with an infinite loop.
while(1) {
Brain.Screen.printAt( 10, 20, "Main running %d", count++ );
vex::task::sleep(100);
}
}