Lab 3ES_ John Curran T00214119

Dependencies:   mbed MMA7660

main.cpp

Committer:
johnc89
Date:
2021-05-26
Revision:
8:6e8a4fe65820
Parent:
7:5bb36caad016

File content as of revision 8:6e8a4fe65820:

// 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"
DigitalOut redled(p23); // built in red led
DigitalOut greenled(p24); // built in blue led
DigitalOut blueled(p25); // built in blue led
Serial pc(USBTX,USBRX);// serial communications
MMA7660 MMA(p28, p27);

DigitalOut connectionLed(LED1);

float calculateAngle(float x, float y, float z)
{
    float angle = 0;
    angle= (atan(x/sqrt((y*y)+(z*z))))*180.0/ 3.14159265;
    return angle;

}
int main()
{
    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);
    }
}