user48805: 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.

user4143: There's a couple of things wrong here:

You are just getting the last read value doubled:
x_offset += x_offset;

However x_offset is always replaced with the lates reading.  You're not summing anything.

To get anything other than 0 out of dividing an integer by 2000 the value stored in the integer would have to be > 2000. But since you can only store a maximum of 32767 in your int16_t variables, even if you didn't have the problem in point 1, the variables would overflow and the results would be meaningless.

You need to:

Use a second variable per axis to store your total
Ensure that it is big enough to store the maximum reading × 2000.  I'd suggest a long or int32_t.

For instance:
int16_t x_offset = 0;
int16_t y_offset = 0;
int16_t z_offset = 0;
int32_t x_total = 0;
int32_t y_total = 0;
int32_t z_total = 0;

And inside setup():
for(int i=0; i<2000;i++)
  {
    GetXYZ(x_offset, y_offset, z_offset);
    x_total += x_offset;
    y_total += y_offset;
    z_total += z_offset; 
    Serial.println(x_offset);
    delay(3);
  }
  x_total /= 2000;
  y_total /= 2000;
  z_total /= 2000;
  Serial.println(x_total);Serial.println("...");Serial.println(y_total);Serial.println("...");
Serial.println(z_total);Serial.println("...");


48805: <p>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:</p>

<pre><code>int16_t x_offset = 0;
int16_t y_offset = 0;
int16_t z_offset = 0;
</code></pre>

<p>And inside <code>setup()</code>:</p>

<pre><code>for(int i=0; i&lt;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("...");
</code></pre>

<p>Here is the GetXYZ() function:</p>

<pre><code>void GetXYZ( int16_t &amp;x, int16_t &amp;y, int16_t &amp;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() &lt;&lt; 8 | Wire.read();
  y = Wire.read() &lt;&lt; 8 | Wire.read();
  z = Wire.read() &lt;&lt; 8 | Wire.read();
}
</code></pre>

<p>The values printed in the serial of <code>x_offset</code>, <code>y_offset</code> and <code>z_offset</code> are always <code>0</code>.</p>
 4143: <p>There's a couple of things wrong here:</p>

<ol>
<li><p>You are just getting the last read value doubled:</p>

<p>x_offset += x_offset;</p></li>
</ol>

<p>However <code>x_offset</code> is always replaced with the lates reading.  You're not summing anything.</p>

<ol start="2">
<li>To get anything other than 0 out of dividing an integer by 2000 the value stored in the integer would have to be > 2000. But since you can only store a maximum of 32767 in your int16_t variables, even if you didn't have the problem in point 1, the variables would overflow and the results would be meaningless.</li>
</ol>

<p>You need to:</p>

<ul>
<li>Use a second variable per axis to store your total</li>
<li>Ensure that it is big enough to store the maximum reading × 2000.  I'd suggest a <code>long</code> or <code>int32_t</code>.</li>
</ul>

<p>For instance:</p>

<pre><code>int16_t x_offset = 0;
int16_t y_offset = 0;
int16_t z_offset = 0;
int32_t x_total = 0;
int32_t y_total = 0;
int32_t z_total = 0;
</code></pre>

<p>And inside setup():</p>

<pre><code>for(int i=0; i&lt;2000;i++)
  {
    GetXYZ(x_offset, y_offset, z_offset);
    x_total += x_offset;
    y_total += y_offset;
    z_total += z_offset; 
    Serial.println(x_offset);
    delay(3);
  }
  x_total /= 2000;
  y_total /= 2000;
  z_total /= 2000;
  Serial.println(x_total);Serial.println("...");Serial.println(y_total);Serial.println("...");
Serial.println(z_total);Serial.println("...");
</code></pre>
