Lab 3ES_ John Curran T00214119

Dependencies:   mbed MMA7660

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Lab 3 Digital Sensors & equations in C
00002 // John Curran T00214119
00003 // This is a 3-axis accelerometer which prints results to Tera Term and also converts readings to degrees
00004 //Q2. The result is accurate however it could be improved by calibration of the sensor
00005 // or updating of software.
00006 // Below are results in a table on Tera-Term:
00007 //x0.093765, y0.000000, z0.984529, ang5.440332
00008 //x0.046882, y-0.046882, z0.984529, ang2.723230
00009 //x0.093765, y0.000000, z0.984529, ang5.440332
00010 //x0.093765, y0.000000, z1.031411, ang5.194428
00011 //x0.046882, y0.000000, z0.984529, ang5.440332
00012 //x0.046882, y-0.093765, z0.984529, ang5.415972
00013 //x0.093765, y0.000000, z1.031411, ang5.194428
00014 //x0.234412, y0.000000, z1.031411, ang12.804265
00015 //x0.515706, y0.468823, z0.797000, ang29.149376
00016 // line 7-13 is mbed lying flat on a table and readings are consistent 
00017 // line 14-15 is sensor being tilted giving different readings 
00018 #include "mbed.h"
00019 #include "MMA7660.h"
00020 DigitalOut redled(p23); // built in red led
00021 DigitalOut greenled(p24); // built in blue led
00022 DigitalOut blueled(p25); // built in blue led
00023 Serial pc(USBTX,USBRX);// serial communications
00024 MMA7660 MMA(p28, p27);
00025 
00026 DigitalOut connectionLed(LED1);
00027 
00028 float calculateAngle(float x, float y, float z)
00029 {
00030     float angle = 0;
00031     angle= (atan(x/sqrt((y*y)+(z*z))))*180.0/ 3.14159265;
00032     return angle;
00033 
00034 }
00035 int main()
00036 {
00037     while (1) {
00038         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()));
00039         wait(0.8);
00040     }
00041 }