Lab 3ES_ John Curran T00214119

Dependencies:   mbed MMA7660

Revision:
6:50b706a10a68
Parent:
5:72d55b40feef
Child:
7:5bb36caad016
--- a/main.cpp	Sat Apr 17 18:34:38 2021 +0000
+++ b/main.cpp	Mon Apr 19 18:05:34 2021 +0000
@@ -1,40 +1,40 @@
-
+// Lab 3 Digital Sensors & equations in C
+// John Curran T00214119
+// This is a 3-axis accelerometer which prints results to Tera Term and also converts readings to degrees
+//Q2. The result is accurate however it could be improved by calibration of the sensor
+// or updating of software.
+// Below are results in a table on Tera-Term:
+//x0.093765, y0.000000, z0.984529, ang5.440332
+//x0.046882, y-0.046882, z0.984529, ang2.723230
+//x0.093765, y0.000000, z0.984529, ang5.440332
+//x0.093765, y0.000000, z1.031411, ang5.194428
+//x0.046882, y0.000000, z0.984529, ang5.440332
+//x0.046882, y-0.093765, z0.984529, ang5.415972
+//x0.093765, y0.000000, z1.031411, ang5.194428
+//x0.234412, y0.000000, z1.031411, ang12.804265
+//x0.515706, y0.468823, z0.797000, ang29.149376
+// line 7-13 is mbed lying flat on a table and readings are consistent 
+// line 14-15 is sensor being tilted giving different readings 
 #include "mbed.h"
 #include "MMA7660.h"
-#include <stdio.h>
-#include <math.h>
+
 #define PI 3.14159265
 Serial pc(USBTX,USBRX);// serial communications
 MMA7660 MMA(p28, p27);
 
 DigitalOut connectionLed(LED1);
 
-float calculateAngle(float x, float y, float z, float val, float ret)
+float calculateAngle(float x, float y, float z)
 {
-    float angle =0;
-
-    val = 180.0 / PI;
+    float angle = 0;
+    angle= (atan(x/sqrt((y*y)+(z*z))))*180.0/ 3.14159265;
+    return angle;
 
-    ret  = atan (x) * val;
-    ret  = atan (y) * val;
-    ret  = atan (z) * val;
-    ret  = ('atan (x) )/ (sqrt float  pow(y) *(y) ) + sqrt float pow(z)* val');
-    printf("The arc tangent of %lf is %lf degrees \n\r ", x, y, z, ret);
-
-
-    return angle;
 }
-
 int main()
 {
-    if (MMA.testConnection())
-        connectionLed = 1;
-
-    while(1) {
-        float val;
-        float ret;
-        pc.printf ("%f, \r\n", calculateAngle (MMA.x(), MMA.y(), MMA.z(),val,ret ));
-        wait (0.5);
+    while (1) {
+        pc.printf("x%f, y%f, z%f, ang%f\n\r",MMA.x(),MMA.y(),MMA.z(),calculateAngle(MMA.x(),MMA.y(),MMA.z()));
+        wait(0.8);
     }
-
 }