Go Down

### Topic: lcd.print() two decimal places (Read 2713 times)previous topic - next topic

#### oidium45

##### Apr 23, 2012, 01:16 am
Hello, I am trying to get the arduino to print to a precision of 2 decimal places.

Here is a slimmed down example of what my code looks like.
Code: [Select]
`...sensorValue = analogRead(sensorPin);sensorValuePercentage = (sensorValue - 23) / 10;...lcd.print(sensorValuePercentage);...`

#### Sev

#1
##### Apr 23, 2012, 01:38 am
See if your LCD library implements the sprintf() method.

#### Delta_G

#2
##### Apr 23, 2012, 01:39 am
Are those float variables?  If so then you can put a second argument to the print() function that tells how many decimal places.

If they are not floats then there are no decimal places to print.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

#### oidium45

#3
##### Apr 23, 2012, 01:49 am
Thank you, I have been looking into float but cannot find a good tutorial on how to properly use it. All of my attempts so far have failed...
Do you have a good example?

#### Delta_G

#4
##### Apr 23, 2012, 01:56 am
http://arduino.cc/en/Reference/Float
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

#### oidium45

#5
##### Apr 23, 2012, 02:22 am
I have seen that page. Cannot seem to implement it correctly

#### oidium45

#6
##### Apr 23, 2012, 02:23 am
This is the code i am using. It is simple enough.

Code: [Select]
`#include <LiquidCrystal.h>LiquidCrystal lcd(52, 53, 50, 51, 48, 49);int sensorPin = A0;int ledPin = 13;int sensorValue = 0;int sensorValuePercentage = 0;void setup() {  pinMode(ledPin, OUTPUT);  lcd.clear();  lcd.begin(20, 4);  lcd.print("... loading");  delay(200);}void loop() {  {    sensorValue = analogRead(sensorPin);    sensorValuePercentage = (sensorValue - 23) / 10;        digitalWrite(ledPin, HIGH);    delay(sensorValue);    digitalWrite(ledPin, LOW);    delay(sensorValue);  }  {  lcd.clear();  lcd.setCursor(0, 0);  lcd.print("Potentiometer Value");  lcd.setCursor(0, 1);  lcd.print(sensorValue);  lcd.setCursor(5, 1);  lcd.print("ohms");  lcd.setCursor(0, 2);  lcd.print(sensorValuePercentage);  lcd.setCursor(5, 2);  lcd.print("%");  }}        `

#### oidium45

#7
##### Apr 23, 2012, 02:29 am
NEVERMIND, I figured it out. Thanks for the help!

Here is the modified code in case someone else has a similar issue...

Code: [Select]
`#include <LiquidCrystal.h>LiquidCrystal lcd(52, 53, 50, 51, 48, 49);int sensorPin = A0;int ledPin = 13;int sensorValue = 0;float sensorValuePercentage;void setup() {  pinMode(ledPin, OUTPUT);  lcd.clear();  lcd.begin(20, 4);  lcd.print("... loading");  delay(200);}void loop() {  {    sensorValue = analogRead(sensorPin);    sensorValuePercentage = (float)(sensorValue - 23) / 10;        digitalWrite(ledPin, HIGH);    delay(sensorValue);    digitalWrite(ledPin, LOW);    delay(sensorValue);  }  {  lcd.clear();  lcd.setCursor(0, 0);  lcd.print("Potentiometer Value");  lcd.setCursor(0, 1);  lcd.print(sensorValue);  lcd.setCursor(5, 1);  lcd.print("ohms");  lcd.setCursor(0, 2);  lcd.print(sensorValuePercentage);  lcd.setCursor(5, 2);  lcd.print("%");  }}        `

#### pluggy

#8
##### Apr 23, 2012, 02:40 am
Thinking outside the box you could use an integer to hold the value multiplied by 100, then print the value / 100 followed by a decimal point followed by the value mod (%) 100.

Example

301.34 would become 30134.  30134  / 100 in integer maths would become 301 and 30134 mod 100 would become 34.
http://pluggy.is-a-geek.com/index.html

#### WizenedEE

#9
##### Apr 23, 2012, 05:07 am

Thinking outside the box you could use an integer to hold the value multiplied by 100, then print the value / 100 followed by a decimal point followed by the value mod (%) 100.

Example

301.34 would become 30134.  30134  / 100 in integer maths would become 301 and 30134 mod 100 would become 34.

... which is what the Print::print(double n, int digits) method that the lcd library contains does.

Go Up

Please enter a valid email to subscribe