title: Detecting accelerometer shakes android
body: <p>I'm trying to detect left, right, up and down shakes. I researched and found this code.</p>
  
  <pre><code>public void onSensorChanged(SensorEvent event) {
              // TODO Auto-generated method stub
  
              if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  
                  float[] values = event.values;
  
                  // Movement
                  float x = values[0];
                  float y = values[1];
                  float z = values[2];
  
  
                  long actualTime = System.currentTimeMillis();
                  if ((actualTime - lastUpdate) &gt; 100) 
                  {
                      long diffTime = (actualTime - lastUpdate);
                      lastUpdate = actualTime;
  
  
                      if(Round(x,4)&gt;8.0000){
                          Log.d("sensor", "=====LEFT====");
  
  
                      }
                      else if(Round(x,4)&lt;-8.0000){
                          Log.d("sensor", "=====RIGHT====");
  
                      }
                      else if(Round(z,4) &lt; -0.0){
                         Log.d("sensor", "=====UP====");
  
  
                      }
  
                      else if(Round(y,4) &lt; 1.0){
                          Log.d("sensor", "=====DOWN====");
  
                      }
  
                  float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
  
                  if (speed &gt; SHAKE_THRESHOLD) {
                      //Log.d("sensor", "shake detected w/ speed: " + speed);
                      //Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
                  }
                  last_x = x;
                  last_y = y;
                  last_z = z;
                  }
              }
  
          }
  </code></pre>
  
  <p>I tried to study it but I have some questions.<br>
  1. Where can I set the delay every after shake detection? I want it to detect a shake after 1 second of the last detected shake.<br>
  2. Why is that if I place it to a flat surface, it always detects down.<br>
  3. I can't detect a right movement shake<br></p>
  
  <p>Any help and suggestion will be appreciated.</p>