initial version

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
cvmanuel
Date:
Thu Feb 22 14:58:13 2018 +0000
Parent:
2:ca0eb23626c5
Commit message:
changes;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ca0eb23626c5 -r d3a5c64bbcf9 main.cpp
--- a/main.cpp	Fri Feb 16 17:39:18 2018 +0000
+++ b/main.cpp	Thu Feb 22 14:58:13 2018 +0000
@@ -11,7 +11,7 @@
 Timer timer;
 Ticker tick;
 
-enum accelerometerPos { flat, left, right, up, down, over };
+enum accelerometerPos { flat, left, right, up, down, over, failure, trigger };
 
 
 int main(void)
@@ -26,34 +26,101 @@
 
 
 
+
     pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
+    
+    accelerometerPos pos = flat;
+    bool restart = true;
 
     while (true) {
         float x, y, z;
         x = acc.getAccX();
         y = acc.getAccY();
         z = acc.getAccZ();
+        
 
+       if(pos!=trigger){ 
         
        if(z>threshold && abs(x)< min_threshold && abs(y)< min_threshold)
         {
-          rled = 1.0f - abs(x);
-          gled = 1.0f - abs(y);
-          bled = 1.0f - abs(z);
-          pc.printf("FLAT\n\r");
-          pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
-        timer.start();
+            if(pos==failure)
+            {
+                if(restart){
+                    timer.reset();
+                    timer.start();
+                    restart = false;
+                }
+                if(timer.read()>=10)
+                {
+                    rled=0;
+                    gled=0;
+                    bled=0;
+                    pos= flat;
+                    timer.reset();
+                    restart = true;
+                    
+                }
+            }else
+            if(pos==up && timer.read()>=4 && timer.read()<=8)
+            {
+                rled=1;
+                gled=0;
+                bled=1;
+                pc.printf("TRIGGER!!!!\n\r");
+                pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+                pc.printf("Time taken was: %f \n\r",timer.read());
+                pos = trigger;
+                
+            }else if(pos!=trigger){
+                  pc.printf("FLAT\n\r");
+                  pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+                  timer.start();
+                  pos = flat;
+                  pc.printf("Time taken was: %f \n\r",timer.read());
+                }
+
           
         }else
         if(y<-threshold && abs(z)<min_threshold && abs(x)<min_threshold)
         {
-            rled = 1.0f - abs(x);
-        gled = 1.0f - abs(y);
-        bled = 1.0f - abs(z);
-            pc.printf("RIGHT\n\r");
-            pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+            if(pos==flat && timer.read()>=10)
+            {
+                    pc.printf("RIGHT\n\r");
+                    pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+                    pc.printf("The time taken was: %f seconds\n\r",timer.read());
+                    pos = right;
+                    timer.reset();
+                    timer.start();
+            }else if(pos!=right){
+                    rled=0;
+                    gled=1;
+                    bled=1;
+                    pc.printf("ERROR in time sequence, time taken: %f \n\r",timer.read());
+                    pos = failure;
+                }
+                
+                pc.printf("The time taken was: %f seconds\n\r",timer.read());
+            
+        }else
+        if(x< -threshold && abs(z)<min_threshold && abs(y)<min_threshold)
+        {
+            if(pos == right && timer.read()>=2 && timer.read()<=6)
+            {
+                pc.printf("UP\n\r");
+                pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+                pc.printf("The time taken was: %f seconds\n\r",timer.read());
+                pos = up;
+                timer.reset();
+                timer.start();
+            }else if(pos!=up) {
+                    rled=0;
+                    gled=1;
+                    bled=1;
+                    pc.printf("ERROR in time sequence, time taken: %f \n\r",timer.read());
+                    pos = failure;
+                }
             pc.printf("The time taken was: %f seconds\n\r",timer.read());
-            
+        }
         }
         
         
@@ -64,7 +131,7 @@
         
         
         Thread::wait(300);
-        timer.stop();
+        //timer.stop();
         
         pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);