# accelerometer to tilt in quadrants 1 and 2 only

below is my illustration/vandal of something im working on. im planning on using this for tilt detection of a vehicle. basically i want the accelerometer to detect tilt in quadrants 1 and 2 only. quadrant 1 will give me [0 to 90] degrees quadrant 2 will give me [-90 to 0] degrees

how can i accomplish this? atan (y,x) will only give values in the the -pi to pi range. thanks!

kaljuan:
How can i accomplish this? atan (y,x) will only give values in the the -pi to pi range. thanks!

The standard library function atan2(y,x) gives values from -PI to PI:

``````void setup()
{
Serial.begin(9600);
for (float y = 1; y >= -1; y -= 2) {
for (float x = 1; x >= -1; x -= 2) {
Serial.print("atan2("),
Serial.print(y);
Serial.print(",");
Serial.print(x);
Serial.print(") = ");
Serial.print(z);
Serial.println(" degrees.");
}
}
}

void loop(){}

{
return theta*180.0/PI;
}
``````

Output:

``````[color=blue]atan2(1.00,1.00) = 45.00 degrees.
atan2(1.00,-1.00) = 135.00 degrees.
atan2(-1.00,1.00) = -45.00 degrees.
atan2(-1.00,-1.00) = -135.00 degrees.[/color]
``````

quadrant 1 will give me [0 to 90] degrees
quadrant 2 will give me [-90 to 0] degrees

Hmmm… that’s not the way that atan2 is defined. That’s not the way that it’s taught in math class (at least that’s not the way I learned it, a century or so ago).

Here’s the usual coordinate system:

Positive X-axis to the right, Positive Y-axis goes up. The angle Theta is equal to zero for points on the positive part of the X-axis, and Theta increases in the counter-clockwise direction. Adding any integer multiple of 2 * Pi radians gets you back to the same angle.

Then:

``````     Quadrant 1 is x > 0, y > 0  (Theta between zero and Pi 2 radians, increasing as theta goes counter-clockwise)
Quadrant 2 is x < 0, y > 0  (Theta between Pi/2 and Pi radians)
Quadrant 3 is x < 0, y < 0  (Theta between Pi and 3 * Pi/2 radians--- same as Theta between -Pi and -Pi/2)
Quadrant 4 is x > 0, y < 0  (Theta between 3 * Pi/2 and 2 * Pi radians--- same as Theta between -Pi/2 and zero
``````

)

I mean, you can define axes and angles anywhichway you want (rotate, reflect, whatever…), but you may have to change your interpretation of the results of standard math functions that use conventional notation.

Regards,

Dave

And pi and -pi is the same as 90 and -90 degrees.

Senso: And pi and -pi is the same as 90 and -90 degrees.

No, they are not.

Regards,

Dave

Ups.. Pi is 180 degrees, just measure till pi/2