How do I print the content of a variable of type float to an 2x16 LCD?
I have the Arduino+LCD up and running and have no problem in displaying characters etc. but I want to display readings from a sensor which is stored as a float value, but I'm a bit lost on how to make the LCD accept the reading.
Somehow I'm expecting that I have to convert the variable to a string, but how is that done easily which is also preserving the sign?
With the std C library, you'd use something like sprintf or (its safer variant) snprintf. You first need a string long enough to hold the characters of the float, then call sprintf with the correct format string. However, I don't know how Arduino handles floats as the ATmega has no floating point capabilities. It's probably done with fixed point math and I think I heard something about having to recompile the arduino library with floating point capabilities, which makes it much larger.
If floats are implemented as fixed point numbers, you should be able to multiply them by 10 or 100 to move the decimal place up. Then convert to integer and put a decimal place in yourself. I would just avoid using floats altogether, they're not supported by the hardware and it's only going to introduce more headaches.
I had the same issue. Finally just used the method Horace suggested to multiply the integers up by 100, then use itoa() to convert to a string, and then wrote a function to add the decimal place back in.
Below is the function I used (note: I'm not a real programmer so it is probably not very elegant).
It is called with the following arguments:
decimate(string containing integer, number of decimal places)
void decimate(char test[],int dec) {
int i=0;
int length=strlen(test);
char msg[10]="";
strcpy(msg,test);
if (length <= dec) {
for(i=dec;i>(dec-length);i--) msg = msg[i-(dec-length+1)];
_ for(i=0;i<(dec+1-length);i++) msg*='0';_
_ length = strlen(msg);_
_ }_
_ for (i=length;i>(length-dec);i--) msg=msg[i-1];
msg[length-dec]='.';
strcpy(test,msg);
}*_
Yes, I would also like to stick with integers but I have to do following calculation and I can't see how that is possible without a float variable:
RH_dec = 0.0405RH_sens+2.810^-6*RH_sens^2-4
You should be able to do the calculation as a float, then convert it to an int to display it.
To give a small explanation of why this doesn't "just work"... There is functionality in the libraries used by Arduino (the AVR Libc) to do printing of floats. Unfortunately, to do this well (i.e. to take into account the value, exponent and number of decimal places of the value) takes a decent amount of computation, and adding this ability to the core would take up a decent amount of space. I think it's about 1.5 KB or something like 10% of the program space. What would be best is to find a way to only provide it for people who need it, but I don't know of an easy way.
please help me....
I am expieriencing with a major problem...
I have sensors on my arduino and i can print them to serial monitor...but i cannot print them to my LCD shield...
Please help me on how to convert the values I want to print...
the code given below is the one i want to print on my lcd...
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");