 # While loop gets stuck

``````const float deltaTheta = PI/8000;
volatile float theta = 0;

void setup()
{
Serial.begin(9600);

lineAndPause();
//  lineAndPauseHalf();
}

void loop() {}

// This first version works. It goes from zero to pi/4, pauses for a second, then goes from pi/4 to pi/2.

void lineAndPause()
{

while(theta < PI/4)
{
Serial.println(theta);
theta += deltaTheta;
}

delay(1000);

while( PI/4 < theta && theta < PI/2 )
{
Serial.println(theta);
theta += deltaTheta;
}
}

// However, the following second version gets stuck at pi/8. I don't understand why. Any insight, please?

void lineAndPauseHalf()
{

while(theta < PI/8)
{
Serial.println(theta);
theta += deltaTheta;
}

delay(1000);

while( (PI/4 + PI/8) < theta && theta < PI/2 )
{
Serial.println(theta);
theta += deltaTheta;
}
}
``````

works for me. reaches 1.57.

what do you expect your code to do after lineAndPause() completes and returns to setup()?
you’re loop() has nothing in it

Um, just uncomment the

``````lineAndPauseHalf();
``````

that is the problem part.

The first function works, yes.

The second function (lineAndPauseHalf) gets “stuck” at 0.79 or pi/4.

In the problem function: at the end of the first while loop, theta is ~Pi/8.

The next while loop runs while theta is greater than Pi/8+Pi/4. It clearly isn't so it never does anything.

Ah, ok–thank you both very much!

Solution

``````  delay(1000);
theta = PI/4 + PI/8;

while( (PI/4 + PI/8) <= theta && theta < PI/2 )
{
Serial.println(theta);
theta += deltaTheta;
}
``````

That second while loop might as well be simply:

``````  while(theta < PI/2 )
``````