Manuel Campos
/
Accelerometer_cvmanuel
initial version accelerometer
Fork of Accelerometer_example by
Diff: main.cpp
- Revision:
- 2:5fb1ae9832d3
- Parent:
- 0:a1caba5c4e48
--- a/main.cpp Wed Feb 07 16:56:55 2018 +0000 +++ b/main.cpp Thu Feb 15 18:11:08 2018 +0000 @@ -14,6 +14,9 @@ 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()); @@ -23,10 +26,63 @@ x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); - rled = 1.0f - abs(x); + + //flat + 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); + + }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); + }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("LEFT\n\r"); + pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); + }else + if(x< -threshold && abs(z)<min_threshold && abs(y)<min_threshold) + { + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + pc.printf("DOWN\n\r"); + pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); + }else + if(x> threshold && abs(z)<min_threshold && abs(y)<min_threshold) + { + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + pc.printf("UP\n\r"); + pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); + }else + if(z<-threshold && abs(y)<min_threshold && abs(x)<min_threshold) + { + rled = 1.0f - abs(x); + gled = 1.0f - abs(y); + bled = 1.0f - abs(z); + pc.printf("OVER\n\r"); + pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); + } + else{ + rled = 1; + gled= 1; + bled =1; + } Thread::wait(300); - pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); + } }