I am working on an android project where I need to detect the exact directions when user turns (i.e. left or right or bottom or top). I have found the directions too by using accelerometer with magnetic sensor, but I couldn't fix the maximum values to be turned.
The problem I face is this: I am unable to make it detect the center point/position.
When a user turns the phone left from the central position, it detects it as a left turn.
However, when I return the position to the central position, the phone detects it as two movements: both left. My understanding is this: If the sensor had got the central position (point of origin) correctly fixed, the movements would be correctly detected. But, because the central position is not fixed, this is not happening.
Kindly guide me in fixing it. Do let me know if you need further clarifications.
My code is as below
public void onSensorChanged(SensorEvent event) {
azimuth = event.values[0]; // azimuth
pitch = event.values[1]; // pitch
roll = event.values[2]; // roll
if (pitch < -45 && pitch > -135) {
// top
currentSide = Side.TOP;
} else if (pitch > 45 && pitch < 135) {
// bottom
currentSide = Side.BOTTOM;
} else if (roll > 45) {
// right
currentSide = Side.RIGHT;
} else if (roll < -45) {
// left
currentSide = Side.LEFT;
}
if (currentSide != null && !currentSide.equals(oldSide)) {
switch (currentSide) {
case TOP :
listener.onTopUp();
break;
case BOTTOM :
listener.onBottomUp();
break;
case LEFT:
listener.onLeftUp();
break;
case RIGHT:
listener.onRightUp();
break;
}
oldSide = currentSide;
}
// forwards orientation to the OrientationListener
listener.onOrientationChanged(azimuth, pitch, roll);
}
Thanks for helping!