16x2 LCD formatting question

So this is a bit screwy using this 16x2 LCD. I want to display the temperature in the following format (for example): 78.35 degrees.

I’m using a Dallas One Wire thingy - and I get good data using the serial monitor. For example, when I open the serial monitor, I get the following: 78.35. Perfect.

Using the 16x2 display, when I use the code: lcd.print(temperature), I get: 78

Now, I realize, that for the 16x2 display, I have to put my own “.” between the first two (or three) digits, followed by a lcd.print(“.”) and then the last two digits.

My question, is how do I get the just the last two digits from my variable “temperature”?

I envision the code to be something like this:

 lcd.print("Temp is: ");
lcd.print(temperature);
lcd.print(".");
lcd.print(temperature); // just need the last two digits of the variable here

and a little degree symbol would be nice too!

Thanks!

I want to display the temperature in the following format (for example): 78.35 degrees.

That's unrealistic for a sensor that is accurate to +/- 0.5 degrees.

I envision the code to be something like this:

Why? I'd envision it to be:

lcd.print(temperature, 2); // assumes that temperature is a float, which then gets printed to 2 decimal places

On my LCD the character 0xDF comes close to a degree symbol. :wink:

lcd.printByte(0xDF);

Got the degree symbol, that's 337. As far as the accuracy, yah, I suppose you're right, so let's just get it to a tenth. Lemme try your suggestion, although I think I already tried this.

mattress67: Got the degree symbol, that's 337.

Your display understands 9 bit values?

First thing is first. I triedlcd.print(temperature,2). I get “1001101”

as far as the degree symbol, I successfully used:

lcd.print(337F). That gave me the degree symbol.

So, I still do not have the solution for the decimal display.

This should work at least for positive values.

int val = temperature * 100;
int dec = val % 100;

    lcd.print(val / 100);
    lcd.printByte('.');
    if (dec < 10) {
      lcd.printByte('0');
    }
    lcd.print(dec);
    lcd.printByte(0xDF);

First thing is first. I tried

Someday, I imagine you'll post ALL of your code, so we can see what types temperature and lcd are.

mattress67: as far as the degree symbol, I successfully used:

lcd.print(337F)

. That gave me the degree symbol.

it gave me:

"error: invalid suffix "F" on integer constant invalid suffix "F" on integer constant"

You're right,

lcd.print("\337F");

Anyhow, we are definitely getting closer - the last code gave me: 78.00 - but the serial monitor had 78.35

:)

mattress67: lcd.print("\337F");

This adds an additional 'F' to the display.

\337 is octal and the same as 0xDF or 223 or 0b11011111

I like my lcd.printByte(0xDF);

@mattress, can you give us your code. I think we can get this straightened out if we can see the code.

Actually, that gave me "78.00223." So, I switched back to what I had for degree symbol. Anyhow, still not working as I am just getting 78.00 for values other than .00

The following code works.

void lcd_2_dot_2_degrees(float inFloat) {
int head, tail;

    inFloat = inFloat * 100;
    head = inFloat;
    tail = head % 100;
    head = head / 100;
    if (abs(head)<10) {
      lcd.printByte(' ');
    }
    lcd.print(head);
    lcd.printByte('.');
    if (tail < 10) {
      lcd.printByte('0');
    }
    lcd.print(tail);
    lcd.printByte(0xDF);
}

[quote author=jack wp date=1441751328 link=msg=2389419] @mattress, can you give us your code. I think we can get this straightened out if we can see the code.

[/quote]

My code exceeds maximum length. Most of the code is a motion clock, and it works great. I was just adding the temp sensor. And I agree, two decimal points exceeds tolerance, but I'd like to at least have one decimal point.

So now you want “22.8°”?

void lcd_2_dot_1_degrees(float inFloat) {
int head, tail;

    inFloat = inFloat * 10;
    head = inFloat;
    tail = head % 10;
    head = head / 10;
    if (abs(head)<10) {
      lcd.printByte(' ');
    }
    lcd.print(head);
    lcd.printByte('.');
    lcd.print(tail);
    lcd.printByte(0xDF);
}

Well, got it to work. Thanks for the snippet of code.

When I used the

temperature=(sensors.getTempFByIndex(0));
int val = temperature*100;
  • that didn't work. For some strange reason, I had to use int val = (sensors.getTempFByIndex(0)) * 100

In other words, when I used my variable in the *100, I kept getting values ending in 00 (81.29=8100). Only when I replaced my variable did I get the proper 8129, which the snippet could then work with.

Thanks all.

Probably because temperature was defined as int or long.

You choose to hide your code, so nobody could know or even guess that.

I’m laughing as I write this, but yes, I defined it as an int.

I tried to include the full code as I’ve seen many before me reprimanded for not doing the same, but it exceeded the message limitation (9000), and I didn’t know what to include or not include.

How should have I defined my variable?

:slight_smile:

You can attach the whole .ino :-)

mattress67: How should have I defined my variable?

Obviously not as an int, long, byte, (you tested the effect of using ints by getting the .00).

Read my snippet (it works) what does it take as parameter? Take a guess. ;)

As a basic rule:

Define variables with the minimal range that fit your needs,

| type | minimum value | maximum value | | - | - | - | | __________ | __________ | __________ | | char | -128 | 127 | | byte | 0 | 255 | | int | -32768 | 32767 | | unsigned int | 0 | 65535 | | long | -2147483648 | 2147483647 | | unsigned long | 0 | 4294967295 |

Avoid floats, whenever possible, its much much slower than integer arithmetic.

If you need fractional parts, use 1/10 1/100 1/1000 units maintained in an int/long.