Step 2: Detect the Orientation of the Development Board

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
novinfard
Date:
Fri Feb 16 20:11:08 2018 +0000
Parent:
1:31f0f53b08bd
Child:
3:5aa36278a6fe
Commit message:
Initial Commit (NOT COMPLETED YET)

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	Fri Feb 16 20:11:08 2018 +0000
@@ -19,14 +19,38 @@
     pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
 
     while (true) {
-        float x, y, z;
+        float x, y, z, ts;
+        //threshold
+        ts = 0.2;
         x = acc.getAccX();
         y = acc.getAccY();
         z = acc.getAccZ();
         rled = 1.0f - abs(x);
         gled = 1.0f - abs(y);
         bled = 1.0f - abs(z);
+        
+        // black
+        rled = 1;
+        gled = 1;
+        bled = 1;
+        
+        if( abs(x) <= 1+ts && abs(x)>= 1-ts) {
+            rled = 0   ;
+        }
+        if( abs(y) <= 1+ts && abs(y)>= 1-ts) {
+            gled = 0   ;
+        }
+        if( abs(z) <= 1+ts && abs(z)>= 1-ts) {
+            bled = 0   ;
+        }
+        
+        if(rled == 0 && gled == 0 && bled ==1)
+            pc.printf("Flat");
+        else if (rled == 0 && gled == 0 && bled ==1)
+            pc.printf("Flat");
+        
+        
         Thread::wait(300);
-        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
+       pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
     }
 }