Calculate angle

Dependencies:   mbed MMA7660

Revision:
0:7c1f435ce121
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Aug 21 22:22:44 2021 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+#include "MMA7660.h"
+#define M_PI 3.14   // Giving PI a value
+MMA7660 MMA (p27,p27);  // Sensor class
+
+Serial pc (USBTX, USBRX);
+
+float calculateAngle(float x, float y, float z);
+// The code I found to convert rad to angle
+    float rad_to_deg = (180/M_PI);  
+    float AngleRad = atan(x / (sqrt (pow(y,2) + pow(z,2))));
+    float angle = AngleRad * rad_to_deg;
+    return angle;
+    
+}
+
+int main()
+{
+    if (MMA.testConnection())   // Test connection
+    printf("Working\r\n");
+    
+    else 
+    printf("Not working\r\n");
+    
+    while(1) {  // Calculate the angle using the sensor
+        float angle = calculateAngle(MMA.x(), MMA.y(), MMA.z());
+        printf("Angle = %f Degrees\r\n", angle);
+        wait(3);
+        }
+    }
+}
\ No newline at end of file