show 90 degree angle

Im trying to make a lean angle sensor for a motorcycle, I have a code my only problem is when I tilt the mpu6050 sensor it goes from 0-360 degrees. How can I make it show 0-90 degrees whether it tilting to the right or to the left?

Current code.

#include<Wire.h>

const int MPU_addr=0x68;
int AcX,AcY,AcZ,

int minVal=265;
int maxVal=402;

double x;
double y;
double z;

void setup(){ Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600); }
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

Serial.print("Lean angle ");
Serial.println(x);
Serial.println("-----------");
delay(1); }

rotate it with the 0 degree top facing left

This code is all wrong:

  int xAng = map(AcX,minVal,maxVal,-90,90);
  int yAng = map(AcY,minVal,maxVal,-90,90);
  int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

Correct tilt code is given in this simplified tutorial: How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot

To ensure negative values become positive, use the abs() or fabs() functions.

I'd suggest you use a library for your MPU6050 like this one here

Once you install it, there is an example that does pitch and roll

Please read the forum guide in the sticky post, then modify your post above and put in the code tags.

How will your mpu6050 be mounted? By which I mean which way will the X, Y & Z arrows printed on the sensor PCB point?

Where did you get this code? Did you modify it at all? There seem to be some errors in it which indicate it has been altered by someone who was confused.

EDIT: several forum members beat me to it!

Im trying to make a lean angle sensor for a motorcycle

Is the bike moving? If so, forget an accelerometer.

PaulRB:
Please read the forum guide in the sticky post, then modify your post above and put in the code tags.

How will your mpu6050 be mounted? By which I mean which way will the X, Y & Z arrows printed on the sensor PCB point?

Where did you get this code? Did you modify it at all? There seem to be some errors in it which indicate it has been altered by someone who was confused.

EDIT: several forum members beat me to it!

I go the code online and modified it a bit since it was lengthy and I am a bit confused on the whole Arduino programing I am more familiar with Matlab.

jremington:
This code is all wrong:

  int xAng = map(AcX,minVal,maxVal,-90,90);

int yAng = map(AcY,minVal,maxVal,-90,90);
  int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);




Correct tilt code is given in this simplified tutorial: https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing

To ensure negative values become positive, use the abs() or fabs() functions.

The code ends up working though it shows the tilt angle in the x direction of the sensor or is the values shown not the currect angles?

JCA79B:
Is the bike moving? If so, forget an accelerometer.

Technically it would but since it's a school project I'm going to be presenting it in-front of class so as long as it shows the tilt angle in a stationary position I should be fine.

jonzjon:
I'm going to be presenting it in-front of class so as long as it shows the tilt angle in a stationary position I should be fine.

Then you will be the laughing stock of the entire class, when the professor explains why it won't work when the bike is moving. Don't do it! The professor needs a "fool" so that when he/she explains, the class will remember the lesson.

To measure the your angle, you need to measure the angle of the bike compared to the road. That is what matters, even if the road is not level (race tracks have banked turns for a reason). Perhaps some distance sensors, pointing outwards from the sides of the bike. But even those will not work well except at very steep tilt angles. At shallow angles, the sensor's outgoing signal will be bounced away and the echo will probably not be received.

I still do not see any code tags in your post. At the moment that is of course entirely academic...

The code ends up working

The angles will not be accurate, or correct according to any recognized 3D angular system.

But if it is just for a classroom demo, who cares?