![](/media/cache/profiles/IvyS.JPG.50x50_q85.jpg)
state led / position change
Fork of Accelerometer_example by
main.cpp@2:18e28291bc02, 2018-02-15 (annotated)
- Committer:
- Ivone
- Date:
- Thu Feb 15 18:28:42 2018 +0000
- Revision:
- 2:18e28291bc02
- Parent:
- 0:a1caba5c4e48
- Child:
- 3:ca2e99f6b193
Lab4
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(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 26 | rled = 1.0f - abs(x); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 27 | gled = 1.0f - abs(y); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 28 | bled = 1.0f - abs(z); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 29 | Thread::wait(300); |
Ivone | 2:18e28291bc02 | 30 | |
Ivone | 2:18e28291bc02 | 31 | |
Ivone | 2:18e28291bc02 | 32 | |
Ivone | 2:18e28291bc02 | 33 | pc.printf("X:flat ,Y:right ,Z:left\n", x,y,z); |
Ivone | 2:18e28291bc02 | 34 | |
Ivone | 2:18e28291bc02 | 35 | |
Ivone | 2:18e28291bc02 | 36 | // if (x> 0){ |
Ivone | 2:18e28291bc02 | 37 | |
Ivone | 2:18e28291bc02 | 38 | // pc.printf("X:flat ,Y:right ,Z:left\n", x,y,z); |
Ivone | 2:18e28291bc02 | 39 | |
Ivone | 2:18e28291bc02 | 40 | //} |
Ivone | 2:18e28291bc02 | 41 | //pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); |
Ivone | 2:18e28291bc02 | 42 | |
Ivone | 2:18e28291bc02 | 43 | |
Ivone | 2:18e28291bc02 | 44 | |
Ivone | 2:18e28291bc02 | 45 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 46 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 47 | } |