Print LCD only 1 decimal point

How do I only print to one decimail on LCD dispaly

#include<Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 
int minVal=265;
int maxVal=402;
 
double x;
 
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
lcd.begin(16, 2); 
Serial.begin(9600);
lcd.print("Scotts Level"); 
}
void loop()
{

Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
 
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
lcd.setCursor(0, 1);

Serial.print("Level ");
Serial.println(x);
lcd.print(x);

delay(1200);
}

The "u_int" designation creates an unsigned (meaning non-negitave value) as whole integers only...

Use a float, or double,

float x;

Lcd.print(x,1)

With the 1 being the decimal places to print

Edit- i see the double now, sorry for that!

Thanks

If you are attempting to calculate tilt angles, that code won't give accurate answers (the use of map() makes no sense at all).

This will (see this tutorial):

  float ax = AcX; 
  float ay = AcY;
  float az = AcZ;
  roll = atan2(ay ,az) * RAD_TO_DEG;
  pitch = atan2(-ax , sqrt(ay * ay + az * az)) * RAD_TO_DEG;
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.