Limit Switch RobotC

Hello, my team is trying to create a program where when we press a button, our launcher arm comes down and stops when it hits a limit switch, however we are running into problems. We have tried various means, we are sure should work, but the launcher never stops. This is the current code we have, which does not work. Our limit switch is plugged into digital port 12.

task intake() {
	while(true) {
		if (vexRT[Btn5U]){
			speed = -127;
			motor[LL1] = speed;
				motor[LL2] = speed;
				motor[LL3] = speed;
				motor[LR1] = speed;
				motor[LR2] = speed;
				motor[LR3] = speed;
			while( SensorValue[launcher] == 1 ) {
				speed = 0;
				motor[LL1] = speed;
				motor[LL2] = speed;
				motor[LL3] = speed;
				motor[LR1] = speed;
				motor[LR2] = speed;
				motor[LR3] = speed;

				if (vexRT[Btn5D]) {
				speed = -127;
				motor[LL1] = speed;
				motor[LL2] = speed;
				motor[LL3] = speed;
				motor[LR1] = speed;
				motor[LR2] = speed;
				motor[LR3] = speed;
				wait1Msec (200);
				speed = 0;
				motor[LL1] = speed;
				motor[LL2] = speed;
				motor[LL3] = speed;
				motor[LR1] = speed;
				motor[LR2] = speed;
				motor[LR3] = speed;

			}
			}

		}
	}

I took your code and broke it out into manageable functions. It should make it easier to read and debug. One thing to note is that both of your speed assignments were -127 so I am not sure if that was causing your issue.

void ArmMotors(int speed)
{
	//Consolidate motor control into one function
	motor[LL1] = speed;
	motor[LL2] = speed;
	motor[LL3] = speed;
	motor[LR1] = speed;
	motor[LR2] = speed;
	motor[LR3] = speed;
}

void  ArmControl()
{
	if(vexRT(Btn5U)){
		ArmMotors(-127); //move motors -
		return; //exit function
	}
	if(vexRT(Btn5D)) {
		//if limit
		if(SensorValue[launcher] == 1) {
			ArmMotors(0); //stop motors
			return;  //exit function
		}
		ArmMotors(127); //move motors +
	}
}

task intake()
{
	while(true) {
		ArmControl();
		wait1Msec (200);
	}
}

My students are working on the same issue.

If your launcher involves elastics (the motors are stretching rubber bands that provide the energy to launch) then setting the motor value to 0 when the switch is pressed is likely to make the launcher arm bounce off the switch. The rubber bands pull the arm just off the switch. Try setting the motors to a low value rather than 0. If your launcher involves a slip gear, then you may be able to add a pawl to the gear that only turns one way, so that the system cannot reverse.

try:

task intake() {
int speed=0; //Creates an integer for speed
	while(true) {
			motor[LL1] = speed;
			motor[LL2] = speed;
			motor[LL3] = speed;
			motor[LR1] = speed;
			motor[LR2] = speed;
			motor[LR3] = speed;
		if (vexRT[Btn5U]){
			speed = -127;
			motor[LL1] = speed;
			if( SensorValue[launcher] == 1 ) {
				speed = 0;
				if (vexRT[Btn5D]) {
				speed = -127;
				wait1Msec (200);
				speed = 0;
			}
			}

		}
	}