yoramlavee:
Something I forgot to mention - The sensor is installed on a machine which have plenty of vibration and sudden movements. I want to have clean 1 axis angle regardless of those movements.
I assumed the problem was the sensor type and was was asking advice for a different type.
So you say the sensor should give me stable read regardless o quick movements?
here is the code im using - it's a sample code I found working and and just trying to get the LED lit when passing above a limit. When I move the sensor quick below that limit, it still make the LED lit...
#include<Wire.h>
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
int SetAngle;
int KillSw_pin = 7;
double x;
double y;
double z;
void setup(){
pinMode(KillSw_pin, OUTPUT);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(250000);
SetAngle = 34;
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
axis_X=Wire.read()<<8|Wire.read();
axis_Y=Wire.read()<<8|Wire.read();
axis_Z=Wire.read()<<8|Wire.read();
int xAng = map(axis_X,minVal,maxVal,-90,90);
int yAng = map(axis_Y,minVal,maxVal,-90,90);
int zAng = map(axis_Z,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
if (SetAngle > x) {
digitalWrite(KillSw_pin, HIGH);
}
else {
digitalWrite(KillSw_pin, LOW);
}
}