Anastasios Barlas
/
Accelerometer_example
Lab 4 Version 1
Fork of Accelerometer_example by
main.cpp@2:6edf3f4a21cb, 2018-02-16 (annotated)
- Committer:
- AnastasiosBarlas
- Date:
- Fri Feb 16 22:00:28 2018 +0000
- Revision:
- 2:6edf3f4a21cb
- 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) { |
AnastasiosBarlas | 2:6edf3f4a21cb | 22 | float x, y, z,t; |
AnastasiosBarlas | 2:6edf3f4a21cb | 23 | t = 0.5; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 24 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 25 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 26 | z = acc.getAccZ(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 27 | rled = 1.0f - abs(x); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 28 | gled = 1.0f - abs(y); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 29 | bled = 1.0f - abs(z); |
AnastasiosBarlas | 2:6edf3f4a21cb | 30 | if (y>=t && abs(x) < t && abs(z) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 31 | pc.printf("down"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 32 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 33 | else if (y<=t && abs(x) < t && abs(z) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 34 | pc.printf("up"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 35 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 36 | else if (x<=t && abs(y) < t && abs(z) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 37 | pc.printf("left"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 38 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 39 | else if (x>=t && abs(y) < t && abs(z) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 40 | pc.printf("right"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 41 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 42 | else if (z<=t && abs(y) < t && abs(x) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 43 | pc.printf("over"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 44 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 45 | else if (z>=t && abs(y) < t && abs(x) < t ){ |
AnastasiosBarlas | 2:6edf3f4a21cb | 46 | pc.printf("flat"); |
AnastasiosBarlas | 2:6edf3f4a21cb | 47 | } |
AnastasiosBarlas | 2:6edf3f4a21cb | 48 | else{ |
AnastasiosBarlas | 2:6edf3f4a21cb | 49 | rled = 1; |
AnastasiosBarlas | 2:6edf3f4a21cb | 50 | gled = 1; |
AnastasiosBarlas | 2:6edf3f4a21cb | 51 | bled = 1; |
AnastasiosBarlas | 2:6edf3f4a21cb | 52 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 53 | Thread::wait(300); |
AnastasiosBarlas | 2:6edf3f4a21cb | 54 | //pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n \r", x, y, z); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 55 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 56 | } |