 const double zmid = (zmin + zmax) / 2.0;
 
 
+const double lowerBound = 1.25;
+const double upperBound = 1.75;
+const int STATE_ONE = 1;
+const int STATE_TWO = 2;
+const int STATE_THREE = 3;
+const int STATE_FOUR = 4;
+
+int state;
+double amax;
+double ax_max, ay_max, az_max;
+
 void setup()  
 {
   // use the baud rate your bluetooth module is configured to 
-  // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
+  // not all baud rates work well, i.e. ATMEGA168 works best with 57600
   Serial.begin(115200); 
+  
+  state = STATE_ONE;
 }
 
 void loop()
 {
+    double ax, ay, az;
+    double a;
+    
+    ax = 2 * (analogRead(pinX) - xmid) / xrange;
+    ay = 2 * (analogRead(pinY) - ymid) / yrange;
+    az = 2 * (analogRead(pinZ) - zmid) / zrange;
+    
+    a = sqrt(ax*ax + ay*ay + az*az);
+
+    switch (state) {
+      case STATE_ONE:
+        if (a >= lowerBound) {
+          state = STATE_TWO; 
+        }
+        break;
+      case STATE_TWO:
+        if (a >= upperBound) {
+          state = STATE_THREE; 
+          amax = 0; 
+        } else if (a < lowerBound) {
+          state = STATE_ONE;
+        }
+        break;
+      case STATE_THREE:
+        if (a > amax) {
+           amax = a;
+           ax_max = ax;
+           ay_max = ay;
+           az_max = az;
+        }
+        
+        if (a < upperBound) {
+           state = STATE_FOUR;
+        }
+        break;
+      case STATE_FOUR:
+        if (a >= upperBound) {
+          state = STATE_THREE;  
+        } else if (a < lowerBound) {
+          state = STATE_ONE;
+          
+          // gesture event
+          Serial.print(micros()); Serial.print(","); Serial.print(amax);
+          Serial.print(": ("); Serial.print(ax_max);
+          Serial.print(","); Serial.print(ay_max);
+          Serial.print(","); Serial.print(az_max);
+          Serial.println(")");          
+        }
+        break;
+    }    
+  
+    /*
     int xraw = analogRead(pinX);
     int yraw = analogRead(pinY);
     int zraw = analogRead(pinZ);

    Serial.print(","); Serial.print(yraw);
    Serial.print(","); Serial.print(zraw);
    Serial.println("");
    
    //delay(500);
  
  
  meetAndroid.receive(); // you need to keep this in your loop() to receive events
  
  // read input pin and send result to Android
  meetAndroid.send(pinX);
  
  // add a little delay otherwise the phone is pretty busy
  delay(100);
}



#include <MeetAndroid.h>

MeetAndroid meetAndroid;
