I've been trying to make an accelerometer calibration tool for my Arduino Nano and MPU6050, but the arduino seems to randomly do incorrect addition. If you look at the output of the console, it works fine for the first 4 iterations, then on the fifth, decides that -31904+-7964 is equal to 25668. It is supposed to be -39868. It does this every time I run it, usually between the 3rd to 6th iteration, but with different numbers.
bool calix = true; //X calibrated or not
while(calix)
{
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("PrevAvgX: "); Serial.println(axo/i);
Serial.print("Axo: "); Serial.println(axo);
Serial.print("RawX: "); Serial.println(ax);
axo = (float)ax + (float)axo;
i += 1;
Serial.print("Axo2: "); Serial.println(axo);
Serial.print("RawX: "); Serial.println(ax);
float cax = axo/i; //Current Average (x)
Serial.print("AverageX: "); Serial.println(cax);
delay(100);
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("NewX: "); Serial.println(ax);
float offsettocurrent = ax-(axo/i);
Serial.print("AverageX-RawX: "); Serial.println(offsettocurrent);
if(offsettocurrent>5 or offsettocurrent<-5)
{
Serial.println("Offset not accurate enough. Continuing.");
}
else
{
Serial.println("Offset accurate enough. Finishing calibration");
break;
}
delay(100);
}
Serial console output:
Initializing I2C devices...
Testing device connections...
Connected to MPU6050
Attempting full calibration
PrevAvgX: -1
Axo: 0
RawX: -8048
Axo2: -8048
RawX: -8048
AverageX: -8048.00
NewX: -8028
AverageX-RawX: 20.00
Offset not accurate enough. Continuing.
PrevAvgX: -8048
Axo: -8048
RawX: -7940
Axo2: -15988
RawX: -7940
AverageX: -7994.00
NewX: -7964
AverageX-RawX: 30.00
Offset not accurate enough. Continuing.
PrevAvgX: -7994
Axo: -15988
RawX: -7972
Axo2: -23960
RawX: -7972
AverageX: -7986.00
NewX: -8016
AverageX-RawX: -30.00
Offset not accurate enough. Continuing.
PrevAvgX: -7986
Axo: -23960
RawX: -7944
Axo2: -31904
RawX: -7944
AverageX: -7976.00
NewX: -8016
AverageX-RawX: -40.00
Offset not accurate enough. Continuing.
PrevAvgX: -7976
Axo: -31904
RawX: -7964
Axo2: 25668
RawX: -7964
AverageX: 5133.00
NewX: -7944
AverageX-RawX: -13077.00
Offset not accurate enough. Continuing.