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()
{
}
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...
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);
}
}
sensors.requestTemperatures();
tempC = sensors.getTempC(insideThermometer);
...
if(tempC>=26) {
///////////////this can never be true, as the while loop will terminate before you get here.
[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.
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.
[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 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?