Here you can see my Sketch, with an auto configuration section at first.
You can check updates or MMA7260 in my blog: La navaja del Geek: Midiendo las inclinaciones con el MMA7260 (in spanish but easy to follow and i can reply in english)
In rest you can see de g-force near 1 with good results!!! In the configuration step keep the accelerometer quiet in horizontal position
x,y,z
-0.03 0.05 1.05
-0.05 0.06 1.06
-0.04 0.01 1.06
-0.07 0.09 1.04
-0.04 0.06 1.06
-0.08 0.09 1.05
the code here
#define DEBUGMODE
const float RESOLUTION=800; //0.8 v/g -> resolucion de 1.5g -> 800mV/g
const float VOLTAGE=3.3; //voltage al que está conectado el acelerómetro
const float ZOUT1G = 2450; // mv Voltage en Zout a 1G
const float ZOUT0G = 1650; // mv Voltage en Zout a 1G
const float ZOUT_1G = 850; // mv Voltage en Zout a 1G
const float XOUT1G = 2450; // mv Voltage en Zout a 1G
const float XOUT0G = 1650; // mv Voltage en Zout a 1G
const float YOUT1G = 2450; // mv Voltage en Zout a 1G
const float YOUT0G = 1650; // mv Voltage en Zout a 1G
const int NADJ = 50;
// Entradas analógicas donde van los sensores
const int xaxis = 0;
const int yaxis = 1;
const int zaxis = 2;
// Salida digital del led de la placa
const int LEDPIN = 13;
float XError,YError,ZError;
float zero_g_reference, ZRes, XRes, YRes;
float xd,yd,zd,z,x,y;
float AccelAdjust(int axis)
{
float acc = 0,lectura = 0;
int j;
for (j=0;j<NADJ;j++)
{
lectura=analogRead(axis);
acc = acc + ((lectura*5000)/1024.0);
delay(11); //número primo para evitar ciclos de lectura
}
return acc/NADJ;
}
void setup()
{
Serial.begin(9600); // 9600 bps
pinMode(xaxis,INPUT);
pinMode(yaxis,INPUT);
pinMode(zaxis,INPUT);
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, HIGH);
XError = AccelAdjust(xaxis);
YError = AccelAdjust(yaxis);
ZError = AccelAdjust(zaxis);
ZError = ZError - ZOUT_1G;
digitalWrite(LEDPIN, LOW);
#ifdef DEBUGMODE
Serial.println("Ajustado acelerometro eje X");
Serial.print("Error de X= ");
Serial.println(XError);
Serial.println("Ajustado acelerometro eje Y");
Serial.print("Error de Y= ");
Serial.println(YError);
Serial.println("Ajustado acelerometro eje Z");
Serial.print("Error de Z= ");
Serial.println(ZError);
#endif
}
void loop()
{
x=analogRead(xaxis);
y=analogRead(yaxis);
z=analogRead(zaxis);
float aax = (((x5000.0)/1024.0)-XError)/RESOLUTION;
float aay = (((y5000.0)/1024.0)-YError)/RESOLUTION;
float aaz = (((z*5000.0)/1024.0)-ZError)/RESOLUTION;
Serial.print(aax);
Serial.print(" ");
Serial.print(aay);
Serial.print(" ");
Serial.print(aaz);
Serial.println();
delay(500);
}