Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Accelerometer_example by
Revision 2:5fb1ae9832d3, committed 2018-02-15
- Comitter:
- cvmanuel
- Date:
- Thu Feb 15 18:11:08 2018 +0000
- Parent:
- 1:31f0f53b08bd
- Commit message:
- initial version accelerometer
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 31f0f53b08bd -r 5fb1ae9832d3 main.cpp --- 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); + } }