Detecting sudden upward motion in Android
<p>I'm trying to detect an upward motion on my Android device but am finding this quite difficult. I've written the following code, and it works for all sudden motion (i.e. fast acceleration) on all directions. However, I would like this to work only for a sudden upward motion. Any help is greatly appreciated.</p>

<pre><code>public void onSensorChanged(SensorEvent event) {
            // alpha is calculated as t / (t + dT)
            // with t, the low-pass filter's 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]) &gt; 10.0f 
                    &amp;&amp; Math.abs(linear_acceleration[2] - linear_acceleration_old[2]) &gt; 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;
            }
        }
</code></pre>
