Autonomous Code not working in competition template RobotC

I am using a sample program for the IME’s:

#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1,  rightIME,       sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_2,  leftIME,        sensorNone)
#pragma config(Motor,  port1,           backRightMotor, tmotorVex393HighSpeed, openLoop)
#pragma config(Motor,  port2,           backRightMidMotor, tmotorVex393HighSpeed, openLoop, reversed)
#pragma config(Motor,  port3,           frontRightMotor, tmotorVex393HighSpeed, openLoop, reversed, encoder, encoderPort, I2C_1, 1000)
#pragma config(Motor,  port4,           backLeftMidMotor, tmotorVex393HighSpeed, openLoop, reversed)
#pragma config(Motor,  port5,           topLeft,       tmotorVex393, openLoop)
#pragma config(Motor,  port6,           frontLeftMotor, tmotorVex393HighSpeed, openLoop, reversed, encoder, encoderPort, I2C_1, 1000)
#pragma config(Motor,  port7,           topRight,      tmotorVex393, openLoop)
#pragma config(Motor,  port8,           intakeLeft,    tmotorVex393, openLoop)
#pragma config(Motor,  port9,           intakeRight,   tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port10,          backLeftMotor, tmotorVex393HighSpeed, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Move Forward and Backward - 393 Motors
This program instructs your robot to move forward at half power for 1000 encoder counts, then reverse
at half power for 1000 encoder counts. There is a two second pause at the beginning of the program.

Robot Model(s): Modified Squarebot

*          [Name]              [Type]                [Description]
Motor Port 1        rightMotor          393 Motor             Right side motor
Motor Port 10       leftMotor           393 Motor             Left side motor, Reversed
I2C_1               rightIEM            Integrated Encoder    Encoder mounted on rightMotor
I2C_2               leftIEM             Integrated Encoder    Encoted mounted on leftMotor
----------------------------------------------------------------------------------------------------*/

task main
{
	wait1Msec(2000);

	//Clear the encoders associated with the left and right motors
	nMotorEncoder[frontRightMotor] = 0;
	nMotorEncoder[frontLeftMotor] = 0;

	//While less than 1000 encoder counts of the right motor
	while(nMotorEncoder[frontLeftMotor] < 1000)
	{
		//Move forward at half power
		motor[frontRightMotor] = 63;
		motor[backRightMotor] = 63;
		motor[backRightMidMotor] =  63;
		motor[frontLeftMotor] =  63;
		motor[backLeftMotor] = 63;
		motor[backLeftMidMotor] =  63;
	}

  //Clear the encoders associated with the left and right motors
	nMotorEncoder[frontRightMotor] = 0;
	nMotorEncoder[frontLeftMotor] = 0;

	//While less than 1000 encoder counts of the right motor
	while(nMotorEncoder[frontRightMotor] > -1000)
	{
		//Move in reverse at half power
		motor[frontRightMotor] = -63;
		motor[backRightMotor] = -63;
		motor[backRightMidMotor] = -63;
		motor[frontLeftMotor] = -63;
		motor[backLeftMotor] = -63;
		motor[backLeftMidMotor] = -63;
	}
}

Now this works, but when I integrate it into a competition template only the forward part works the reverse part never stops when it needs to. This is the code I have for the competition template:

#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1,  rightIME,       sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_2,  leftIME,        sensorNone)
#pragma config(Motor,  port1,           backRightMotor, tmotorVex393HighSpeed, openLoop)
#pragma config(Motor,  port2,           backRightMidMotor, tmotorVex393HighSpeed, openLoop, reversed)
#pragma config(Motor,  port3,           frontRightMotor, tmotorVex393HighSpeed, openLoop, reversed, encoder, encoderPort, I2C_1, 1000)
#pragma config(Motor,  port4,           backLeftMidMotor, tmotorVex393HighSpeed, openLoop, reversed)
#pragma config(Motor,  port5,           topLeft,       tmotorVex393, openLoop)
#pragma config(Motor,  port6,           frontLeftMotor, tmotorVex393HighSpeed, openLoop, reversed, encoder, encoderPort, I2C_1, 1000)
#pragma config(Motor,  port7,           topRight,      tmotorVex393, openLoop)
#pragma config(Motor,  port8,           intakeLeft,    tmotorVex393, openLoop)
#pragma config(Motor,  port9,           intakeRight,   tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port10,          backLeftMotor, tmotorVex393HighSpeed, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!

