![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Lab4Code2(modified version)
Fork of Accelerometer_example by
main.cpp
- Committer:
- Tobden
- Date:
- 2018-02-23
- Revision:
- 3:2e32120a45f3
- Parent:
- 2:e553dd3fca46
File content as of revision 3:2e32120a45f3:
#include "mbed.h" #include "rtos.h" #include "MMA8451Q.h" PinName const SDA = PTE25; PinName const SCL = PTE24; Timer t; Thread change; enum orientation { flat, up, right, final, errors }; #define MMA8451_I2C_ADDRESS (0x1d<<1) /*void position() { }*/ 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()); orientation type = flat; while (true) { Thread::wait(500); float x, y, z, trled, tgled, tbled; x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); trled = 1.0f - abs(x); tgled = 1.0f - abs(y); tbled = 1.0f - abs(z); int rd = t.read(); switch (type) { case flat : t.start(); if (trled>0.8 && tgled > 0.8) { rled = 1; gled = 1; bled = 0; if (z>0){ if (rd<10){ pc.printf("Current position: FLAT\n\r"); // pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); pc.printf("Time remaining: %d seconds\n\r", 10-rd);} else pc.printf("Change to the RIGHT now\n\r"); } } if (tgled > 0.8 && tbled > 0.8) { rled = 0; gled = 1; bled = 1; pc.printf("SEQUENCE ERROR\n\r"); t.reset(); t.stop(); type = errors; break;} if (rd>=10 && (trled > 0.8 && tbled > 0.8)) { t.reset(); t.stop(); type = right; } if (rd<10 && (trled > 0.8 && tbled > 0.8)) { rled = 0; gled = 1; bled = 1; t.reset(); t.stop(); type = errors; } break; case right: if (trled > 0.8 && tbled > 0.8) {t.start(); rled = 1; gled = 1; bled = 0; if (y<0){ pc.printf("Current position: RIGHT\n"); if (rd<2){ pc.printf("Please wait %d seconds\n\r ", 2-rd); } else if (rd>=2 && rd<=6) { pc.printf("You have: %d seconds to change to UP: \n\r", 6-rd);} else pc.printf("Too late\n\r"); } } if (trled<=0.8 || tbled <= 0.8) {t.stop();} if (trled > 0.8 && tgled > 0.8 && rd > 0) { rled = 0; gled = 1; bled = 1; pc.printf("SEQUENCE ERROR\n\r"); t.reset(); t.stop(); type = errors; break;} if (rd>=2 && rd<=6 && (tgled > 0.8 && tbled > 0.8)) { t.reset(); type = up; } if ((rd<2 || rd>6) && (tgled > 0.8 && tbled > 0.8)) { rled = 0; gled = 1; bled = 1; t.reset(); t.stop(); type = errors; } break; case up: t.start(); if (tgled > 0.8 && tbled > 0.8) { rled = 1; gled = 1; bled = 0; if (x<0){ pc.printf("Current position: UP\n"); if (rd<4){ pc.printf("Please wait %d seconds\n\r ", 4-rd); } else if (rd>=4 && rd<=8) { pc.printf("You have: %d seconds to change to FLAT: \n\r", 8-rd);} else pc.printf("Too late\n\r"); } } if (tgled <= 0.8 || tbled <= 0.8) {t.stop();} if (trled > 0.8 && tbled > 0.8) { rled = 0; gled = 1; bled = 1; pc.printf("SEQUENCE ERROR\n\r"); t.reset(); t.stop(); type = errors; break;} if (rd>=4 && rd<=8 && (tgled > 0.8 && trled > 0.8)) { t.reset(); type = final; } if ((rd<4 || rd>8 )&& (trled > 0.8 && tgled > 0.8)) { rled = 0; gled = 1; bled = 1; t.reset(); t.stop(); type = errors; } break; case final: if (trled>0.8 && tgled > 0.8) { rled = 1; gled = 0; bled = 1; if (z>0){ pc.printf("SEQUENCE COMPLETE: SYSTEM TRIGGERED \n\r"); } } break; case errors: if (trled< 0.8 || tgled < 0.8 || z<0) {pc.printf("Error has occured. Please orientate back to the flat position\n\r");} else if (trled>0.8 && tgled > 0.8 && z>0 && rd<10) {t.start(); pc.printf("FLAT\n"); pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); pc.printf("system resetting in %d seconds\n\r", 10-rd); } else if (rd>=10) { rled = 1; gled = 1; bled = 0; pc.printf("Error successfully cleared\n\r"); t.reset(); t.stop(); type = right; break;} } } }