Lab3 Final copy

Dependencies:   mbed MMA7660

Revision:
5:af5a6c9606e6
Parent:
4:c3bb91b3b992
Child:
6:25a7f695e570
--- a/main.cpp	Mon May 03 16:43:17 2021 +0000
+++ b/main.cpp	Mon May 03 17:40:07 2021 +0000
@@ -2,23 +2,33 @@
 //embedded systems lab question 3 Aidan Cleary T00209564
 //Lab 3: Digital sensors & equations in C
 
-//Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
+#include "mbed.h"//preprosser command
+#include "MMA7660.h"//preprocesser command
+
+#define PI 3.14159265//PI defined 
 
-#include "mbed.h"
-#include "MMA7660.h"
+Serial pc(USBTX,USBRX);//serial transmission to pc via USBTX, USBRX
+MMA7660 MMA(p28,p27);//IC2 pins associated with accelerometer
 
-MMA7660 MMA(p28, p27);
-
-DigitalOut connectionLed(LED1);
+float calculateAngle(float x,float y,float z)
+{
+    float angle = 0;
+// calculate the angle from the formula given in the instructions
 
-int main()
-{
-    if (MMA.testConnection())
-        connectionLed = 1;
+    angle = (y*y) + (z*z);//bottom of the division line
+    angle = sqrt (angle);//square root
+    angle = x/angle;
+    angle = atan (angle);//arctan of angle
+    angle = angle *180/PI; //radians to degrees!
+    return angle;
+}
 
-    while(1) {
-        printf("%f, %f, %f \r\n", MMA.x (), MMA.y(), MMA.z());
-        wait(0.5);
+int main() //main program
+{
+    while (1) {
+        pc.printf("    X%f     Y%f      Z%f     angle%.2f degrees\r", MMA.x(),
+                  MMA.y(), MMA.z(), calculateAngle(MMA.x(), MMA.y(), MMA.z()));
+        //i have used \r to repeat the function and keep the tera term tidy
+        wait(1);//wait 1 second
     }
-
-}
+}
\ No newline at end of file