![](/media/cache/profiles/IvyS.JPG.50x50_q85.jpg)
state led / position change
Fork of Accelerometer_example by
main.cpp@3:ca2e99f6b193, 2018-02-16 (annotated)
- Committer:
- Ivone
- Date:
- Fri Feb 16 11:53:18 2018 +0000
- Revision:
- 3:ca2e99f6b193
- Parent:
- 2:18e28291bc02
- Child:
- 4:42e2706f9847
Lab4 accelerometer leds flat, right, left
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) { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 22 | float x, y, z; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 23 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 24 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 25 | z = acc.getAccZ(); |
Ivone | 3:ca2e99f6b193 | 26 | |
Ivone | 3:ca2e99f6b193 | 27 | Thread::wait (300); |
Ivone | 2:18e28291bc02 | 28 | |
Ivone | 3:ca2e99f6b193 | 29 | //Flat my led |
Ivone | 3:ca2e99f6b193 | 30 | if(z>= 0.9){ |
Ivone | 3:ca2e99f6b193 | 31 | |
Ivone | 3:ca2e99f6b193 | 32 | rled = 0; |
Ivone | 3:ca2e99f6b193 | 33 | gled = 0; |
Ivone | 3:ca2e99f6b193 | 34 | bled = 0; |
Ivone | 3:ca2e99f6b193 | 35 | |
Ivone | 3:ca2e99f6b193 | 36 | pc.printf("flat\n\r"); |
Ivone | 3:ca2e99f6b193 | 37 | |
Ivone | 2:18e28291bc02 | 38 | |
Ivone | 3:ca2e99f6b193 | 39 | }else if (y>=0.9){ |
Ivone | 3:ca2e99f6b193 | 40 | |
Ivone | 3:ca2e99f6b193 | 41 | //right my led |
Ivone | 3:ca2e99f6b193 | 42 | rled = 0; |
Ivone | 3:ca2e99f6b193 | 43 | gled = 0; |
Ivone | 3:ca2e99f6b193 | 44 | bled = 0; |
Ivone | 3:ca2e99f6b193 | 45 | pc.printf("right\n\r"); |
Ivone | 3:ca2e99f6b193 | 46 | |
Ivone | 3:ca2e99f6b193 | 47 | }else if (x>=0.9){ |
Ivone | 3:ca2e99f6b193 | 48 | //over |
Ivone | 3:ca2e99f6b193 | 49 | |
Ivone | 3:ca2e99f6b193 | 50 | rled = 0; |
Ivone | 3:ca2e99f6b193 | 51 | gled = 0; |
Ivone | 3:ca2e99f6b193 | 52 | bled = 0; |
Ivone | 3:ca2e99f6b193 | 53 | pc.printf("over \n \r"); |
Ivone | 3:ca2e99f6b193 | 54 | |
Ivone | 3:ca2e99f6b193 | 55 | }else if (x<= -0.90){ |
Ivone | 3:ca2e99f6b193 | 56 | |
Ivone | 3:ca2e99f6b193 | 57 | rled = 0; |
Ivone | 3:ca2e99f6b193 | 58 | gled = 0; |
Ivone | 3:ca2e99f6b193 | 59 | bled = 0; |
Ivone | 3:ca2e99f6b193 | 60 | pc.printf("up\n\r"); |
Ivone | 3:ca2e99f6b193 | 61 | |
Ivone | 3:ca2e99f6b193 | 62 | } |
Ivone | 3:ca2e99f6b193 | 63 | |
Ivone | 3:ca2e99f6b193 | 64 | else { |
Ivone | 3:ca2e99f6b193 | 65 | |
Ivone | 3:ca2e99f6b193 | 66 | rled = 1; |
Ivone | 3:ca2e99f6b193 | 67 | gled = 1; |
Ivone | 3:ca2e99f6b193 | 68 | bled = 1; |
Ivone | 3:ca2e99f6b193 | 69 | pc.printf("\n\r"); |
Ivone | 3:ca2e99f6b193 | 70 | |
Ivone | 3:ca2e99f6b193 | 71 | } |
Ivone | 2:18e28291bc02 | 72 | |
Ivone | 3:ca2e99f6b193 | 73 | //pc.printf("X:%1.2f ,Y:%1.2f ,Z:%1.2f\n\r", x,y,z); |
Ivone | 3:ca2e99f6b193 | 74 | |
Ivone | 2:18e28291bc02 | 75 | |
Ivone | 2:18e28291bc02 | 76 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 77 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 78 | } |