Victor Szultka / Mbed 2 deprecated CPS_Lab2_3Axis_Acc

Dependencies:   mbed C12832

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
00002 
00003 #include "mbed.h"                                   // add mbed header
00004 #include "MMA7660.h"                                // add accelerometer header
00005 #include "C12832.h"                                 // add LCD screen header
00006 
00007 MMA7660 MMA(p28, p27);                              // definine accelerometer
00008 Serial pc(USBTX,USBRX);                             // define serial port tx,rx
00009 C12832 lcd(p5, p7, p6, p8, p11);                    // define LCD screen
00010 DigitalOut connectionLed(LED1);                     // define acc status LED
00011 float pi = 3.14159;                                 // abbreviated value for pi
00012 
00013 float calculatePitch(float x, float y, float z)     // define function to derive pitch angle
00014 {
00015     float pitch = 0.0;                              // initialise pitch
00016     pitch = atan( x /sqrt((y*y)+(z*z)) );           // define pitch equation
00017     pitch = pitch * (180/pi);                       // ammend from radians to degrees
00018     return pitch;                                   // return value of pitch
00019 }
00020 
00021 float calculateRoll(float x, float y, float z)      // define function to derive pitch angle
00022 {
00023     float roll = 0.0;                               // initialise roll
00024     roll = atan( y /sqrt((x*x)+(z*z)) );            // define roll equation
00025     roll = roll * (180/pi);                         // ammend from radians to degrees
00026     return roll;                                    // return value of roll
00027 }
00028 
00029 int main()                                                                                          // Start of main program
00030 {
00031     if (MMA.testConnection())                                                                       // Test if accelerometer functional
00032         connectionLed = 1;                                                                          // if functional light LED1
00033 
00034     while(1) {                                                                                      // continuous operation
00035         float pitchResult = calculatePitch(MMA.x(), MMA.y(), MMA.z());                              // define value for pitch
00036         float rollResult = calculateRoll(MMA.x(), MMA.y(), MMA.z());                                // define value for roll
00037         lcd.printf("Pitch = %.4f degrees\n\r", pitchResult);                                        // print statement for pitch [mbed]
00038         lcd.printf("Roll = %.4f degrees\n\n\r", rollResult);                                        // print statement for roll [mbed]
00039         pc.printf("Pitch is %.4f degrees, and Roll is %.4f degrees\n\r", pitchResult, rollResult);  // print statement for pitch and roll [serial]
00040         wait(5);                                                                                    // delay of 5 seconds between iterations of program
00041     }
00042 
00043 }