Never Exits Do-While Loop

I'm sorry, This has to be really obvious, but I'm just not seeing it.

I saw a similar (much older) thread on this topic, but it didn't seem to have quite the same problem...or did it? Never exits loop to print out the final value, noob question but I'm exhuasted - Programming Questions - Arduino Forum

Anyway, here's my code. It never exits the DO-While loop no matter what I set the voltage to that pin to be. The console will display all values properly between 0 and 5v, and I vary the voltage all across there.

I'm expecting it to exit the DO-While loop whenever the voltage goes above 3 volts, but even at 5 volts, it just stays right where it is.

float voltage = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
//   while (! Serial); // Wait untilSerial is ready - Leonardo


}

void loop() {

do
{
  // read the input on analog pin 1:
  int sensorValue = analogRead(A1);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
 // print out the value you read:
    Serial.println("Inside DO loop");
  Serial.println(voltage);

} while (voltage < 3.00);

  Serial.println("Outside DO Loop");

}

Does anyone have any suggestions?

Thanks,
-AJ

No was not the same issue.

Can you show the output you get from the console ?

Does anyone have any suggestions?

My suggestion is to not define a local variable named "voltage" when you have already defined a global variable named voltage.

 float voltage = sensorValue * (5.0 / 1023.0);

(get rid of the "float").

I'm expecting it to exit the DO-While loop whenever the voltage goes above 3 volts,

According to the code, the loop exits if voltage >= 1.00.

My guess is the Serial print is crashing the uno
Add a delay(100); to test the thery

jremington, you were absolutely right.

Changing

float voltage = sensorValue * (5.0 / 1023.0);

to

voltage = sensorValue * (5.0 / 1023.0);

is exactly what I needed to do. Now it exits the Do-While loop when voltage goes above 3 volts.

Thank you for the fast and kind help!

And thank you all for your other feedback here as well. Much appreciated!

-AJ

so variable scope issue

Interesting to dig deeper though As the global is set to 0 so it should exit right away and because it's in loop it should loop.

That's why seeing the console output would have been interesting - you should have seen the second print message

J-M-L:
As the global is set to 0 so it should exit right away

while (0.0 < 3.0)

Looks like repeat forever to me...

Right - of course !