Hi Everyone
I played around with the ADXL345 to build a electronic bubble level. Code workes almost right, but the released Degrees differs about approx. 5 Degrees from exakt horizontal position measured by two different mechanical bubble levels. Is there any need to adjust the accelerometer Chip? Thanks for any Answers and good Ideas!
#include "Wire.h"
#include "ADXL345.h"
const float alpha = 1;
ADXL345 acc;
void setup()
{
acc.begin();
Serial.begin(9600);
delay(100);
}
void loop()
{
double Xg, Yg, Zg;
acc.read(&Xg, &Yg, &Zg);
//Low Pass Filter to smooth out data
double fXg = Xg * alpha + (fXg * (1.0 - alpha));
double fYg = Yg * alpha + (fYg * (1.0 - alpha));
double fZg = Zg * alpha + (fZg * (1.0 - alpha));
//Convert to Degrees
int x = RAD_TO_DEG * (atan2(-fYg, -fZg) + PI);
int y = RAD_TO_DEG * (atan2(-fXg, -fZg) + PI);
int z = RAD_TO_DEG * (atan2(-fYg, -fXg) + PI);
Serial.print(x);
Serial.print(" ");
Serial.println(y);
delay(250);
}