Go Down

Topic: lcd.print() two decimal places (Read 1 time) previous topic - next topic

oidium45

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

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

Delta_G

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.

oidium45

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

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

oidium45

I have seen that page. Cannot seem to implement it correctly  :(

oidium45

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

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

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


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