Hard time with while loop

I am having a problem getting the following to work. In the setup I am trying to test if the temp is < or > 27c. I was hoping to get the system to hold until the temp is <27 and then move on. I just cant seem to get it right. I have exhausted my very limited coding skills and Googling. Thanks for any help.

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
int numberOfDevices; // Number of temperature devices found



void setup()
{
  pinMode(13, OUTPUT);
  // start serial port
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  // Start up the library
  sensors.begin();

  oneWire.reset_search();


  // assigns the first address found to insideThermometer
  while (!sensors.getAddress(insideThermometer, 0))    
  {
    lcd.setCursor(0,0);
    lcd.print("Condensor Therm ");
    lcd.setCursor(0,1);
    lcd.print("Not Found      ");

  }

  lcd.print("Condensor Therm ");   
  lcd.setCursor(0,1);
  lcd.print("Online");    

  delay(3000);
  
    // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
   sensors.requestTemperatures();
   float tempC = sensors.getTempC(insideThermometer);
    


//THIS IS MY PROBLEM AREA

 while(sensors.getTempC(insideThermometer), tempC < 27){
     
    lcd.clear();
    lcd.print("Condensor NOT");
    lcd.setCursor(0,1);
    lcd.print("Ready");
    lcd.setCursor(9,1);
    lcd.print(sensors.getTempCByIndex(0));
    lcd.setCursor(15,1);
    lcd.print("C");
   
    }
    
  if(tempC >= 27) {
  lcd.clear();
  lcd.print("Condensor       ");
  lcd.setCursor(0,1);
  lcd.print("Ready");
  lcd.setCursor(9,1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.setCursor(15,1);
  lcd.print("C");
  }
  
  delay(2000);
  lcd.clear();
  lcd.print("Vacuum Pump");
  lcd.setCursor(0,1);
  lcd.print("Starting in");
  
  for (int i=9; i>-1; i--){
      lcd.setCursor(13,1);
      lcd.print(i);
      delay(1000);
  }
      digitalWrite(13, HIGH);
  }

  


  void loop()
{ 
  }
 while(sensors.getTempC(insideThermometer), tempC < 27){

That line does not put anything into tempC.

Thanks Nick.
The line above doesn't help with that?
float tempC = sensors.getTempC(insideThermometer);

Not really because you are not reading it in the loop. It will either be true forever or false forever.

You might write:

 while(tempC = sensors.getTempC(insideThermometer), tempC < 27){

However I don't recommend that coding style, it is abusing the comma operator.

Better would be:

   float tempC = sensors.getTempC(insideThermometer);

 while(tempC < 27){
     
    lcd.clear();
    lcd.print("Condensor NOT");
    lcd.setCursor(0,1);
    lcd.print("Ready");
    lcd.setCursor(9,1);
    lcd.print(sensors.getTempCByIndex(0));
    lcd.setCursor(15,1);
    lcd.print("C");
    tempC = sensors.getTempC(insideThermometer);  // read again
    }

Unfortunately neither of those suggestions worked. The first basically hung it up and the display would not update. The second method would allow for the temps to update but once the temp had passed 27C it still did not exit and move forward. I am working on what you said at first that nothing is put into tempC. Not there yet...

Please post your amended code.

Thanks for the kelp Nick. With your suggestions I was finally able to get it going. Now on to the next step.

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
int numberOfDevices; // Number of temperature devices found


float tempC;

void setup()
{
  pinMode(13, OUTPUT);
  // start serial port
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  // Start up the library
  sensors.begin();

  oneWire.reset_search();


  // assigns the first address found to insideThermometer
  while (!sensors.getAddress(insideThermometer, 0))    
  {
    lcd.setCursor(0,0);
    lcd.print("Condensor Therm ");
    lcd.setCursor(0,1);
    lcd.print("Not Found      ");

  }

  lcd.print("Condensor Therm ");   
  lcd.setCursor(0,1);
  lcd.print("Online");    

  delay(3000);
  
    // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
   
   sensors.requestTemperatures();
   tempC = sensors.getTempC(insideThermometer);
   lcd.clear();   
   
 while(tempC < 26){
    sensors.requestTemperatures();
    tempC = sensors.getTempC(insideThermometer);
    lcd.print("Condensor NOT");
    lcd.setCursor(0,1);
    lcd.print("Ready");
    lcd.setCursor(9,1);
    lcd.print(tempC);
    Serial.println(tempC);
    lcd.setCursor(15,1);
    lcd.print("C");
       if(tempC>=26) {  
              lcd.clear();
              lcd.print("Condensor       ");
              lcd.setCursor(0,1);
              lcd.print("Ready");
              lcd.setCursor(9,1);
              delay(3000);
            }
           }
           
 if(tempC>=26) {  
  lcd.print("Condensor       ");
  lcd.setCursor(0,1);
  lcd.print("Ready");
  lcd.setCursor(9,1);
  lcd.print(tempC);
  lcd.setCursor(15,1);
  lcd.print("C");
  }
  
  delay(3000);
  lcd.clear();
  lcd.print("Vacuum Pump");
  lcd.setCursor(0,1);
  lcd.print("Starting in");
  
  for (int i=9; i>-1; i--){
      lcd.setCursor(13,1);
      lcd.print(i);
      delay(1000);
  }
      digitalWrite(13, HIGH);
  }

  
//void getCondTemp(DeviceAddress deviceAddress){
//     
//   sensors.requestTemperatures();
//   float tempC = sensors.getTempC(insideThermometer);
//   }

  void loop()
{ 
  }
 while(tempC < 26){
    sensors.requestTemperatures();
    tempC = sensors.getTempC(insideThermometer);
    lcd.print("Condensor NOT");
    lcd.setCursor(0,1);
    lcd.print("Ready");
    lcd.setCursor(9,1);
    lcd.print(tempC);
    Serial.println(tempC);
    lcd.setCursor(15,1);
    lcd.print("C");
       if(tempC>=26) {  
///////////////this can never be true, as the while loop will terminate before you get here.
              lcd.clear();
              lcd.print("Condensor       ");
              lcd.setCursor(0,1);
              lcd.print("Ready");
              lcd.setCursor(9,1);
              delay(3000);
            }
           }
while(tempC < 26){

sensors.requestTemperatures();
    tempC = sensors.getTempC(insideThermometer);
...
      if(tempC>=26) { 
///////////////this can never be true, as the while loop will terminate before you get here.

No, because he re-reads the temperature.

[quote author=Nick Gammon date=1416636233 link=msg=1971522]
No, because he re-reads the temperature.[/quote]
Is the temperature likely to change in the few microseconds between the two reads?
Any longer than that and the program will be going round loop() again and avoid the while loop altogether, as it's condition will be false. So, even though it's theoretically possible to catch a change of temperature there, it's highly unlikely.

have you got a heater you can turn on inside that loop. Perhaps then the temperature will actually change while you're waiting.

Is the temperature likely to change in the few microseconds between the two reads?

Absolutely, but you can't say it will "never" be true, but it is unlikely to be true.

It has to change sometime, right? And therefore it might be one microsecond at one temperature, and then a microsecond later it will be a different temperature.

[quote author=Nick Gammon link=msg=1972578 date=1416728646]
It has to change sometime, right?
[/quote]It would change even quicker if he just turned the heater on. :slight_smile:

KenF:
It would change even quicker if he just turned the heater on. :slight_smile:

I see that this is turning into a hot topic. ;D

[quote author=Nick Gammon date=1416728646 link=msg=1972578]
It has to change sometime, right? And therefore it might be one microsecond at one temperature, and then a microsecond later it will be a different temperature.[/quote]
At the change, it will always be at one temperature, and then a microsecond later it will be a different temperature, but the likelihood of that change occurring between his two reads is mighty slim. I'll concede that I was a little too pessimistic with "will never be true." "Will almost never be true" would be more accurate.

I have not studied the followup posts or codes yet, and I will be doing that shortly, but I do indeed have heaters that will be getting turned on.

I don't really understand why the while loop would terminate before reading the if statement that is within the while loop and I will keep studying that. This is the code that I ended up with. Does it make more sense?

  sensors.requestTemperatures();
  tempC = sensors.getTempC(insideThermometer);
  lcd.clear();   

  //check to see if condensor temp is ready 
  while(tempC < 26){
    sensors.requestTemperatures();
    tempC = sensors.getTempC(insideThermometer);
    lcd.print("Condensor NOT");
    lcd.setCursor(0,1);
    lcd.print("Ready");
    lcd.setCursor(9,1);
    lcd.print(tempC);
    lcd.setCursor(15,1);
    lcd.print("C");
  }


  lcd.clear();
  lcd.print("Condensor       ");
  lcd.setCursor(0,1);
  lcd.print("Ready");
  lcd.setCursor(9,1);
  lcd.print(tempC);
  lcd.setCursor(15,1);
  lcd.print("C");
  condensorReady = !condensorReady;
  delay(3000);