to string

08 Dec 2010

Hi,

I'd like to display the value of AnalogIn on the text lcd.

Afaik, thats a float. lcd.printf wants string.  How to do this?

08 Dec 2010

printf is a so-called formatting function. It's not limited to strings, you can print other values by using so-called format specifiers in the format string (the first parameter). The following will print a float:

float ana_val = my_analog.read();
lcd.printf("Val: %f", ana_val);

On running, %f will be replaced by the value of the float converted to a string. You can use several specifiers, as long as you pass enough parameters to the call.

Some other specifiers you might use: %d - int (decimal), %x - int (hexadecimal), %c - char (as a character), %s - a zero-terminated string/character array (char * or char[]).

See here for more info and examples.

08 Dec 2010

thanks, works great!