That's the idea except that you end the if statement with a semicolon so the line after it will always be executed, regardless of the result of the condition.
lcd1.setCursor(11, 1);
lcd1.print("Pov:");// IME
lcd1.setCursor(16, 1);
if (temp4 < 10) <--- semi-colon removed
lcd1.print(" ");
lcd1.print(temp4, 1);
And personally I will add curlies as well
lcd1.setCursor(11, 1);
lcd1.print("Pov:");// IME
lcd1.setCursor(16, 1);
if (temp4 < 10)
{
lcd1.print(" ");
}
lcd1.print(temp4, 1);
You do not need an 'else' if there is nothing to do if the condition is not true (as is the case here).
The safest and most consistent way to write if clauses is to always use curly brackets around the code to be executed if the test returns true even if there is only 1 line of code to be executed. It also helps considerably to put each curly brace on its own line so that the code blocks can easily be seen
if (thisIsTrue)
{
//execute the code between the curly braces
}
I made a snippet that might be of use. Here it is:
// function to format a temperature as a character array
void formatTemperature(char * buf, float temp) {
// how many tenths of a degree? (for example, 12.3 degrees is 123 tenths)
int tenths = 10*((temp<0.0)?(temp-0.05):(temp+0.05));
// check for out-of-range temperatures
if ((tenths < -999) || (tenths > 9999)) {
strcpy(buf, " N/A ");
return;
}
// determine the first character (index 0)
if (tenths < 0) {
buf[0] = '-'; // leading minus sign
tenths = -tenths; // turn negative into positive
}
else if (tenths < 1000) {
buf[0] = ' '; // leading space
}
else {
buf[0] = '0'+(tenths/1000); // hundreds digit of temperature
}
// determine the second character (index 1)
if (tenths < 100) {
buf[1] = buf[0]; // this will be a space or a minus sign
buf[0] = ' '; // blank out the first character
}
else {
buf[1] = '0'+((tenths/100)%10); // tens digit of temperature
}
// determine the remaining characters
buf[2] = '0'+((tenths/10)%10); // ones digit of temperature
buf[3] = '.'; // replace the dot with a comma if you wish
buf[4] = '0'+(tenths%10); // tenths digit of temperature
buf[5] = 0; // null terminator for end of string
// we are finished
return;
}
Example usage:
// declare a buffer for temperature output
char myBuffer[7]; // the buffer size MUST be 6 or more
// declare a variable for the temperature
float myTemperature = 37.0; // or however many degrees
// make the function do its thing, then show the result on the LCD
formatTemperature(myBuffer, myTemperature);
lcd.print(myBuffer);
odometer:
I made a snippet that might be of use. Here it is:
// function to format a temperature as a character array
void formatTemperature(char * buf, float temp) {
// how many tenths of a degree? (for example, 12.3 degrees is 123 tenths)
int tenths = 10*((temp<0.0)?(temp-0.05):(temp+0.05));
// check for out-of-range temperatures
if ((tenths < -999) || (tenths > 9999)) {
strcpy(buf, " N/A ");
return;
}
// determine the first character (index 0)
if (tenths < 0) {
buf[0] = '-'; // leading minus sign
tenths = -tenths; // turn negative into positive
}
else if (tenths < 1000) {
buf[0] = ' '; // leading space
}
else {
buf[0] = '0'+(tenths/1000); // hundreds digit of temperature
}
// determine the second character (index 1)
if (tenths < 100) {
buf[1] = buf[0]; // this will be a space or a minus sign
buf[0] = ' '; // blank out the first character
}
else {
buf[1] = '0'+((tenths/100)%10); // tens digit of temperature
}
// determine the remaining characters
buf[2] = '0'+((tenths/10)%10); // ones digit of temperature
buf[3] = '.'; // replace the dot with a comma if you wish
buf[4] = '0'+(tenths%10); // tenths digit of temperature
buf[5] = 0; // null terminator for end of string
// we are finished
return;
}
Example usage:
// declare a buffer for temperature output
char myBuffer[7]; // the buffer size MUST be 6 or more
// declare a variable for the temperature
float myTemperature = 37.0; // or however many degrees
// make the function do its thing, then show the result on the LCD
formatTemperature(myBuffer, myTemperature);
lcd.print(myBuffer);
that's a lot of machinations... Why not just use sprintf or it's safer sibling, snprintf:
float myTemperature = 12.3456;
char myBuffer[21] = ""; // width of LCD plus 1
snprintf(myBuffer, sizeof(myBuffer), "Sensor1:%9d.%02d", (int)myTemperature, int(myTemperature * 100.0) % 100);
lcd1.setCursor(0,0);
lcd1.print(myBuffer); // print the 20 character line all at once
I fail to see the need to use 9 characters before the decimal point to print a value that will never need more than 6 (-32768). Personally, if the temperature fell that low where I was living, I'd move. Or get a new sensor. It is unlikely that any temperature sensor will operate that low.
BulldogLowell:
that's a lot of machinations... Why not just use sprintf or it's safer sibling, snprintf:
float myTemperature = 12.3456;
char myBuffer[21] = ""; // width of LCD plus 1
snprintf(myBuffer, sizeof(myBuffer), "Sensor1:%9d.%02d", (int)myTemperature, int(myTemperature * 100.0) % 100);
lcd1.setCursor(0,0);
lcd1.print(myBuffer); // print the 20 character line all at once
That doesn't handle negative numbers. It also doesn't handle the "N/A" that svedr said s/he wanted.
PaulS:
I fail to see the need to use 9 characters before the decimal point to print a value that will never need more than 6 (-32768).
Apparently, BulldogLowell's intent was to pad out the line to 20 characters.
client.print("Damjan soba: ");
client.print("[");
if (temp3 == -127){
client.print("N/A");
client.println("] °");
client.println("C");
}
else{
client.print(temp3); //print temperature from DS18x20 sensor
client.println("] °");
client.println("C");
}
Work !!
Another question
i have 8 sensors, I need to add this line for each sensors,
or is there a shortcut for all sensors?
i want like this in WEB page (client.print) and Serial.monitor (serial.prind) and LCD (lcd.print)
And how add exception for 85 degrees, the same procedure?
I do not want to print nowhere -127 and 85, I use temp. monitoring and then i have abnormal temp. in graph (85 degrees or -127 degrees)
You know that from the start DS18B20 sensors reports 85 degrees for 1-2 second and -127 if is no connect..