Prints Pitch & Slope Angles

Dependencies:   mbed MMA7660

main.cpp

Committer:
t00203814
Date:
2019-08-07
Revision:
0:885c71e3dd67

File content as of revision 0:885c71e3dd67:

#include "mbed.h"//includes mbed header
#include "MMA7660.h"//includes MMA7660 header
 
#define PI 3.14159265// defines PI as 3.14159265

Serial pc(USBTX, USBRX);// sets up serial connection to PC
MMA7660 MMA(p28, p27);// declares MMA7660 on P27 & p28 and calles them MMA
 
 
float calculateAngle(float x, float y, float z) {// sets up a float values called calculateAngler, x, y & z
float angle = 0;// iniates a float value called angle and sets it to zero
    angle = (atan(x/sqrt((y*y)+(z*z)))*180/PI);//Calculating the pitch from formular in instructions
    pc.printf("Pitch angle is %.3f degrees\n\n\r",angle);
    angle = (atan(y/sqrt((x*x)+(z*z)))*180/PI);//Calculating the roll from formular in instructions
    pc.printf("Roll angle is %.3f degrees\n\n\r",angle);
    wait(3);// waits 3 seconds
    return angle;
}
 
  
 
int main() {  
 
    while(1) {
        pc.printf("     X %f       Y %f      Z %f    angle %.2f degrees \n", MMA.x(), MMA.y(), MMA.z(), calculateAngle(MMA.x(), MMA.y(), MMA.z()));// prints to Pc
          wait(1);// waits 1 second
 
}   
    }