I trying to loop over a function in arduino to get values for 2000 times, and then divide it by 2000 to get an average offset:
int16_t x_offset = 0;
int16_t y_offset = 0;
int16_t z_offset = 0;
And inside setup()
:
for(int i=0; i<2000;i++)
{
GetXYZ(x_offset, y_offset, z_offset);
x_offset += x_offset;
y_offset += y_offset;
z_offset += z_offset;
Serial.println(x_offset);
delay(3);
}
x_offset /= 2000;
y_offset /= 2000;
z_offset /= 2000;
Serial.println(x_offset);Serial.println("...");Serial.println(y_offset);Serial.println("...");
Serial.println(z_offset);Serial.println("...");
Here is the GetXYZ() function:
void GetXYZ( int16_t &x, int16_t &y, int16_t &z) {
// Get the gyro values
Wire.beginTransmission(MPU);
Wire.write(0x43); // first register of gyro values
Wire.endTransmission(false);
Wire.requestFrom(MPU,6);
x = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
}
The values printed in the serial of x_offset
, y_offset
and z_offset
are always 0
.