Lab_3 complete with questions

Dependencies:   mbed C12832 MMA7660

main.cpp

Committer:
stephensv650
Date:
2021-05-18
Revision:
1:9dca98a5b948
Parent:
0:192cc7653fff
Child:
2:31b0dedb8d0f

File content as of revision 1:9dca98a5b948:


#include "mbed.h"                // Stephen Reidy; Q13
#include "MMA7660.h"             // Include Accelerometer header

MMA7660 MMA(p28, p27);           // Accelerometer 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 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
    pitch = pitch * (180/pi);                    // pitch x 180/3.14159
    return pitch;
}

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
    slope = slope * (180/pi);                    // slope x 180/3.14159
    return slope;
}

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 (Zaxis_n = -MMA.z()) {   // Display blue LED for z axis motion
            RED = 1;                //blue
            GREEN = 1;
            BLUE = 0;
            wait(.5);
        }

        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);
        }
    }
}