Sarang Kharche
/
Accelerometer_example
Accelerometer
Fork of Accelerometer_example by
Revision 2:1c9bfcba9e49, committed 2018-02-16
- Comitter:
- Sarang
- Date:
- Fri Feb 16 23:38:53 2018 +0000
- Parent:
- 1:31f0f53b08bd
- Commit message:
- yes
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 31f0f53b08bd -r 1c9bfcba9e49 main.cpp --- a/main.cpp Wed Feb 07 16:56:55 2018 +0000 +++ b/main.cpp Fri Feb 16 23:38:53 2018 +0000 @@ -2,8 +2,8 @@ #include "rtos.h" #include "MMA8451Q.h" - PinName const SDA = PTE25; - PinName const SCL = PTE24; +PinName const SDA = PTE25; +PinName const SCL = PTE24; #define MMA8451_I2C_ADDRESS (0x1d<<1) @@ -14,19 +14,42 @@ PwmOut gled(LED2); PwmOut bled(LED3); Serial pc(USBTX, USBRX); // tx, rx - + float threshold = 0.80; + float threshold2 = 0.20; pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); while (true) { + + float x, y, z; x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); + + if (x<=threshold2 && y<=threshold && z>=threshold) { + pc.printf("Flat"); + } + + else if (x<=threshold2 && y>=threshold && z<=threshold2) { + pc.printf("Left"); + } + + + else if (x<=threshold2 && y<=threshold && z<=threshold2) { + pc.printf("Right"); + } + + 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); + pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); + + + + + } }