I am an electrical engineering student and this is my first project without the supervision of a professor. I want to use the accelerometer MMA7361 connected to an Arduino in a RC plane in order to know the angle of the three axes of the aircraft, the acceleration that each axle suffers or the total acceleration of the plane. Initially I used this code for the measurement of angles of only two axes:
int maxi = 0,mini = 1023;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorX= analogRead(A0);
sensorX = map(sensorX, 180, 530, 90, -90);
Serial.print(" Angulo X = ");
Serial.println(sensorX);
int sensorY= analogRead(A1);
sensorY = map(sensorY, 205, 585, 90, -90);
Serial.print(" Angulo Y = ");
Serial.println(sensorY);
delay(600);
}
When I was testing this code I realized that if the angle remained the same but the accelerometer suffer any abrupt deceleration, the angle value indicated would change. Then I began to think of a solution to my problem. When reading the accelerometer library, I realized that there was a function called getXAccel that returns the acceleration of the X-axis as a int (1 G = 100.00). The code bellow uses that function and I found it in the library.
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;
void setup()
{
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3);
accelero.setSensitivity(LOW);
accelero.calibrate();
}
void loop()
{
x = accelero.getXAccel();
y = accelero.getYAccel();
z = accelero.getZAccel();
Serial.print("\nx: ");
Serial.print(x);
Serial.print(" \ty: ");
Serial.print(y);
Serial.print(" \tz: ");
Serial.print(z);
Serial.print("\tG*10^-2");
delay(500);
}
Testing that code, initially everything seemed to be correct. In X and Y-axis the acceleration practically had not existed and in the Z-axis the acceleration was approximately 1G. But, when I leaned the accelerometer 90 in x, the acceleration was about to 4G, but actually I expected it to be 1G.
Can someone tell me what is going on? I also saw in the library the function getTotalVector that returns the magnitude of the total acceleration vector as an integer but I don't know how to use it. Anyone have a better ideia to find the angle, the total acceleration of the plane or the acceleration of each axis?
Thanks a lot.