accelerometer

Dependencies:   mbed MMA7660

Revision:
2:b99449535e77
Parent:
1:19c7e004b469
--- a/main.cpp	Wed Aug 19 11:15:23 2020 +0000
+++ b/main.cpp	Wed Aug 19 12:25:24 2020 +0000
@@ -1,37 +1,27 @@
-
-#include "mbed.h"
-#include "MMA7660.h"
-
+#include "mbed.h"//preprosser command
+#include "MMA7660.h"//preprocesser command
+ 
 #define PI 3.14159265
  
+Serial pc(USBTX,USBRX);//serial transmission 
+MMA7660 MMA(p28,p27);//IC2 pins  accelerometer
  
-MMA7660 MMA(p28, p27);
-DigitalOut connectionLed(LED1);
-
-float calculateAngle(float x, float y, float z){
+float calculateAngle(float x,float y,float z){
     float angle = 0;
-    double val = 180 / PI;
-    
-    double bot_part = sqrt( pow((double)y, 2.0) + pow((double)z, 2.0) );
-    double top_part = (double)x;
-    angle = atan(top_part/bot_part);
-    
-    return angle*val;
+// calculate of angle
+ 
+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;
 }
-int main() { 
-
-if (MMA.testConnection()) //LED shows connection is ok
-connectionLed = 1;
-   
-    while(1) {
-        double res = calculateAngle(MMA.x(), MMA.z(), MMA.x());
-        printf("result = %f \r", res);
-        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()));
+        wait(1);//wait 1 second
+    }
+}
\ No newline at end of file