Calibrate MMA7455 Accelerometer

Hello,

I am trying to make a calibrate button for the accelerometer.
This is the code to calibrate it:

mySensor.calibrateOffset(Ax, Ay, Az);

where Ax, Ay and Az are the calibrations of the sensor.

I made a simple code to calibrate it by reading the current x, y and z value and do it *1 or *-1 and put that into the Ax, Ay and Az.

if (xPos < 0) {
  Ax = xPos * -1;
}
else if (xPos == 0) {}
else {
  Ax = -xPos - CalibrateArray[0][1];
}

But every time I recalibrate it after moving it, it takes the first values before the calibration.
So if it starts reading X = 20, I calibrate it with -10 (push button to run that small code above) so X = 0, then I move it till X becomes 5, Calibrate again, it reads 5, so it does -5 on the first reading, meaning X will become 15 (20-5=15).

I had in mind to make a 2D array to save values of X, Y and Z twice and add or remove the old value with the new readings.

void Accelerometer::calibrate() {
	
	ReadArray[0] = xPos;
	ReadArray[1] = yPos;
	ReadArray[2] = zPos;
	
	CalibrateArray[0][0] = Ax;
	CalibrateArray[1][0] = Ay;
	CalibrateArray[2][0] = Az;
	
	Serial.println("Array 0:");
	Serial.print((int)CalibrateArray[0][0]);
	Serial.print('\t');
	Serial.print((int)CalibrateArray[1][0]);
	Serial.print('\t');
	Serial.println((int)CalibrateArray[2][0]);
	Serial.println("Array 1:");
	Serial.print((int)CalibrateArray[0][1]);
	Serial.print('\t');
	Serial.print((int)CalibrateArray[1][1]);
	Serial.print('\t');
	Serial.println((int)CalibrateArray[2][1]);
	
	if (xPos < 0) {
		Ax = xPos * -1 + CalibrateArray[0][1];
	}
	else if (xPos == 0) {	}
	else {
		Ax = -xPos - CalibrateArray[0][1];
	}
	if (yPos < 0) {
		Ay = yPos * -1 + CalibrateArray[1][1];
	}
	else if (yPos == 0) {	}
	else {
		Ay = -yPos - CalibrateArray[1][1];
	}
	if (zPos < 0) {
		Az = zPos * -1 + CalibrateArray[2][1];
	}
	else if (zPos == 0) {	}
	else {
		Az = -zPos - CalibrateArray[2][1];
	}
	
	mySensor.calibrateOffset(Ax, Ay, Az); // Calibreer x, y, z
	delay(100);
	
	CalibrateArray[0][1] = CalibrateArray[0][0];
	CalibrateArray[1][1] = CalibrateArray[1][0];
	CalibrateArray[2][1] = CalibrateArray[2][0];
	
	delay(100);
	
	Serial.println("-----------");
	Serial.println("Calibrated");	
	Serial.println("-----------");
	Serial.println("Array 0:");
	Serial.print((int)CalibrateArray[0][0]);
	Serial.print('\t');
	Serial.print((int)CalibrateArray[1][0]);
	Serial.print('\t');
	Serial.println((int)CalibrateArray[2][0]);
	Serial.println("Array 1:");
	Serial.print((int)CalibrateArray[0][1]);
	Serial.print('\t');
	Serial.print((int)CalibrateArray[1][1]);
	Serial.print('\t');
	Serial.println((int)CalibrateArray[2][1]);
	Serial.println("+++++++++++++++++++");
	Serial.println("+++++++++++++++++++");
}

This is the code I made, but I can't seem to get it calibrate correctly.

Okay, I found a way around by resetting the values and then calibrate it.

void Accelerometer::calibrate() {
	
	mySensor.calibrateOffset(0, 0, 0); // Reset calibration
	delay(50);
	
	xPos = mySensor.readAxis('x');
	yPos = mySensor.readAxis('y');
	zPos = mySensor.readAxis('z');
	
	ReadArray[0] = xPos;
	ReadArray[1] = yPos;
	ReadArray[2] = zPos;
	
	if (xPos < 0) {
		Ax = xPos * -1;
	}
	else if (xPos == 0) {	}
	else {
		Ax = -xPos;
	}
	if (yPos < 0) {
		Ay = yPos * -1;
	}
	else if (yPos == 0) {	}
	else {
		Ay = -yPos;
	}
	if (zPos < 0) {
		Az = zPos * -1;
	}
	else if (zPos == 0) {	}
	else {
		Az = -zPos;
	}
	
	mySensor.calibrateOffset(Ax, Ay, Az); // Calibrate x, y, z
	delay(50);
	
	Serial.println("-----------");
	Serial.println("Calibrated");	
	Serial.println("-----------");
}