Calculate angle

Dependencies:   mbed MMA7660

Committer:
t00214916
Date:
Sat Aug 21 22:22:44 2021 +0000
Revision:
0:7c1f435ce121
Calculate angle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t00214916 0:7c1f435ce121 1 #include "mbed.h"
t00214916 0:7c1f435ce121 2 #include "MMA7660.h"
t00214916 0:7c1f435ce121 3 #define M_PI 3.14 // Giving PI a value
t00214916 0:7c1f435ce121 4 MMA7660 MMA (p27,p27); // Sensor class
t00214916 0:7c1f435ce121 5
t00214916 0:7c1f435ce121 6 Serial pc (USBTX, USBRX);
t00214916 0:7c1f435ce121 7
t00214916 0:7c1f435ce121 8 float calculateAngle(float x, float y, float z);
t00214916 0:7c1f435ce121 9 // The code I found to convert rad to angle
t00214916 0:7c1f435ce121 10 float rad_to_deg = (180/M_PI);
t00214916 0:7c1f435ce121 11 float AngleRad = atan(x / (sqrt (pow(y,2) + pow(z,2))));
t00214916 0:7c1f435ce121 12 float angle = AngleRad * rad_to_deg;
t00214916 0:7c1f435ce121 13 return angle;
t00214916 0:7c1f435ce121 14
t00214916 0:7c1f435ce121 15 }
t00214916 0:7c1f435ce121 16
t00214916 0:7c1f435ce121 17 int main()
t00214916 0:7c1f435ce121 18 {
t00214916 0:7c1f435ce121 19 if (MMA.testConnection()) // Test connection
t00214916 0:7c1f435ce121 20 printf("Working\r\n");
t00214916 0:7c1f435ce121 21
t00214916 0:7c1f435ce121 22 else
t00214916 0:7c1f435ce121 23 printf("Not working\r\n");
t00214916 0:7c1f435ce121 24
t00214916 0:7c1f435ce121 25 while(1) { // Calculate the angle using the sensor
t00214916 0:7c1f435ce121 26 float angle = calculateAngle(MMA.x(), MMA.y(), MMA.z());
t00214916 0:7c1f435ce121 27 printf("Angle = %f Degrees\r\n", angle);
t00214916 0:7c1f435ce121 28 wait(3);
t00214916 0:7c1f435ce121 29 }
t00214916 0:7c1f435ce121 30 }
t00214916 0:7c1f435ce121 31 }