Code for accelerometer Lab 4 Code 1

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Revision:
2:a1282101390e
Parent:
0:a1caba5c4e48
--- a/main.cpp	Wed Feb 07 16:56:55 2018 +0000
+++ b/main.cpp	Fri Feb 16 23:37:15 2018 +0000
@@ -19,14 +19,60 @@
     pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
 
     while (true) {
-        float x, y, z;
+        float x, y, z, trled, tgled, tbled;
         x = acc.getAccX();
         y = acc.getAccY();
         z = acc.getAccZ();
+        trled = 1.0f - abs(x);
+        tgled = 1.0f - abs(y);
+        tbled = 1.0f - abs(z); 
+        
+        if (trled>0.8 && tgled > 0.8)
+        {
         rled = 1.0f - abs(x);
         gled = 1.0f - abs(y);
-        bled = 1.0f - abs(z);
+        bled = 1.0f - abs(z); 
+        if (z>0){
+        pc.printf("FLAT\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+            }
+            else{
+                pc.printf("OVER\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);}
+        }
+           else if (tgled > 0.8 && tbled > 0.8)
+            {
+               rled = 1.0f - abs(x);
+        gled = 1.0f - abs(y);
+        bled = 1.0f - abs(z); 
+        if (x>0){
+        pc.printf("DOWN\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+            }
+            else{
+                pc.printf("UP\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);}
+                }
+             else   if (trled>0.8 && tbled > 0.8)
+                {
+                rled = 1.0f - abs(x);
+        gled = 1.0f - abs(y);
+        bled = 1.0f - abs(z); 
+        if (y>0){
+        pc.printf("LEFT\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+            }
+            else{
+                pc.printf("RIGHT\n");    
+        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);}
+                }
+                else {
+                   rled = 1;
+        gled = 1;
+        bled = 1; 
+                    }
         Thread::wait(300);
-        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
+       // if (gled == 0 || bled == 0 || rled == 0){
+      //  pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);}
     }
 }