/////////////////////////////////////////////////////////////////////////////////////////
//
//                          Pre-Autonomous Functions
//
// You may want to perform some actions before the competition starts. Do them in the
// following function.
//
/////////////////////////////////////////////////////////////////////////////////////////

void pre_auton()
{
  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
  bStopTasksBetweenModes = true;

	// 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.
//
/////////////////////////////////////////////////////////////////////////////////////////

task autonomous()
//{
  // .....................................................................................
  // Insert user code here.
  // .....................................................................................

{
	wait1Msec(2000);

	//Clear the encoders associated with the left and right motors
	nMotorEncoder[frontRightMotor] = 0;
	nMotorEncoder[frontLeftMotor] = 0;

	//While less than 1000 encoder counts of the right motor
	while(nMotorEncoder[frontLeftMotor] < 500)
	{
		//Move forward at half power
		motor[frontRightMotor] = 63;
		motor[backRightMotor] = 63;
		motor[backRightMidMotor] =  63;
		motor[frontLeftMotor] =  63;
		motor[backLeftMotor] = 63;
		motor[backLeftMidMotor] =  63;
	}
  //Clear the encoders associated with the left and right motors
	nMotorEncoder[frontRightMotor] = 0;
	nMotorEncoder[frontLeftMotor] = 0;

	//While less than 1000 encoder counts of the right motor
	while(nMotorEncoder[frontRightMotor] > -1000)
	{
		//Move in reverse at half power
		motor[frontRightMotor] = -63;
		motor[backRightMotor] = -63;
		motor[backRightMidMotor] = -63;
		motor[frontLeftMotor] = -63;
		motor[backLeftMotor] = -63;
		motor[backLeftMidMotor] = -63;
	}
}
	//AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.
//}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 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.
//
/////////////////////////////////////////////////////////////////////////////////////////

task usercontrol()
{
	// User control code here, inside the loop

	while (true)
	{
	  // This is the main execution loop for the user control program. Each time through the loop
	  // your program should update motor + servo values based on feedback from the joysticks.

	  // .....................................................................................
	  // Insert user code here. This is where you use the joystick values to update your motors, etc.
	  // .....................................................................................

	  UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
	}
}

I do not know what I am doing wrong, I would appreciate if someone could help me out. Thank You.*

In the original program, when the main task exits the programs stops and ROBOTC automatically stops the motors. In the competition template, the main task is always running (it’s in the Vex_Competition_Includes.c file), so the autonomous task exits but you have not stopped the motors, modify it as follows.

task autonomous()
{
    //Clear the encoders associated with the left and right motors
    nMotorEncoder[frontRightMotor] = 0;
    nMotorEncoder[frontLeftMotor] = 0;

    //While less than 1000 encoder counts of the right motor
    while(nMotorEncoder[frontLeftMotor] < 500)
        {
        //Move forward at half power
        motor[frontRightMotor]   = 63;
        motor[backRightMotor]    = 63;
        motor[backRightMidMotor] = 63;
        motor[frontLeftMotor]    = 63;
        motor[backLeftMotor]     = 63;
        motor[backLeftMidMotor]  = 63;
        }
        
    //Clear the encoders associated with the left and right motors
    nMotorEncoder[frontRightMotor] = 0;
    nMotorEncoder[frontLeftMotor] = 0;

    //While less than 1000 encoder counts of the right motor
    while(nMotorEncoder[frontRightMotor] > -1000)
        {
        //Move in reverse at half power
        motor[frontRightMotor]   = -63;
        motor[backRightMotor]    = -63;
        motor[backRightMidMotor] = -63;
        motor[frontLeftMotor]    = -63;
        motor[backLeftMotor]     = -63;
        motor[backLeftMidMotor]  = -63;
        }

    // Stop motors
    motor[frontRightMotor]   = 0;
    motor[backRightMotor]    = 0;
    motor[backRightMidMotor] = 0;
    motor[frontLeftMotor]    = 0;
    motor[backLeftMotor]     = 0;
    motor[backLeftMidMotor]  = 0;
}

You also do not need the 2 second delay at the beginning, it just wastes 2 seconds of your 15 second autonomous period.

Alright thank you very much :slight_smile: