University project with FRDM-KL25Z

Dependencies:   mbed MMA8451Q

Files at this revision

API Documentation at this revision

Comitter:
rolandtamas
Date:
Tue Jan 11 19:12:13 2022 +0000
Parent:
0:81d50ca17412
Commit message:
Final version of the project

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jan 04 11:05:22 2022 +0000
+++ b/main.cpp	Tue Jan 11 19:12:13 2022 +0000
@@ -22,27 +22,36 @@
     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
     PwmOut rled(LED1);
     PwmOut gled(LED2);
-    //PwmOut bled(LED3);
-
-    printf("MMA8451 ID: %d\n", acc.getWhoAmI());
+    
+    float x_data, y_data;
+    float x_ref = 0.05f;
+    float y_ref = 0.05f;
+    
+    printf("MMA8451 ID: %d\n", acc.getWhoAmI()); //acts as a reset message
 
     while (true) {
-        float x;
-        x = acc.getAccX();
-        if(x < 0.0) {
+        x_data = acc.getAccX(); // detect pitch
+        y_data = acc.getAccY(); //detect roll
+        
+        if(abs(x_data) > x_ref)
+        {
+            gled = 1.0f;
+            printf("Pitch detected !!!\n");
+            wait(MAXBLINKSPEED * abs(x_data)); //speed of blinking changes depending on pitch position
+            gled = 0.0f;
+        }
+        if(abs(y_data) > y_ref)
+        {
             rled = 1.0f;
-            wait(MAXBLINKSPEED * abs(x));
+            printf("Roll detected !!!\n");
+            wait(MAXBLINKSPEED * abs(y_data)); //speed of blinking changes depending on roll position
             rled = 0.0f;
         }
-        else if(x>=0.0){
-            gled = 1.0f;
-            wait(MAXBLINKSPEED * abs(x));
-            gled = 0.0f;
+        if(abs(x_data) == x_ref && abs(y_data) == y_ref)
+        {
+            printf("Board is leveled !!!\n");
         }
-        //rled = 1.0f - x;
-        //gled = 1.0f - y;
-        //bled = 1.0f - z;
-        //wait(0.1f);
-        printf("X: %1.2f\n", x);
+        printf("X: %.2f, Y: %.2f\n", x_data, y_data);
+        
     }
 }