Go Down

Topic: Float significant digits on LCD display (Read 853 times) previous topic - next topic

StePava

Hello everybody, this is my first post.
I manage to use my Arduino like a simpe "voltmeter" and interfacing with a 16x2 display.
I done all the hardware and the lcd correctly working with the included library LiquidCrystal, read an analog value and related it to the Vref of 5V.. now the problem is the format to display the value.
I use the lcd.print function, but in the reference it is not specified if it works with float values... I try it and it Works! but it display 2 significant digits.
For example, if the value is 12.3423 it display 12.34.. it can be ok, but I don't need such a precision and I want to save space on the display, so for me 1 single digit is enough (practically I want that the print on the display is 12.3).
How can I make this thing?

Thank you

robtillaart

Think this should work, 2 digits is the default.
Code: [Select]
float f = 12.3456;
byte digits = 1;
lcd.print(f, digits);


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr


Think this should work, 2 digits is the default.
Code: [Select]
float f = 12.3456;
byte digits = 1;
lcd.print(f, digits);





Are you sure this will work? The ref. page says no but maybe it's old.

http://arduino.cc/en/Reference/LiquidCrystalPrint

robtillaart

@liudr
from the liquidCrystal.cpp :    class LiquidCrystal : public Print

So LCD implements print, and print class supports printing floats - so it should work ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

StePava

Thank you very much! it Works.. the reference is not updated.
It is very powerful this library because, not only define the number of significant digits, but automatically rounds the value.

liudr


@liudr
from the liquidCrystal.cpp :    class LiquidCrystal : public Print

So LCD implements print, and print class supports printing floats - so it should work ...


Rob, I read the print.h. You're right the print now has (float,digit). I wish same has happened to the sprintf float support.

bperrybap

#6
Aug 01, 2011, 05:56 am Last Edit: Aug 01, 2011, 06:15 am by bperrybap Reason: 1


@liudr
from the liquidCrystal.cpp :    class LiquidCrystal : public Print

So LCD implements print, and print class supports printing floats - so it should work ...


Rob, I read the print.h. You're right the print now has (float,digit). I wish same has happened to the sprintf float support.



The AVR libc function sprintf() does support floating point. It is the arduino IDE that is the problem.
With the AVR libc as it it currently supplied, you have change the linker option to enable the floating point
version of of the xxprintf() functions.
Currently, there is no way to set the linker options with the official arduino IDE.
There was an old forum post,  (which I was unable to locate now )
that gave the patches as well as included a pre build IDE that allowed the compiler and linker options
to be configured in a file. I used it and it was great for changing optimizations and turning on/off warnings.

You could always blow off the IDE and use makefiles.....

Update:
Here is the link to the thread I mentioned above:
http://arduino.cc/forum/index.php/topic,54456.0.html
Using his IDE you can alter compiler and link options.

--- bill

#7
Nov 21, 2014, 05:57 am Last Edit: Nov 21, 2014, 07:37 am by penjoseph
I searched high & low to solve right justified lcd printing of float values restricted to 1 decimal place for my Arduino OBD LCD project.

Got the tip here: http://www.hobbytronics.co.uk/arduino-float-vars

sprintf() does not work.
dtostrf() works

Syntax
dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

where:
floatvar -float variable
StringLengthIncDecimalPoint -length of the string that will be created
numVarsAfterDecimal -number of digits after the deimal point to print
charbuf -array to store the results

Here's the thread where I display Fuel Rail Pressure, Turbo Boost & Voltage in single decimal floats: Arduino Freematics OBD

Go Up