I want to trigger some function when the phone is tilted back and forth along the YZ axis. If you were holding your phone in front of your face, you'd be tilting it back until it's nearly parallel with the floor, then back up until it's perpendicular to the floor. Looking at the drawing below, it would be the pitch.
I've tried to do so by only looking at the Z value of the accelerometer. Detecting a change in Z should theoretically be enough to detect a change in pitch, but in practice it is wildly erratic based on the sampling rate and the threshold for the change. Someone tilting the phone back and forth quickly will have a different result than someone doing so slowly. How do I make it so I'm not reliant on the sampling rate and the threshold for the tilt?
@Override
public void onSensorChanged(SensorEvent event) {
newZ = event.values[2];
if (Math.abs(oldZ - newZ) > 3) {
//do something
}
oldZ = newZ;
}