Lab 4 Version 1

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

main.cpp

Committer:
AnastasiosBarlas
Date:
2018-02-16
Revision:
2:6edf3f4a21cb
Parent:
0:a1caba5c4e48

File content as of revision 2:6edf3f4a21cb:

#include "mbed.h"
#include "rtos.h"
#include "MMA8451Q.h"

  PinName const SDA = PTE25;
  PinName const SCL = PTE24;

#define MMA8451_I2C_ADDRESS (0x1d<<1)

int main(void)
{
    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    PwmOut rled(LED1);
    PwmOut gled(LED2);
    PwmOut bled(LED3);
    Serial pc(USBTX, USBRX); // tx, rx


    pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());

    while (true) {
        float x, y, z,t;
        t = 0.5;
        x = acc.getAccX();
        y = acc.getAccY();
        z = acc.getAccZ();
        rled = 1.0f - abs(x);
        gled = 1.0f - abs(y);
        bled = 1.0f - abs(z);
        if (y>=t && abs(x) < t && abs(z) < t ){
            pc.printf("down");        
        }
        else if (y<=t && abs(x) < t && abs(z) < t ){
            pc.printf("up");        
        }
        else if (x<=t && abs(y) < t && abs(z) < t ){
            pc.printf("left");        
        }
        else if (x>=t && abs(y) < t && abs(z) < t ){
            pc.printf("right");        
        }
        else if (z<=t && abs(y) < t && abs(x) < t ){
            pc.printf("over");        
        }
        else if (z>=t && abs(y) < t && abs(x) < t ){
            pc.printf("flat");        
        }
        else{
            rled = 1;
            gled = 1;
            bled = 1;
            }
        Thread::wait(300);
        //pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n \r", x, y, z);
    }
}