Code for accelerometer Lab 4 Code 1
Fork of Accelerometer_example by
main.cpp@2:a1282101390e, 2018-02-16 (annotated)
- Committer:
- Tobden
- Date:
- Fri Feb 16 23:37:15 2018 +0000
- Revision:
- 2:a1282101390e
- Parent:
- 0:a1caba5c4e48
Version 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:a1caba5c4e48 | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 2 | #include "rtos.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 3 | #include "MMA8451Q.h" |
WilliamMarshQMUL | 0:a1caba5c4e48 | 4 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 5 | PinName const SDA = PTE25; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 6 | PinName const SCL = PTE24; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 7 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 8 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 9 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 10 | int main(void) |
WilliamMarshQMUL | 0:a1caba5c4e48 | 11 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 12 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 13 | PwmOut rled(LED1); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 14 | PwmOut gled(LED2); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 15 | PwmOut bled(LED3); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 16 | Serial pc(USBTX, USBRX); // tx, rx |
WilliamMarshQMUL | 0:a1caba5c4e48 | 17 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 18 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 19 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 20 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 21 | while (true) { |
Tobden | 2:a1282101390e | 22 | float x, y, z, trled, tgled, tbled; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 23 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 24 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 25 | z = acc.getAccZ(); |
Tobden | 2:a1282101390e | 26 | trled = 1.0f - abs(x); |
Tobden | 2:a1282101390e | 27 | tgled = 1.0f - abs(y); |
Tobden | 2:a1282101390e | 28 | tbled = 1.0f - abs(z); |
Tobden | 2:a1282101390e | 29 | |
Tobden | 2:a1282101390e | 30 | if (trled>0.8 && tgled > 0.8) |
Tobden | 2:a1282101390e | 31 | { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 32 | rled = 1.0f - abs(x); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 33 | gled = 1.0f - abs(y); |
Tobden | 2:a1282101390e | 34 | bled = 1.0f - abs(z); |
Tobden | 2:a1282101390e | 35 | if (z>0){ |
Tobden | 2:a1282101390e | 36 | pc.printf("FLAT\n"); |
Tobden | 2:a1282101390e | 37 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
Tobden | 2:a1282101390e | 38 | } |
Tobden | 2:a1282101390e | 39 | else{ |
Tobden | 2:a1282101390e | 40 | pc.printf("OVER\n"); |
Tobden | 2:a1282101390e | 41 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);} |
Tobden | 2:a1282101390e | 42 | } |
Tobden | 2:a1282101390e | 43 | else if (tgled > 0.8 && tbled > 0.8) |
Tobden | 2:a1282101390e | 44 | { |
Tobden | 2:a1282101390e | 45 | rled = 1.0f - abs(x); |
Tobden | 2:a1282101390e | 46 | gled = 1.0f - abs(y); |
Tobden | 2:a1282101390e | 47 | bled = 1.0f - abs(z); |
Tobden | 2:a1282101390e | 48 | if (x>0){ |
Tobden | 2:a1282101390e | 49 | pc.printf("DOWN\n"); |
Tobden | 2:a1282101390e | 50 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
Tobden | 2:a1282101390e | 51 | } |
Tobden | 2:a1282101390e | 52 | else{ |
Tobden | 2:a1282101390e | 53 | pc.printf("UP\n"); |
Tobden | 2:a1282101390e | 54 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);} |
Tobden | 2:a1282101390e | 55 | } |
Tobden | 2:a1282101390e | 56 | else if (trled>0.8 && tbled > 0.8) |
Tobden | 2:a1282101390e | 57 | { |
Tobden | 2:a1282101390e | 58 | rled = 1.0f - abs(x); |
Tobden | 2:a1282101390e | 59 | gled = 1.0f - abs(y); |
Tobden | 2:a1282101390e | 60 | bled = 1.0f - abs(z); |
Tobden | 2:a1282101390e | 61 | if (y>0){ |
Tobden | 2:a1282101390e | 62 | pc.printf("LEFT\n"); |
Tobden | 2:a1282101390e | 63 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
Tobden | 2:a1282101390e | 64 | } |
Tobden | 2:a1282101390e | 65 | else{ |
Tobden | 2:a1282101390e | 66 | pc.printf("RIGHT\n"); |
Tobden | 2:a1282101390e | 67 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);} |
Tobden | 2:a1282101390e | 68 | } |
Tobden | 2:a1282101390e | 69 | else { |
Tobden | 2:a1282101390e | 70 | rled = 1; |
Tobden | 2:a1282101390e | 71 | gled = 1; |
Tobden | 2:a1282101390e | 72 | bled = 1; |
Tobden | 2:a1282101390e | 73 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 74 | Thread::wait(300); |
Tobden | 2:a1282101390e | 75 | // if (gled == 0 || bled == 0 || rled == 0){ |
Tobden | 2:a1282101390e | 76 | // pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z);} |
WilliamMarshQMUL | 0:a1caba5c4e48 | 77 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 78 | } |