Programming Errors

We have been encountering a problem with our while loop if you could please help me.

Sorry about the name of our functions.The problem is that when we try to activate the arm it goes into a continuous loop down like an autonomous.

Ok, so your first problem is that you never stop your motors, once you set them to a power they will stay at that power unless told otherwise. Try using an ‘else’ to stop them. Your second problem is that you’re using while loops to set your power, while the button is held it will stay in that loop and you won’t be able to drive. Try using if statements instead so it will continue with the rest of your code.

Some of your “while” statements should be “if” statements. For example, this

// Arm
while(vexRT[Btn5U] == 1) {
motor[port4] = 75;
}
while(vexRT[Btn5D] == 1) {
motor[port4] = -75;
}

should be be like this

// Arm
if(vexRT[Btn5U] == 1) {
    motor[port4] = 75;
    }
else
if(vexRT[Btn5D] == 1) {
    motor[port4] = -75;
    }
else
    motor[port4] = 0;

I didn’t realize you had posted this when I answered your PM, I’m going to post my reply here so others can learn from it

=========================================
My reply to the PM ethree sent me about this

Hi E3,

I’m tired atm and busy, but I’ll try to help.

This program will never work as written, you’ve got the wrong idea conceptually about how code works.

Saying…


while(vexRT[Btn5D] == 1) motor[port4] = -75;

…does not permanently make the motor on port4 become -75 when the joystick is pressed.

It doesn’t work like that.

A while loop functionally is the following…

while ( conditional ) {
	statement(s)
}

In plan text it reads like this …

while ( this part is true ) {
	do all the stuff here
}

Let’s start with the basics (ignoring the ROBOTC config stuff), we know we need a task main.

task main() {

}

We also know that the code in here should run over and over, so let’s create an infinite loop for our code to live in.

task main() {

	// Loop forever
	while(true) {
	
	}

}

Now you may not know this but the vex motors can only be updated every 20 milliseconds so we shouldn’t run our loop any faster than 20 ms, doing so is just wasteful. So let’s add a delay at the end.

task main() {

	// Loop forever
	while(true) {

		// Pause for 20ms
		wait1Msec(20);

	}

}

Now we have a loop which will run forever, but will do so in 20ms intervals.

Each time the loop runs (every 20ms) we should probably update those first two motors.

task main() {

	// Loop forever
	while(true) {

		// Update motor 2 & 3
		motor[port2] = vexRT[Ch2];
		motor[port3] = vexRT[Ch3];

		// Pause for 20ms
		wait1Msec(20);

	}

}

Now every 20ms motors 2 & 3 are updated to the current joystick values.

Let’s finally handle those button presses, we want to ask if the button is pressed…

task main() {

	// Loop forever
	while(true) {

		// Update motor 2 & 3
		motor[port2] = vexRT[Ch2];
		motor[port3] = vexRT[Ch3];

		// Arm
		if(vexRT[Btn5U] {
			motor[port4] = 75;
		} else if(vexRT[Btn5D]) {
			motor[port4] = -75;
		} else {
			motor[port4] = 0;
		}

		// Lift
		if(vexRT[Btn7U]) {
			motor[port5] = 75;
		} else if(vexRT[Btn7D]) {
			motor[port5] = -75;
		} else {
			motor[port5] = 0;
		}

		// SpinnyBasket
		if(vexRT[Btn6U]) {
			motor[port6] = 75;
		} else if(vexRT[Btn7D]) {
			motor[port6] = -75;
		} else {
			motor[port6] = 0;
		}

		// Intake
		if(vexRT[Btn8U]) {
			motor[port7] = 75;
		} else if(vexRT[Btn8D]) {
			motor[port7] = -75;
		} else {
			motor[port7] = 0;
		}

		// Pause for 20ms
		wait1Msec(20);

	}

}

All of these buttons have to be held to work, a toggle button can be achieved by removing the else clause, the else clause happens when neither button is pressed, it stops the motor - without it the motor will continue to go.

Hope this helped. -Cody