# LCD character positioning

A 16x2 display needs to show on line 1 a floating voltage value (between 0,00 and 99,99) and after that a integer temperature 1 value (0 to 999). On line 2 the same but now with first a current (amperage) and then another temperature 2 value. What happens is that when voltage changes between 9,99 and 10,00 the temperature 1 value also shifts position. Same on line 2.

How do I give both temperature readings a fixed position independent of the value of either voltage or current?

Use lcd.setCursor for each of the 4 values.

For neater display where the decimal point always stays in the same place position the cursor to the right by 1 for values less that 10.

Here is a link to a more elegant solution: --> http://liudr.wordpress.com/2012/01/16/sprintf/

Don

Great! Both are nice though I prefer the sprintf solution because it directly commands the number format. Thank you both of you!

I just looked at the sprintf function more in detail and cannot figure out how to define the string as a float.

This is the code for integer: sprintf(buffer, “%02d:%02d:%02d”, hour, min, sec); ///< This has 3 2-digit integers with leading zeros, separated by “:” . The list of parameters, hour, min, sec, provides the numbers the sprintf prints out with.

But if I convert the voltage xx,yy to a string for the buffer I need to be able to tell the buffer there is a decimal point behind the first two digits. And I have no clue how to do this.

Would this be an alternative:

dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

where
floatvar float variable
StringLengthIncDecimalPoint This is the length of the string that will be created
numVarsAfterDecimal The number of digits after the decimal point to print
charbuf the array to store the results

Just thinking loud. Thanks for the feedback.

floresta: Here is a link to a more elegant solution: --> http://liudr.wordpress.com/2012/01/16/sprintf/

Don

Neato, thanks for sharing.

This did it for floating point output on LCD in a fixed posotion regardless of the number of digits before the decimal point:

The following statements convert the floating point value "voltage" to a string 7 charachters long, with the voltage value taking 5 (2 before the decimal point, the decimal point and 2 after the decimal point). The result is displayed on the LCD in a fixed position, regardslees whether the value is 9.99 or 10.00.

static char outvolt[7]; dtostrf(voltage,5,2, outvolt); lcd.print(outvolt);

@brice3010

As you found sprintf() does not work on the Arduino for floats although it will compile without an error :( , you will find it prints "?"

The dtostrf() solution is good, although using it immediately makes your compiled sketch size ~1500 bytes bigger! Not a problem on small sketches though.

My choice in this cases is a bit different... if you know that you only need 2 decimal, you don't need to use float at all, just use an unsigned long and multiply your value x 100. Es. :

``````int voltagevalue = analogRead(A6);                 reads analog in A6
unsigned int volt = map(voltagevalue,0,1023,0,3000);
sprintf_P(buf,PSTR("%02d.%02d"),volt / 100,volt % 100);
``````

Ciao, Ale.

ilguargua: My choice in this cases is a bit different... if you know that you only need 2 decimal, you don't need to use float at all, just use an unsigned long and multiply your value x 100. Es. :

``````int voltagevalue = analogRead(A6);                 reads analog in A6
unsigned int volt = map(voltagevalue,0,1023,0,3000);
sprintf_P(buf,PSTR("%02d.%02d"),volt / 100,volt % 100);
``````

Ciao, Ale.

Caveat: negative numbers. E.g. -1234 will print as -12.-34

so:

``````sprintf_P(buf,PSTR("%02d.%02d"),volt / 100, abs(volt % 100));
``````