Peiling Yi
/
Accelerometer_peilingmodify
Lab4-1
Fork of Accelerometer_example by
main.cpp
- Committer:
- Peilingyi
- Date:
- 2018-02-16
- Revision:
- 2:589ab4e1c22e
- Parent:
- 0:a1caba5c4e48
- Child:
- 3:79e48bb0fa93
File content as of revision 2:589ab4e1c22e:
#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); DigitalOut led1(LED1); DigitalOut led2(LED1); DigitalOut led3(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; float errorrange = 0.05; float threshold = 0.2; int filter = 4; int countflag = 0; //int entry = 0; x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); Thread::wait(300); pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); if ((abs((x+y+z)-1))<threshold) { if ((abs(z-1)<=errorrange)) { if (countflag == 0) { pc.printf("flat"); countflag = filter; led1= 1; led2=1; led3 =0; } else { countflag--; } } if (abs(x-1)<=errorrange) { if (countflag == 0) { pc.printf("left"); countflag = filter; led1= 1; led2=0; led3 =1; } else { countflag--; } } if (abs(x+1.0f)<=errorrange) { if (countflag == 0) { pc.printf("right"); countflag = filter; led1= 0; led2=1; led3 =1; } else { countflag--; } } } } }