I am trying to detect upward movement on my Android device, but find it quite complicated. I wrote the following code and it works for all sudden movements (i.e. Fast acceleration) in all directions. However, I would like this to work only for a sudden upward movement. Any help is appreciated.
public void onSensorChanged(SensorEvent event) { // alpha is calculated as t / (t + dT) // with t, the low-pass filter time-constant // and dT, the event delivery rate final float alpha = 0.8f; gravity = new float[3]; linear_acceleration = new float[3]; linear_acceleration_old = new float[3]; gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; System.out.println(linear_acceleration[0] + " " + linear_acceleration[1]+" " +linear_acceleration[2]); //detects swift movements, but I want to detect only upward ones if((Math.abs(linear_acceleration[1] - linear_acceleration_old[1]) > 10.0f && Math.abs(linear_acceleration[2] - linear_acceleration_old[2]) > 5.0f) || isFirstEvent) { if(!isFirstEvent) //do something linear_acceleration_old[0] = linear_acceleration[0]; linear_acceleration_old[1] = linear_acceleration[1]; linear_acceleration_old[2] = linear_acceleration[2]; isFirstEvent= false; } }
source share