initial version

Dependencies:   MMA8451Q

Fork of Accelerometer_example by William Marsh

main.cpp

Committer:
cvmanuel
Date:
2018-02-16
Revision:
2:ca0eb23626c5
Parent:
0:a1caba5c4e48
Child:
3:d3a5c64bbcf9

File content as of revision 2:ca0eb23626c5:

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

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

#define MMA8451_I2C_ADDRESS (0x1d<<1)
  
Thread pollT;
Timer timer;
Ticker tick;

enum accelerometerPos { flat, left, right, up, down, over };


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
        float threshold = .50;
    float min_threshold = 0.25;



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

    while (true) {
        float x, y, z;
        x = acc.getAccX();
        y = acc.getAccY();
        z = acc.getAccZ();

        
       if(z>threshold && abs(x)< min_threshold && abs(y)< min_threshold)
        {
          rled = 1.0f - abs(x);
          gled = 1.0f - abs(y);
          bled = 1.0f - abs(z);
          pc.printf("FLAT\n\r");
          pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
        timer.start();
          
        }else
        if(y<-threshold && abs(z)<min_threshold && abs(x)<min_threshold)
        {
            rled = 1.0f - abs(x);
        gled = 1.0f - abs(y);
        bled = 1.0f - abs(z);
            pc.printf("RIGHT\n\r");
            pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
            pc.printf("The time taken was: %f seconds\n\r",timer.read());
            
        }
        
        
        
        
        
        
        
        
        Thread::wait(300);
        timer.stop();
        
        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);
        
    }
}