Question with conversion of int to string and losing a leading zero

I am trying to use an arduino to read voltages on an analog pin and I did some calculations that will convert the ADC value. This uses the T6963c Library to post it to a GLCD.

The problem I am having is as the ADC value rises from the analog pin, I get these segments where a decimal place is incorrect. For example when the LCD reads "1.98V", then "1.99V", then once it goes over the 2V mark what should be 2.01V is read 2.1 (and sometimes 2.1* where the * is an intermittent jittery number because I have the display refreshing this number quickly.) What should be 2.03V is displayed as 2.30V and so on

It looks to me that as the decimal number is separated and stored in a separate variable, the number is converted and any leading zeros are omitted. (Just as if your electric bill is $50.00 you wouldn't say that it is $050.00. I think that's causing the displayed values to shift over.

I'm new to this and I have googled some functions and tried the implement them. I probably did them wrong for lack of understanding but I have played with some sprint() statements and I tried the .startswith("") function to check for a leading zero and (in the hopes that if it were missing i could add one) but that didn't seem to work either (again, I probably didn't enter it right.)

Help is much appreciated!!! I bet its a simple issue to some of the veterans here.

Heres the code:

int Vadc = analogRead(InputPin);
int VoltageValue = (Vadc / 2.04799979);     //0-5V   (2.04 is the ADC change ratio needed for a difference of .01V)
int whole = VoltageValue / 100;             //sets integer portion of text
int fraction = VoltageValue - whole * 100;  //sets decimal portion of text in a seperate variable
char vwhole[10];                            //contains the Whole number
char vfraction[5];                          //contains the decimal value
itoa(whole, vwhole, 10);                    //itoa()'s to convert integers to strings
itoa(fraction, vfraction, 10);
LCD.TextGoTo(18,0);
LCD.writeString(vwhole);
LCD.writeString(".");
LCD.writeString(vfraction);

Instead of itoa(), use sprintf(). The f means formatted. You can make it print values with leading zeros, using %02d as the format specifier for an int. 9 will become "09". 10 will become "10". %02ld for longs.

except sprint() on the arduino does not handle floats.

Thanks for your responses.

How would I use that sprintf() statement? I looked at it before but I don't think I did it right. Since then I ditched it to play with itoa().

If sprintf() doesn't work how would you guys suggest I go about it?

Not so sure this will work well either:

int VoltageValue = (Vadc / 2.04799979);

since both variables are int's. Integers are truncated, not rounded.

Edit: Use dtostrf() to do the conversion. It will likely use less memory, too.

except sprint() on the arduino does not handle floats.

whole and fraction are not floats.

OP: You could use dtostrf() (double to string, formatted) to convert a float to a string.

I will try that. Is it safe to assume the usage for dtostrf() is the same as sprintf() ? just declare the int variables as floats first right?

Is it safe to assume the usage for dtostrf() is the same as sprintf() ?

No.

Change

LCD.writeString(vwhole);
LCD.writeString(".");
LCD.writeString(vfraction);

to

LCD.writeString(vwhole);
LCD.writeString(".");
LCD.writeString(vfraction/10);
LCD.writeString(vfraction%10);

ok so while looking up that function i found a few more goodies and plugged them into my code. Here are the changes:

float VoltageValue = 0;
Vadc = analogRead(InputPin);
VoltageValue = (Vadc * (5.0 / 1023.0));   //0-5V Scale
char Voltage[10];
dtostrf(VoltageValue, 10, 3, Voltage);
LCD.TextGoTo(18,0);
LCD.writeString(Voltage);
LCD.writeString("v");

Works like a champ. 3 decimal places, accurate over entire range, and no splitting into seperate values. Thanks for that dtostrf() advice!

Thank you everyone.

char Voltage[10];
dtostrf(VoltageValue, 10, 3, Voltage);

I don’t recommend telling dtostrf() to write up to 10 characters and a terminating NULL in an array that can only hold 10 characters.

Ok I will modify that to only hold whats needed. I just wasnt sure how long that decimal is. I read that you are supposed to make sure the container is able to hold all of the characters.

thanks!