I am trying to measure the tilt angle of the project but the sensor values change wildly:
I am not even moving the sensor and you can see below it has values all over the place.
Code: Select all
// Basic demo for accelerometer readings from Adafruit LIS3DH
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10
// software SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
// hardware SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
}
Serial.println("LIS3DH found!");
lis.setRange(LIS3DH_RANGE_2_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
lis.setDataRate(LIS3DH_DATARATE_10_HZ);
Serial.print("Data rate set to: ");
switch (lis.getDataRate()) {
case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break;
case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break;
case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break;
case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break;
case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break;
case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break;
case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break;
case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break;
case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break;
case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break;
}
}
int x, y, z; //three axis acceleration data
double roll = 0.00, pitch = 0.00; //Roll & Pitch are the angles which rotate by the axis X and y
void RP_calculate(){
double x_Buff = float(x);
double y_Buff = float(y);
double z_Buff = float(z);
//roll = atan2(y_Buff , z_Buff) * 57.3;
roll = atan2( y_Buff , x_Buff ) * 57.3;
pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
}
void loop() {
sensors_event_t event;
lis.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("2X: "); Serial.print(event.acceleration.x);
Serial.print(" \t2Y: "); Serial.print(event.acceleration.y);
Serial.print(" \t2Z: "); Serial.print(event.acceleration.z);
x = event.acceleration.x;
y = event.acceleration.y;
z = event.acceleration.z;
RP_calculate();
Serial.print("\tRoll 2: "); Serial.print(roll);
Serial.print("\tPitch 2: "); Serial.print(pitch);
Serial.println();
delay(200);
}
Here is the OUTPUT:
Code: Select all
14:36:20.148 -> 2X: 182.62 2Y: 90.39 2Z: 60.28 Roll 2: 26.31 Pitch 2: -59.28
14:36:20.352 -> 2X: 212.75 2Y: 90.39 2Z: 30.16 Roll 2: 23.00 Pitch 2: -65.90
14:36:20.558 -> 2X: -239.14 2Y: 180.76 2Z: 30.16 Roll 2: 143.03 Pitch 2: 52.64
14:36:20.758 -> 2X: 212.75 2Y: 150.64 2Z: 60.28 Roll 2: 35.28 Pitch 2: -52.69
14:36:20.964 -> 2X: 182.62 2Y: 120.51 2Z: 120.53 Roll 2: 33.40 Pitch 2: -47.01
14:36:21.170 -> 2X: 152.50 2Y: 90.39 2Z: 90.41 Roll 2: 30.63 Pitch 2: -50.06
14:36:21.376 -> 2X: 212.75 2Y: 150.64 2Z: 90.41 Roll 2: 35.28 Pitch 2: -50.48
14:36:21.580 -> 2X: -239.14 2Y: 120.51 2Z: 90.41 Roll 2: 153.35 Pitch 2: 57.89
14:36:21.783 -> 2X: -239.14 2Y: 30.13 2Z: 90.41 Roll 2: 172.86 Pitch 2: 68.35
14:36:21.990 -> 2X: 212.75 2Y: 60.26 2Z: 120.53 Roll 2: 15.80 Pitch 2: -57.68
14:36:22.162 -> 2X: -239.14 2Y: 150.64 2Z: 60.28 Roll 2: 147.90 Pitch 2: 55.95