itoa function with floating numbers

Hi i am new to arduino programming. So i m strugling a bit.

I want to print an float value on lcd but as i m using itoa the float is first converted into integer and then that value is printed. So not getting correct results.

Also when i m printing value on lcd and decrease it from say 1840 to 0 it does not show the value 0 but it shows like 056 something like this. The MSB becomes zero but the others retain there vale unless i increse the value from 2 digits to 3 or from 3 digits to 4.

Any help will be appreciated.

Thank You.

I think this library may let you do what you want to do.

http://arduiniana.org/libraries/pstring/

Depending on which lcd library you are using, its print function probably derives from Print so you will be able to use lcd.print() to print the string created by PString. I use it to print floats using print() from the Ethernet class.

I am using the itoa() function for displaying the voltage value on the LCD display. The range of value is 0 to 5000 mV. I am using the character buffer of char[5]. This buffer is sble to print 4 digits and working fine for the values greater than 1000 mV. But as i try to display the value in between 0 to 999mV it still shows some vale at lsb. I mean instead of displaying 808 it will display something like 8086.
Its always displaying four digits evenif the output is 3 digits. I know this is because my buffer size is [5]. But i need that to display 4 digit value.

So anyone has any suggestion on how can i improve the code.

Thanks for any help.

Without seeing your code, it's hard to say for sure but it appears that you need to clear the old data out of your buffer before you write the new data. From your example it looks like you are displaying a remnant of the last data with the new written on top.

Also, your lcd buffers the characters you send it so if you don't rewrite the entire 4 character locations when you update your display, whatever was there previously will remain in the unwritten locations.

Thanks for the reply
This is my code

I m trying to get input from 4 channels and display the corrosponding values continuously. As i told earlier the problem is in displaying the volatge between 0 to 999 mvolts.

#include <LCD4Bit_mod.h>
#include<stdlib.h>

LCD4Bit_mod lcd=LCD4Bit_mod(2);

int val =0;

float ch1=0.0000, ch2=0.0000, ch3=0.0000, ch4=0.0000;
int temp,i;

char val1[5];

void setup()
{
lcd.init();
lcd.clear();
}

void loop()
{

for(i=0;i<4;i++){ // clears the buffer val1[]
val1 = '0';

  • }*

  • val= analogRead(5);*

  • delay(100);*
    _ ch1=(val*4.82);_

  • lcd.cursorTo(1,0);*

  • lcd.printIn("C1");*

  • temp = ch1;*

  • itoa(temp,val1,10);*

  • lcd.cursorTo(1,3);*

  • lcd.printIn(val1);*

  • //delay(500);*

  • for(i=0;i<4;i++){ // clears the buffer val1[]*
    _ val1 = '0';_
    * }*

* val= analogRead(4);*
* delay(100);*
_ ch2=(val4.82);_
_
lcd.cursorTo(1,9);_
_
lcd.printIn("C2");_
_
temp=ch2;_
_
itoa(temp,val1,10);_
_
lcd.cursorTo(1,12);_
_
lcd.printIn(val1);_
_
//delay(500);_
_
for(i=0;i<4;i++){ // clears the buffer val1[]_
_ val1 = '0';
} *_

* val= analogRead(3);*
* delay(100);*
_ ch3=(val4.82);
lcd.cursorTo(2,0);
lcd.printIn("C3");
itoa(ch3,val1,10);
lcd.cursorTo(2,3);
lcd.printIn(val1);
//delay(500);
for(i=0;i<4;i++){ // clears the buffer val1[]
val1 = '0';
}*_

* val= analogRead(2);*
* delay(100);*
_ ch4=(val4.82);
lcd.cursorTo(2,9);
lcd.printIn("C4");
itoa(ch4,val1,10);
lcd.cursorTo(2,12);
lcd.printIn(val1);
//delay(500);*_

* for(i=0;i<4;i++){ // clears the buffer val1[]*
_ val1 = '0';
* }*_

* } *

I looked at the library code and it looks like you need to clear the 4 character positions on the LCD before you write new characters.

lcd.printIn("    ");

Also, your conversion to mV's will be more accurate if you use 4.8876 instead of 4.82.

Thank You very much EmilyJane.

i am really thankful for your help. I was not getting the correct answer at all.

Now it works absolutely fine.

Thanks.

You're welcome. I've made the same mistake when using an LCD display. Nothing like experience. :wink:

Hi EmilyJane,

I am pleased that you stumbled across (and are using) PString. Did I understand that you use it to format strings in memory before sending them to an lcd with lcd.print()?

Mikal

Thanks for contributing such a great library to the community!

I am using PString to format and print strings (of floats) from an Arduino web server i.e. client.print(). I have temperature, humidity, and barometric pressure sensors monitoring "weather" in my loft. :slight_smile:

Did I read somewhere that you intend to allow greater precision (>2) for floats at some future date?

EmilyJane,

Thanks for the kind comments. The reason PString works is that it too derives from class Print. I don't want to steer you away, but if you are using LiquidCrystal (which also derives from Print) to print strings rendered by PString, then it seems to me that you may have an unnecessary step. Why not print the floats directly using the LCD library?

If inheriting from Print is PString's strength, it is also its weakness. Even though there is code in Print to print a float with arbitrary precision, the current implementation does not take advantage of it. Until the Print class is changed to allow users to define what precision they want, there is nothing I can do in PString to give you access to anything but 2 digits.

I hope that this changes in the future. It would not be difficult and would provide a great benefit at low cost.

Mikal

Mikal,

Thanks for the correction.

I'm new to the Arduino environment and at first glance at the reference for Print, since it doesn't list float as a type that data can be,

data: the data to print (char, byte, int, long, or string)

I incorrectly assumed it could not. Now that I have looked at the library code and have run a short test program I see what you are saying.

Regardless, your PString library is a great addition to the Arduino environment. :slight_smile:

Interesting observation. It looks like the documentation for Serial::print[ln] and LiquidCrystal::print[ln] need to be updated. Support for float printing was added to both of these a few versions ago.

Maintainers-- it looks like "ordinary civilians" are not empowered to modify these pages.