Victor Szultka
/
CPS_Lab2_3Axis_Acc
3 axis accellerometer for pitch and roll on lcd and serial output
Revision 5:5779c52fa70e, committed 2020-12-05
- Comitter:
- saltire78
- Date:
- Sat Dec 05 12:57:45 2020 +0000
- Parent:
- 4:36a163511e34
- Commit message:
- .
Changed in this revision
diff -r 36a163511e34 -r 5779c52fa70e C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Sat Dec 05 12:57:45 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/askksa12543/code/C12832/#990d5eec2ef6
diff -r 36a163511e34 -r 5779c52fa70e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 05 12:57:45 2020 +0000 @@ -0,0 +1,43 @@ +//Uses the measured z-acceleration to drive leds 2 and 3 of the mbed + +#include "mbed.h" // add mbed header +#include "MMA7660.h" // add accelerometer header +#include "C12832.h" // add LCD screen header + +MMA7660 MMA(p28, p27); // definine accelerometer +Serial pc(USBTX,USBRX); // define serial port tx,rx +C12832 lcd(p5, p7, p6, p8, p11); // define LCD screen +DigitalOut connectionLed(LED1); // define acc status LED +float pi = 3.14159; // abbreviated value for pi + +float calculatePitch(float x, float y, float z) // define function to derive pitch angle +{ + float pitch = 0.0; // initialise pitch + pitch = atan( x /sqrt((y*y)+(z*z)) ); // define pitch equation + pitch = pitch * (180/pi); // ammend from radians to degrees + return pitch; // return value of pitch +} + +float calculateRoll(float x, float y, float z) // define function to derive pitch angle +{ + float roll = 0.0; // initialise roll + roll = atan( y /sqrt((x*x)+(z*z)) ); // define roll equation + roll = roll * (180/pi); // ammend from radians to degrees + return roll; // return value of roll +} + +int main() // Start of main program +{ + if (MMA.testConnection()) // Test if accelerometer functional + connectionLed = 1; // if functional light LED1 + + while(1) { // continuous operation + float pitchResult = calculatePitch(MMA.x(), MMA.y(), MMA.z()); // define value for pitch + float rollResult = calculateRoll(MMA.x(), MMA.y(), MMA.z()); // define value for roll + lcd.printf("Pitch = %.4f degrees\n\r", pitchResult); // print statement for pitch [mbed] + lcd.printf("Roll = %.4f degrees\n\n\r", rollResult); // print statement for roll [mbed] + pc.printf("Pitch is %.4f degrees, and Roll is %.4f degrees\n\r", pitchResult, rollResult); // print statement for pitch and roll [serial] + wait(5); // delay of 5 seconds between iterations of program + } + +} \ No newline at end of file
diff -r 36a163511e34 -r 5779c52fa70e mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Dec 05 12:57:45 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file