Defining initial value for IMU9250

Hi there, I need some help with my code, my project is about making a device that detects users’ posture that gives haptic feedback when users are slouched -20degrees or reclined back 40degrees (When the upright posture of the user is considered as 0 degree). So what I want to is to detect forward tilt of -20 degrees and back-recline of 40 degrees (when the sensor is at 0 degrees default) and also detect side inclinations of each right and left sides (both 30 degrees). When the values are more than or equal to those front, back angles and side angles, the device will give notification with an output sensor. So when the user turns on the device, no matter how the device is placed/positioned it starts measuring the angles from that point.

My code at the moment looks like this:

#include <Wire.h>
#include <MPU9250.h>

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;
float x = 0 ;
float y = 0 ;
float z = 0 ;
int x_start = 0;
int y_start = 0;
int x_now = 0;
int y_now = 0;;
void setup() {
// serial to display data
Serial.begin(115200);
while(!Serial) {}

// start communication with IMU
status = IMU.begin();
if (status < 0) {
Serial.println(“IMU initialization unsuccessful”);
Serial.println(“Check IMU wiring or try cycling power”);
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
x = atan(x/(sqrt(yy + zz)));
y = atan(y/(sqrt(xx + zz)));
x_start = (int)(x * 180/M_PI);
y_start = (int)(y * 180/M_PI);
}
void loop() {
// read the sensor
IMU.readSensor();
x = IMU.getAccelX_mss();
y = IMU.getAccelY_mss();
z = IMU.getAccelZ_mss();

// Print raw values
Serial.print(“x = “);
Serial.print(x);
Serial.print(” y = “);
Serial.print(y);
Serial.print(”, z = “);
Serial.print(z);
Serial.println(” m/s^2”);

// Convert acceleration to angular movement in radians
x = atan(x/(sqrt(yy + zz)));
y = atan(y/(sqrt(xx + zz)));
// Convert radians to degrees and ignore the decimal point
x_now = (int)(x * 180/M_PI);
y_now = (int)(y * 180/M_PI);

// Calculate tilt angle with respect to original at rest position
int x_tilt = x_now - x_start;
int y_tilt = y_now - y_start ;

// Beep
if ((x_tilt < -20) || (x_tilt > 40) || (y_tilt < -30) || (y_tilt > 30)) {
// beep function to make noise
Serial.println("===================");
Serial.println(" BEEP !!! “);
Serial.println(”===================");

}
Serial.print("x_tilt = ");
Serial.println(x_tilt);
Serial.print("y_tilt = ");
Serial.println(y_tilt);

delay(2000);
}

This code displays the angle changes on serial monitor, however, the initial starting value is not at 0. When hold my sensor still, and turn on the serial monitor it displays this:

when first opens the serial monitor/ sensor positioned randomly:

x = 6.91 y = 7.23, z = 1.91 m/y_tilt = 60
x = 3.24 y = 0.57, z = -10.40 m/s^2
x_tilt = 17
y_tilt = 3
x = 8.12 y = -4.32, z = 3.22 m/s^2

BEEP !!!

x_tilt = 56
y_tilt = -52
x = 9.36 y = -2.23, z = 2.47 m/s^2

BEEP !!!

So my intention is to have 0 as a default value no matter how the device is positioned but I keep getting already set numbered angle values at the moment.
Can anyone help me to fix this? thank you very much.

Please use code tags when posting code.

You are not using the correct formulas for tilt angles. See this short tutorial: How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot.

But in either case, using those formula, zero tilt is defined when the accelerometer Z axis is straight up or down.

It is much, much more complicated to have an arbitrary sensor orientation designated as "zero", in which case you have to use the full 3D angle sensor capabilities. The Madgwick/Mahony AHRS filter works well for that, but you must do matrix or quaternion multiplication to correct for the arbitrary starting orientation.