Hi!
I used the exemple code from Adafruit_LSM303_U librairie to create an inclinometer.
It works great but when I want to write the tilt (theta value) in my modbus table, it writes 45 or 225...
I really don't get it.
If you have any idea about what's going on, I would be happy to read you
Here's my code :
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <math.h>
#include <ModbusRtu.h>
#define TXEN 4
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
/* Here are the variables used */
float theta; //angle theta pour l'inclinaison
float acc_x;
float acc_y;
float acc_z;
float g = 9.81;
Modbus slave(1,Serial,TXEN); //Slave Nano // this is slave @1 and RS-485
int au16data[17] = { 0, 0, 0, 0, 0, 0, 9999, 9999, 0, 0, 9999, 0, 0, 0, 9999, 9999, 9999 }; //Les trois derniers chiffres du tableau sont accéléromètres X Y Z
void setup()
{
while (!Serial);
Serial.begin(19200);
slave.start();
}
void loop()
{
get_Inclinaison();
slave.poll( au16data, 17 );
}
void get_Inclinaison(){
/* Get a new sensor event */
sensors_event_t event2;
accel.getEvent(&event2);
acc_x = event2.acceleration.x;
acc_y = event2.acceleration.y;
acc_z = event2.acceleration.z;
theta = atan2(acc_x/9.81,acc_z/9.81)/2/3.141592654*360; /*Should be in degrees*/
if (theta < 0)
{
theta = theta + 360;
au16data[9] = (int)theta;
}
else
{
au16data[9] = (int)theta;
}
}
Thanks in advance.