While loop updating

My while loop does not update so I’m missing something.

sensor is a 10K pot wired to A0.
phtemp is a variable set to 175, but it could be any value 0-1023.

int val = analogRead(sensor);
  while(val < phtemp)
  {
    digitalWrite(ledred, HIGH);
    lcd.setCursor(0,0);
    lcd.print(val);
  }

The while loop does not change (or react) to val >= phtemp… a hint would be much appreciated.

My while loop does not update so I’m missing something.

Yes, you are. You are missing code to update the values.

int val = analogRead(sensor);
  while(val < phtemp)
  {
    digitalWrite(ledred, HIGH);
    lcd.setCursor(0,0);
    lcd.print(val);
  }

Nothing in the loop changes the value in val. You need another call to analogRead() in the body of the loop.

You're not updating val within the while loop.

I suggest you read my While Loop tutorial: http://hacking.majenko.co.uk/the-while-loop

Thanks guys.

Majenko, your tut is excellent, thanks for that.

I had tried the following but it didn’t work:

  int val = analogRead(sensor);
  while(val < phtemp)
  {
    int val = analogRead(sensor);
    lcd.setCursor(0,0);
    lcd.print(val1);
  }

so I guess “int” again was redefinition of the variable instead of the desired existing variable update?

This works…

  int val = analogRead(sensor);
  while(val < phtemp)
  {
    val = analogRead(sensor);
    lcd.setCursor(0,0);
    lcd.print(val1);
  }

Why do you have the while at all ?
Rather finish the loop ( ) …
It will restart immediately…

I’d prefer something like

if(val < phtemp) { 
   //  redled on
}  else  {
   // redled  off 
}

michael_x:
Why do you have the while at all ?
Rather finish the loop ( ) …
It will restart immediately…

I’d prefer something like

if(val < phtemp) { 

//  redled on
}  else  {
  // redled  off
}

Maybe there’s more in the loop() than he’s showing us…

Thanks guys, getting my head around uC control of a toaster oven for SMD work. Control of four zones in a reflow profile, and the ability to adjust time & temperature in each zone.

so I guess "int" again was redefinition of the variable instead of the desired existing variable update?

Yes. You defined another variable of the same name, with different scope. Loose the int to reuse the existing variable.

Maybe there's more in the loop() than he's showing us...

Sure, but that won't happen while he's in the inner while loop ...

As a provoking thesis (not sure about my English) : while() is even worse than delay () ;)

As a provoking thesis (not sure about my English) : while() is even worse than delay ()

Both while() and delay() have there places. Either can be abused, but it certainly seems like more people use delay() where millis() and keeping track of when something last occurred is more appropriate.

Most of the issues with while that get reported here deal with failing to update the continue clause variables inside the body of the statement.

even better is

int val;
do {
    val = analogRead(sensor);
    lcd.setCursor(0,0);
    lcd.print(val1);
} while(val < phtemp);

Then, if you wanted to change the analogRead function to something else to update val, you only have to do it in once place. Although I suppose this changes the code a little bit because setCursor and print are called even if val is >= phtemp

even better is

Only better if you want it to run at least once.

Thanks again for the feedback guys.

I am using "while" as the oven has to heat up from 25 C (ambient) to 175 C (preheat temp). During this time I only need to monitor the temp sensor and update the LCD, which the while loop does nicely.