initial version accelerometer

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
cvmanuel
Date:
Thu Feb 15 18:11:08 2018 +0000
Parent:
1:31f0f53b08bd
Commit message:
initial version accelerometer

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Feb 07 16:56:55 2018 +0000
+++ b/main.cpp	Thu Feb 15 18:11:08 2018 +0000
@@ -14,6 +14,9 @@
     PwmOut gled(LED2);
     PwmOut bled(LED3);
     Serial pc(USBTX, USBRX); // tx, rx
+    float threshold = .50;
+    float min_threshold = 0.25;
+    
 
 
     pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
@@ -23,10 +26,63 @@
         x = acc.getAccX();
         y = acc.getAccY();
         z = acc.getAccZ();
-        rled = 1.0f - abs(x);
+        
+        //flat
+        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);
+          
+        }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);
+        }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("LEFT\n\r");
+            pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+        }else
+        if(x< -threshold && abs(z)<min_threshold && abs(y)<min_threshold)
+        {
+            rled = 1.0f - abs(x);
+        gled = 1.0f - abs(y);
+        bled = 1.0f - abs(z);
+            pc.printf("DOWN\n\r");
+            pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+        }else
+        if(x> threshold && abs(z)<min_threshold && abs(y)<min_threshold)
+        {
+            rled = 1.0f - abs(x);
+        gled = 1.0f - abs(y);
+        bled = 1.0f - abs(z);
+            pc.printf("UP\n\r");
+            pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
+        }else
+        if(z<-threshold && abs(y)<min_threshold && abs(x)<min_threshold)
+        {
+            rled = 1.0f - abs(x);
+        gled = 1.0f - abs(y);
+        bled = 1.0f - abs(z);
+            pc.printf("OVER\n\r");
+            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);
+        
     }
 }