I'm using accelometer sensor to detect whether my device is flat on a table or not. The weird thing is even when I put my phone flat or rotate it on it's side the value is always between 90 and 100! This shouldn't be correct! am I missing something? Here is my code:
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x / norm_Of_g);
y = (y / norm_Of_g);
z = (z / norm_Of_g);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
Log.i("tag","incline is:"+inclination);
if (inclination < 25 || inclination > 155)
{
// device is flat
Toast.makeText(this,"device flat - beep!",Toast.LENGTH_SHORT);
}
Edit: I'm using this code : How to measure the tilt of the phone in XY plane using accelerometer in Android
You're using the y-axis instead of the z-axis as used in the answer you linked.
The value of acos will be near-zero when the argument is near one (or near 180 degrees when near negative one), as seen in this picture:
As such, your inclination will be near zero (or 180) degrees only when the y axis is normalized to about one or negative one, eg when it is parallel to gravity, (thus, the device is "standing up").
If there's no other error, simply switching from:
to
should do it.