Lab_3 complete with questions
Dependencies: mbed C12832 MMA7660
Diff: main.cpp
- Revision:
- 1:9dca98a5b948
- Parent:
- 0:192cc7653fff
- Child:
- 2:31b0dedb8d0f
--- a/main.cpp Tue May 04 12:23:11 2021 +0000 +++ b/main.cpp Tue May 18 15:30:55 2021 +0000 @@ -1,27 +1,19 @@ -// Mbed compared with Iphone app -// Test state Flat USB axis LCD axis -// Iphone X:0.50, Y:0.50 X: 20.16, Y:0.56 X: 0.92, Y:34.62 -// Mbed Pitch 2.7232, slope -2.7232 Pitch 20.1992, slope -2.8273 Pitch 0.0000, slope 35.2176 -// Result = Manually testing both Iphone and Mbed, the results are very similar. This confirms the code works correctly. - -// Improving the accuracy. -// 1) as the LCP1768 is mounted to the application board, this unit is not level as different length pins extrude the bottom of the PCB. -// I would mount the application board into a level holder. - -#include "mbed.h" // Stephen Reidy; Lab3 +#include "mbed.h" // Stephen Reidy; Q13 #include "MMA7660.h" // Include Accelerometer header -#include "C12832.h" // Include LCD header file MMA7660 MMA(p28, p27); // Accelerometer pins initialised -Serial pc(USBTX, USBRX); // Transmit & Receiv pins initialised -C12832 lcd(p5, p7, p6, p8, p11); // LCD pins initialised +Serial pc(USBTX, USBRX); // Transmit & Receive pins initialised +int Zaxis_n; +DigitalOut RED(p23); +DigitalOut GREEN(p24); +DigitalOut BLUE(p25); DigitalOut connectionLed(LED1); // led output float pi = 3.14159; // pi value set -float CalculatePitch (float x, float y, float z) // Pitch calculate function +float CalculateX (float x, float y, float z) // Pitch calculate function { float pitch = 0.0; // pitch initialised pitch = atan(x /sqrt((y*y)+(z*z)) ); // pitch calculations @@ -29,7 +21,7 @@ return pitch; } -float CalculateSlope (float x, float y, float z) // Slope calculate function +float CalculateY (float x, float y, float z) // Slope calculate function { float slope = 0.0; // slope initialised slope = atan(y /sqrt((x*x)+(z*z)) ); // slope calculations @@ -39,17 +31,38 @@ int main() //Main structure { + while (1) { + float XResult = CalculateX(MMA.x(),MMA.y(), MMA.z()); //X result initialised + float YResult = CalculateY (MMA.x(), MMA.y(), MMA.z()); //Y result initialised + pc.printf("Pitch is %.4f degrees, and slope is %.4f degrees \n\r", XResult, YResult); //PC print slope (Y)& pitch (X) (Tera term) - if (MMA.testConnection()) { // test if accelerometer is connected, if yes LED oN - connectionLed = 1; - } - + if (Zaxis_n = -MMA.z()) { // Display blue LED for z axis motion + RED = 1; //blue + GREEN = 1; + BLUE = 0; + wait(.5); + } - while (1) { - float pitchResult = CalculatePitch(MMA.x(),MMA.y(), MMA.z()); //Pitch result initialised - float SlopeResult = CalculateSlope (MMA.x(), MMA.y(), MMA.z()); //slope result initialised - lcd.printf("pitch = %.4f degrees\n\r", pitchResult); // LCD print pitch result - pc.printf("Pitch is %.4f degrees, and slope is %.4f degrees \n\r", pitchResult, SlopeResult); // PC print slope& pitch (Tera term) - wait(5); // Delay 5 seconds + if (XResult >= 5) { // Display green LED for x axis motion + RED = 1; //green + GREEN = 0; + BLUE = 1; + wait(.5); + } + + if (YResult >= 5) { // Display blue LED for y axis motion + RED = 0; //red + GREEN = 1; + BLUE = 1; + wait(.5); + wait(.5); + } + + else { // LED off + RED = 1; // off + GREEN = 1; + BLUE = 1; + wait(.5); + } } }