Program not working with competition correctly

If i hook my robot into my competition switch first then turn the power on the robot will spin in circles but if i turn everything on and then plug the competition switch in it runs fine, however, on a competition field if i try to plug things in the way it works it cycles power on my controller and then my program wont do what its supposed to do. Here is my program:

#pragma config(Sensor, dgtl1, solenoid1, sensorDigitalOut)
#pragma config(Sensor, dgtl2, solenoid2, sensorDigitalOut)
#pragma config(Sensor, dgtl3, solenoid3, sensorDigitalOut)
#pragma config(Sensor, dgtl4, solenoid4, sensorDigitalOut)
//!!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()
{
motor[port2] = 127; //currently in red
motor[port4] = 127;
motor[port6] = 127;
motor[port8] = 127;
motor[port5] = 127;
motor[port9] = 127;
wait1Msec(1100);
motor[port2] = -127;
motor[port4] = -127;
motor[port6] = 127;
motor[port8] = 127;
motor[port5] = -127;
motor[port9] = 127;
wait1Msec(300);
motor[port2] = 0;
motor[port4] = 0;
motor[port6] = 0;
motor[port8] = 0;
motor[port5] = 0;
motor[port9] = 0;
wait1Msec(500);
motor[port2] = 127;
motor[port4] = 127;
motor[port6] = 127;
motor[port8] = 127;
motor[port5] = 127;
motor[port9] = 127;
wait1Msec(500);
motor[port2] = -127;
motor[port4] = -127;
motor[port6] = -127;
motor[port8] = -127;
motor[port5] = -127;
motor[port9] = -127;
wait1Msec(500);
motor[port2] = 127;
motor[port4] = 127;
motor[port6] = -127;
motor[port8] = -127;
motor[port5] = 127;
motor[port9] = -127;
wait1Msec(300);
motor[port2] = -127;
motor[port4] = -127;
motor[port6] = -127;
motor[port8] = -127;
motor[port5] = -127;
motor[port9] = -127;
wait1Msec(1100);
motor[port2] = 0;
motor[port4] = 0;
motor[port6] = 0;
motor[port8] = 0;
motor[port5] = 0;
motor[port9] = 0;
wait1Msec(500);

// …
// Insert user code here.
// …

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

bMotorFlippedMode[port6] = true;
bMotorFlippedMode[port8] = true;
bMotorFlippedMode[port9] = true;

motor[port2] = vexRT[Ch3];
motor[port4] = vexRT[Ch3];
motor[port6] = vexRT[Ch2];
motor[port8] = vexRT[Ch2];
motor[port5] = vexRT[Ch3];
motor[port9] = vexRT[Ch2];

if(vexRT[Btn6U] == 1)
{

motor[port3] = 127;
motor[port7] = -127;

}
else if(vexRT[Btn6D] == 1)
{

motor[port3] = -127;
motor[port7] = 127;
}
else
{

motor[port3] = 0;
motor[port7] = 0;
}

if(vexRT[Btn5U] == 1)
{
motor[port1] = -127;
motor[port10] = 127;

}
else if(vexRT[Btn5D] == 1)
{
motor[port1] = 127;
motor[port10] = -127;

}
else
{
motor[port1] = 0;
motor[port10] = 0;

if(vexRT[Btn8U] == 1)
{
SensorValue[solenoid1] = 1;
SensorValue[solenoid2] = 1;

}
else if(vexRT[Btn8U] == 0)
{
SensorValue[solenoid1] = 0;
SensorValue[solenoid2] = 0;

}
else
{
SensorValue[solenoid1] = 0;
SensorValue[solenoid2] = 0;// Remove this function call once you have “real” code.
}

if(vexRT[Btn7U] == 1)
{
SensorValue[solenoid3] = 1;
SensorValue[solenoid4] = 1;

}
else if(vexRT[Btn7U] == 0)
{
SensorValue[solenoid3] = 0;
SensorValue[solenoid4] = 0;

}
else
{
SensorValue[solenoid3] = 0;
SensorValue[solenoid4] = 0;// Remove this function call once you have “real” code.
}
}
}
}

There are a couple of main issues that I am seeing with the code.

First, make sure to set up the motors in the Motors and Sensors Setup window to match what type of motor you are using on each port. This will ensure that the correct motors types are being used on each port and is a good practice to follow.

Also, which mode are you running when you have the competition control switch plugged into the robot and enabled? You’ve manually reversed three of the motors in the User Control section of the code, but have not reversed them in the Autonomous portion of your code:

bMotorFlippedMode[port6] = true;
bMotorFlippedMode[port8] = true;
bMotorFlippedMode[port9] = true;

This is another good reason to use the Motors and Sensors Setup window for configuring your robot, as it has a ‘Reversed’ checkbox for that can be used to reverse the motors for the entire program.

One more thing to note:

		if(vexRT[Btn5U] == 1)
		{
			motor[port1] = -127;
			motor[port10] = 127;


		}
		else if(vexRT[Btn5D] == 1)
		{
			motor[port1] = 127;
			motor[port10] = -127;

		}
		else
		{
			motor[port1] = 0;
			motor[port10] = 0;

The closing brace for this ‘else’ statement is not found until the very end of the program; this means that the solenoid control will only be checked when buttons 5U or 5D are not pressed. I’m not sure if this is intentional or not, but wanted to let you know just in case it isn’t.