Pages: [1]   Go Down
Author Topic: Float significant digits on LCD display  (Read 742 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 223
Posts: 13902
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7260
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Think this should work, 2 digits is the default.
Code:
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
Logged


Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 223
Posts: 13902
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7260
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
Logged


Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2740
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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
« Last Edit: July 31, 2011, 11:15:47 pm by bperrybap » Logged

Pages: [1]   Go Up
Jump to: