deni setiawan
/
Accelerometer_example_w4
11
Fork of Accelerometer_example by
Revision 2:ed3e92bcae80, committed 2018-02-16
- Comitter:
- dhenis
- Date:
- Fri Feb 16 12:21:52 2018 +0000
- Parent:
- 1:31f0f53b08bd
- Commit message:
- 1;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 31f0f53b08bd -r ed3e92bcae80 main.cpp --- a/main.cpp Wed Feb 07 16:56:55 2018 +0000 +++ b/main.cpp Fri Feb 16 12:21:52 2018 +0000 @@ -1,32 +1,103 @@ #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; + + float lower_th, upper_th; + x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); - rled = 1.0f - abs(x); - gled = 1.0f - abs(y); - bled = 1.0f - abs(z); + + // Thread::wait(300); - pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); + + if(z>= 0.9 ){ // flat + // myled1 = 0; + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + + pc.printf("flat \n \r"); + + }else if(y>= 0.9 ){//right + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + pc.printf("right \n \r"); + + }else if(x>= 0.9){ // dowm + + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + pc.printf("down \n \r"); + + }else if(z<= -0.90 ){ //over + + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + + pc.printf("over \n \r"); + + }else if(y<= -0.90 ){ + + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + + pc.printf("left \n \r"); + + }else if(x<= -0.90 ){ + + + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + + + pc.printf("up \n \r"); + + } + + else{ + + rled = 1; + gled = 1; + bled =1; + pc.printf(" \n \r"); + } + // + // pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f \n \r", x, y, z); } -} +} \ No newline at end of file