Andrew Mkpanam
/
Accelerometer_pt1
Final
Revision 6:a0f642150296, committed 2020-03-05
- Comitter:
- ex19397
- Date:
- Thu Mar 05 23:02:34 2020 +0000
- Parent:
- 5:392ac6e02d80
- Commit message:
- Final
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 392ac6e02d80 -r a0f642150296 main.cpp --- a/main.cpp Thu Feb 27 10:07:17 2020 +0000 +++ b/main.cpp Thu Mar 05 23:02:34 2020 +0000 @@ -13,8 +13,10 @@ PwmOut gled(LED2); PwmOut bled(LED3); Serial pc(USBTX, USBRX); // tx, rx - - + enum position { flat, over, intermediate, down, up, left, right } pos; + pos = intermediate; + //orientationstate cases = flat; + pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); while (true) { @@ -22,10 +24,129 @@ x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); - rled = 1.0f - abs(x); - gled = 1.0f - abs(y); - bled = 1.0f - abs(z); - ThisThread::sleep_for(300); // wait(0.3); - pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); + + switch (pos) { + + case flat: + if (z < 0.8) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + + break; + + case over: + if (z > -0.8) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + break; + + case intermediate: + if (z < -0.9 ) { + rled = 1; + gled = 0; + bled = 1; + pos = over; + pc.printf("Board Orientation : Over\n\r"); + } + + if ((z > 0.9 ) ){ + rled = 1; + gled = 0; + bled = 1; + pc.printf("Board Orientation : Flat\n\r"); + pos = flat; + } + /*if ((z < 0.8) && (z > -0.8)&& (x < 0.8) && (x > -0.8) && (z < 0.8) && (z > -0.8)) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + }*/ + if ((x >0.9) ) { + rled = 1; + gled = 1; + bled = 0; + pos = down; + pc.printf("Board Orientation : Down\n\r"); + } + if (x < -0.9) { + rled = 1; + gled = 1; + bled = 0; + pos = up; + pc.printf("Board Orientation : Up\n\r"); + + } + if ((y > 0.9 ) ) { + rled = 0; + gled = 1; + bled = 1; + pos = left; + pc.printf("Board Orientation : Left\n\r"); + + } + if (y < -0.9) { + rled = 0; + gled = 1; + bled = 1; + pos = right; + pc.printf("Board Orientation : Right\n\r"); + + } + + break; + + case down: + if (x < 0.8) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + break; + + case up: + if (x > - 0.8) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + + break; + + case left: + + if (y < 0.8 ){ + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + + break; + + case right: + + if (y > - 0.8) { + rled = 1; + gled = 1; + bled = 1; + pos = intermediate; + } + + break; + //rled = 1.0f - abs(x); + //gled = 1.0f - abs(y); + //bled = 1.0f - abs(z); + ThisThread::sleep_for(3000); // wait(0.3); + + } } -} + } \ No newline at end of file