LCD display temperature RIGHT ALIGN

HI!

On my LCD 20*4 print 4 temperature value.

I wish that the numbers are at the end of a series aligned ( decimal dot in same place )

I have problem with temperature less than 10...

12.4
55.3
60.9
2.3

Now i use code :

 lcd1.setCursor(0, 0);
    lcd1.print("SENSOR 2: ");//IME
    lcd1.setCursor(16, 0);
    lcd1.print(temp2,1)

Can you help me ?

( picture in attach.. )

If the temperature is less than 10, print a space first.

code for this ?

A little bit of creative thinking :wink: How do you test if something is below a certain value? 'if' statement comes to mind :wink:

Try and if you get stuck, show your attempt and we point out where you go wrong.

OK ... something I've been working on this, I will continue.

Must be at function "IF" always function ELSE or is not necessary ?

like this :

lcd1.setCursor(11, 1);
    lcd1.print("Pov:");// IME
    lcd1.setCursor(16, 1);
    if (temp4 <10);
    lcd1.print(" ");
    lcd1.print(temp4,1);

( I do not have the screen in the office to see result )

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).

This means that it work in both cases ? ( without semicolon ) ?

I have little trouble with this mathematical characters ,
like semicolon and parenthesis various forms of.....
:confused: :sleeping:

Then what is the charm with curly brackets in second example ? :o :o

As soon as I get home I will try and report .....

Thankyou!

regards

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
}

OK.... I hope i understand...

And how to solve the case in example with ds18b20 temp. sensors,
i do not want to print -127 ( if not connect )

if (temp2 = -127) {
    int temp2 = 0
    }
    lcd.setCursor(0, 0);
    lcd.print("DNEVNA: V");
    lcd.setCursor(9, 0);
    lcd.print(h);

this work, but i want to write in LCD "N/A"

"int temp2 = "n/A" . not work

svedr:

if (temp2 = -127) {

I think you mean this instead:

if (temp2 == -127) {

Use = (single equals sign) to change a value.

Use == (double equals sign) to examine a value.

"int temp2 = "n/A" . not work

That is because you cannot assign a string to an int. If temp2 equals -127 then simply print the string rather than assigning it to a variable.

@svedr

Please help us to help you: please post your entire sketch.

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
snprintf(myBuffer, sizeof(myBuffer), "Sensor1:%9d.%02d", (int)myTemperature, int(myTemperature * 100.0) % 100);

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.

THANKYOU all :smiley:

I use this in code:

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 !! :wink:

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..

Regards

.... any suggestions?

i have 8 sensors, I need to add this line for each sensors,
or is there a shortcut for all sensors?

.... any suggestions?

Instead of repeating the code put it into a function and call that when required.

If you want the temperature to display differently for some ranges of temperature then include code for that in the function too.