program getting stuck

I’m taking the course and tutorials on robotc. Currently trying to run the basketball drill w/ encoders with autostraight. It think it’s getting stuck on running forward for 4’ and not progressing to the end of the program.:frowning:
Basketball drill with encoders.c (3.1 KB)


	while(nMotorEncoder[rightMotor] < 975) //move forward 2'

 if (SensorValue[leftMotor] > SensorValue[rightMotor])
	{
		motor[rightMotor] = normalSpeedforward;
		motor[leftMotor] = slowSpeedforward;
	}

This is one of the problems that you are running into, and the one that is giving you this first issue. Remember, the while loops need to have their own set of curly braces {} to encapsulate the code that you want to loop; without these braces, the loop will only run the very next line of code that it sees, which is the if (SensorValue[leftMotor] > SensorValue[rightMotor]) statement. Essentially, the program is running this:

while(nMotorEncoder[rightMotor] < 975) //move forward 2'
     {
        if (SensorValue[leftMotor] > SensorValue[rightMotor])
	 {
		motor[rightMotor] = normalSpeedforward;
		motor[leftMotor] = slowSpeedforward;
	 }
     }

Since both of the encoders are (correctly) cleared before this while loop executes, both will have a value of 0. 0 will never be less than 0, so the motors are not turned on, the encoder values will not change, and the loop will keep checking to see if 0 is less than 0 indefinitely.

There are a couple of other issues with the code, but instead of giving you the exact answers I’d rather give a couple of tips first:

  • Remember, comments are your best friend; they allow you to keep track of what the program is doing, when it is doing it, etc
  • Paired punctuation is key!
  • Check the error pane at the bottom of ROBOTC (once the program is compiled) to see if there are any issues with the code, and work through each error and warning one at a time. Make one change, recompile, check to see if that resolves the issue, and repeat.

Good luck, and let us know if you run into any other issues!

I took the autostraight commands out and wrote void statements for forward and backward and it worked well. I was going to try adding autostraight to one of the void statements and try it that way but haven’t got there yet.