Pages: [1]   Go Down
Author Topic: lcd.print() two decimal places  (Read 1076 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

...

sensorValue = analogRead(sensorPin);
sensorValuePercentage = (sensorValue - 23) / 10;

...

lcd.print(sensorValuePercentage);

...
Logged

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

See if your LCD library implements the sprintf() method. 
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 17
Posts: 723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/Float
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have seen that page. Cannot seem to implement it correctly  smiley-sad
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the code i am using. It is simple enough.

Code:
#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("%");
  }
}
   
   
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

NEVERMIND, I figured it out. Thanks for the help!

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

Code:
#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("%");
  }
}
   
   
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: