code not adding numbers properly

I am trying to create a function to find the average of sensor values on the Arduino in order to calibrate it, however the summation is not working properly and therefore the average is not correct. The table below shows a sample of the output. The left column should be the rolling sum of the output which is displayed in the right column (How are negatives getting in there?)

-10782      17112        
  6334      17116       
 23642      17308        
-24802      17092        
 -7706      17096        
  9326      17032        
 26422      17096        
-21986      17128

The calibrateSensors() function, which is supposed to execute this is shown below

void calibrateSensors(int16_t * accelOffsets){

  int16_t rawAccel[3];
  int sampleSize = 2000;


  Serial.println("Accelerometer calibration in progress...");

  for (int i=0; i<sampleSize; i ++){

    readAccelData(rawAccel);        // get raw accelerometer data
    accelOffsets[0] += rawAccel[0]; // add x accelerometer values
    accelOffsets[1] += rawAccel[1]; // add y accelerometer values
    accelOffsets[2] += rawAccel[2]; // add z accelerometer values
    Serial.print(accelOffsets[2]);
    Serial.print("\t\t");
    Serial.print(rawAccel[2]);
    Serial.print("   \t FIRST I:");
    Serial.println(i);
  }

  for (int i=0; i<3; i++)
  {
    accelOffsets[i] = accelOffsets[i] / sampleSize;
    Serial.print("Second I:");
    Serial.println(i);
  }

  Serial.println("Accelerometer calibration complete");
  Serial.println("Accelerometer Offsets:");
  Serial.print("Offsets (x,y,z): ");
  Serial.print(accelOffsets[0]);
  Serial.print(", ");
  Serial.print(accelOffsets[1]);
  Serial.print(", ");
  Serial.println(accelOffsets[2]);
}

and the readAccelData() function is as follows

void readAccelData(int16_t * destination){
  // x/y/z accel register data stored here
  uint8_t rawData[6];
  // Read the six raw data registers into data array
  I2Cdev::readBytes(MPU6050_ADDRESS, ACCEL_XOUT_H, 6, &rawData[0]);  
  // Turn the MSB and LSB into a signed 16-bit value
  destination[0] = (int16_t)((rawData[0] << 8) | rawData[1]) ;  
  destination[1] = (int16_t)((rawData[2] << 8) | rawData[3]) ;  
  destination[2] = (int16_t)((rawData[4] << 8) | rawData[5]) ;

Any idea where I am going wrong?

David376:
I am trying to do this also and getting the same response.

And you've got the exact same IP address!
What are the chances of that?

Perhaps if one of you ( :slight_smile: ) posted a complete program showing the problem then you might get some help

Perhaps if both of them don't get banned for trying to spam hidden links. . . .

Oops. Not going to happen.