How to display a number using LCDI2C4Bit.h library

Hi,

How can I display a number (reading sensor 0-1024) using LCDI2C4Bit.h library?
function printIn() - let me print a string, so meaby I should just convert int to string? But I do not know how to do it either.

one more question:

I have 2x16 display.
I want on the first row print “Hello”, on the sesond “World”.
I thought I should do it like this:

lcd.cursorTo(0,0);
lcd.printIn(“Hello”);
lcd.cursorTo(1,0);
lcd.printIn(“World”);

but there is only “Hello” on the first row…

Thanks for help!

You can use the function itoa() to convert from integer to string. Something like this:

void loop()
{
  ...
  int intvalue = 512;
  char stringvalue[5];  // since your value is only 4 digits long
  
  itoa(intvalue, stringvalue, 10);
  ...
}

itoa function prototype is:

char *itoa( int value, char *string, int radix );

where value is the integer to convert (source), string is the destination and radix the numeric base (10 for decimal).

The second line of an LCD is often not next to the first in its memory map so try:-
lcd.cursorTo(1,16);
lcd.printIn(“World”);

or

lcd.cursorTo(1,32);
lcd.printIn(“World”);

or

lcd.cursorTo(1,48);
lcd.printIn(“World”);

and so on.

or even:-

lcd.cursorTo(2,0);
lcd.printIn(“World”);

or

lcd.cursorTo(3,0);
lcd.printIn(“World”);
or
lcd.cursorTo(4,0);
lcd.printIn(“World”);

and so on.

The second line of an LCD is often not next to the first in its memory map so try:-
lcd.cursorTo(1,16);
lcd.printIn(“World”);

It works, thanks!
Function itoa() works too. Another question… How to display a float number?

[… Another question… How to display a float number?

Have a look at the code at the end of this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548

How to display a float number?

the function is ftoa(). But I think floating point library would make your compiled program bigger.

Thanks a lot!

How to display a float number… the function is ftoa().

I didn’t see ftoa in stdlib.h when I went looking for this functionality in the avr gcc code distributed with the arduino. Where is it?

How to display a float number… the function is ftoa().

I didn’t see ftoa in stdlib.h when I went looking for this functionality in the avr gcc code distributed with the arduino. Where is it?

Yes, checking out I found that avr-gcc doesn’t support ftoa(). You could use then sprintf() like here:

void loop()
{
  ...
  float floatvalue = 5.12;
  char stringvalue[10];
  
  sprintf(stringvalue, "%.2f", floatvalue);  // %.2f means to round decimal places to 2 digits. If you don' want to round then use %f
  ...
}

How to display a float number… the function is ftoa().

I didn’t see ftoa in stdlib.h when I went looking for this functionality in the avr gcc code distributed with the arduino. Where is it?

Yes, checking out I found that avr-gcc doesn’t support ftoa(). You could use then sprintf() like here:

void loop()

{
 …
 float floatvalue = 5.12;
 char stringvalue[10];
 
 sprintf(stringvalue, “%.2f”, floatvalue);  // %.2f means to round decimal places to 2 digits. If you don’ want to round then use %f
 …
}



Have you tried these things you're suggesting? I don't think floating point is supported in sprintf either. Anyway, why use sprint which brings in almost 2k of code when there is a much smaller and easy to use function as per post #4 above.