How do I put my code correctly in the competition Tempelate?

#1

#include “robot-config.h”
/---------------------------------------------------------------------------/
/* /
/
Description: Competition template for VCS VEX V5 /
/
/
/
---------------------------------------------------------------------------*/

//Creates a competition object that allows access to Competition methods.
vex::competition Competition;

/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/
/
/
You may want to perform some actions before the competition starts. /
/
Do them in the following function. You must return from this function /
/
or the autonomous and usercontrol tasks will not be started. This /
/
function is only called once after the cortex has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …

}

/---------------------------------------------------------------------------/
/* /
/
Autonomous Task /
/
/
/
This task is used to control your robot during the autonomous phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void autonomous( void ) {
while(Sonar1.distance(distanceUnits::in) <8)
{
Arm1.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);

}
while(Sonar1.distance(distanceUnits::in)>8)
{
Frontleftmotor.spin(vex::directionType::fwd,200,vex::velocityUnits::pct);
Backleftmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);

Backrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
Frontrightmotor.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);
}
Frontleftmotor.stop();
Frontrightmotor.stop();
Backleftmotor.stop();
Backrightmotor.stop();

/----------------------------------------------------------------------------/
/* /
/
User Control Task /
/
/
/
This task is used to control your robot during the user control phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
----------------------------------------------------------------------------*/

void usercontrol( void );
Backleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2
Backrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);//(Axis1-Axis2)/2
Frontleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis2+Axis1)/2
Frontrightmotor.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct); //(Axis4+Axis3)/2

if (Controller1.ButtonUp.pressing())
{

Arm1.spin(vex::directionType::fwd,200, vex::velocityUnits::pct);

}

else if (Controller1.ButtonDown.pressing())
{

Arm1.spin(vex::directionType::rev, 200, vex::velocityUnits::pct);

}

else if (!Controller1.ButtonUp.pressing())
{

Arm1.stop();

}

       else if (!Controller1.ButtonDown.pressing())
       {
           Arm1.stop();
       }

while (1){

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() {

//Run the pre-autonomous function. 
pre_auton();

//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );

//Prevent main from exiting with an infinite loop.                        
while(1) {using namespace vex;



while(true)




  vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}    

}

0 Likes

#2

Put a closed bracket after Backrightmotor.stop(); in your autonomous code.

Change


void usercontrol( void );

to


void usercontrol( void ){

But for the issue you specifically asked for, put every line of code from Backleftmotor.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct); //(Axis3+Axis4)/2 to the closed bracket in line 96 or 97 and paste it inside the


while (1){

and


vex::task::sleep(20);

By having the code the way you put it, it should only check at the very beginning joystick input, and then get stuck in a while loop just sleeping.

1 Like

#3

Make sure you don’t press the button at the top with the file name that says “Competition.” That’s essentially overwriting but it’s misleading.

0 Likes

#4

Where do functions go in the template?

0 Likes