Program does not end

I have a problem with my autonomous code. It involves the use of a lot of while() loops that the compiler says will not end. I heard you might have to put an extra condition in the loop such as “If the motor is not on…”(In code). Here’s my code:
while (rightteeth < 4 || leftteeth < 4)/*if robot has not turned four gear tooth counts… */
pwm13 = 0; /*rev left motor */
pwm15 = 255; /*forward right motor, hopefully will turn robot about ten deg’s */
The variables rightteeth and leftteeth are to count inputs from the hall effect sensors.
Please someone help me!

There is nothing actually wrong with the code you’ve shown so far. The most likely reason that the compiler is complaining is that before you enter the loop you set one or other of the variables to something less than 4. I didn’t think that this compiler was that smart, but maybe it realized that you never alter the variables in the loop. However, if you are using interrupts, then this code should be fine. If you could post all your code, that would be extremely helpful.

It may also just be that the compiler is confused. If your code works (have you tested it?), then I’d just ignore the warning.

The compiler is definately right…

You are incorrectly using the while loop. Since you never increment rightteeth or leftteeth inside the loop, it will loop forever This will have the side effect of having your robot run at whatever speed it was running before the loop was hit. This is because the outputs are set at the bottom of the main loop, which will never get hit in your case.

What you probably want to do is use an IF case. The main loop will take care of the iteration.

While loops are tricky in robotics programming because only the code section within the loop runs until it is broken. Since the sensors are read outside of the loop itself, the loop is infinite.

Another implementation might be like this:

if (l teeth < 4 || r teeth < 4)
run motors;
motors = 127;

The question I ask is if the code is meant to turn the robot, why would the left and right sides both count similarly? Are they on the same gear as a redundancy check? The real question I ask is are the sensors on the robot so one is on each side of the robot, or are they positioned differently?

The other comments that have been posted are correct. You’re going to have problems if you use a while loop that way. Your code is being called from within the RC’s own code, and that code has lots of other things to do. If those things don’t get done in time, the RC will shut you down. You need to make sure that each call to your code completes quickly - it only gets about 26 ms to run.

The “if” version in the pseudocode is more in line with what you need. You’ll probably also need a static variable to keep track of which state you are in - starting to turn, working on completing the turn, turn completed, starting to drive, driving, etc.

BTW - The compiler is complaining specifically because you are using a loop whose control statement uses variables whose values aren’t being changed in the loop. It is not aware that you might be changing them in an interrupt routine. (That is where they get updated, right?)