Manuel Campos
/
Accelerometer_cvmanuel
initial version accelerometer
Fork of Accelerometer_example by
main.cpp@2:5fb1ae9832d3, 2018-02-15 (annotated)
- Committer:
- cvmanuel
- Date:
- Thu Feb 15 18:11:08 2018 +0000
- Revision:
- 2:5fb1ae9832d3
- Parent:
- 0:a1caba5c4e48
initial version accelerometer
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 |
cvmanuel | 2:5fb1ae9832d3 | 17 | float threshold = .50; |
cvmanuel | 2:5fb1ae9832d3 | 18 | float min_threshold = 0.25; |
cvmanuel | 2:5fb1ae9832d3 | 19 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 20 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 21 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 22 | pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 23 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 24 | while (true) { |
WilliamMarshQMUL | 0:a1caba5c4e48 | 25 | float x, y, z; |
WilliamMarshQMUL | 0:a1caba5c4e48 | 26 | x = acc.getAccX(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 27 | y = acc.getAccY(); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 28 | z = acc.getAccZ(); |
cvmanuel | 2:5fb1ae9832d3 | 29 | |
cvmanuel | 2:5fb1ae9832d3 | 30 | //flat |
cvmanuel | 2:5fb1ae9832d3 | 31 | if(z>threshold && abs(x)< min_threshold && abs(y)< min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 32 | { |
cvmanuel | 2:5fb1ae9832d3 | 33 | rled = 1.0f - abs(x); |
cvmanuel | 2:5fb1ae9832d3 | 34 | gled = 1.0f - abs(y); |
cvmanuel | 2:5fb1ae9832d3 | 35 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 36 | pc.printf("FLAT\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 37 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 38 | |
cvmanuel | 2:5fb1ae9832d3 | 39 | }else |
cvmanuel | 2:5fb1ae9832d3 | 40 | if(y<-threshold && abs(z)<min_threshold && abs(x)<min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 41 | { |
cvmanuel | 2:5fb1ae9832d3 | 42 | rled = 1.0f - abs(x); |
cvmanuel | 2:5fb1ae9832d3 | 43 | gled = 1.0f - abs(y); |
cvmanuel | 2:5fb1ae9832d3 | 44 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 45 | pc.printf("RIGHT\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 46 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 47 | }else |
cvmanuel | 2:5fb1ae9832d3 | 48 | if(y>threshold && abs(z)<min_threshold && abs(x)<min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 49 | { |
cvmanuel | 2:5fb1ae9832d3 | 50 | rled = 1.0f - abs(x); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 51 | gled = 1.0f - abs(y); |
WilliamMarshQMUL | 0:a1caba5c4e48 | 52 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 53 | pc.printf("LEFT\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 54 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 55 | }else |
cvmanuel | 2:5fb1ae9832d3 | 56 | if(x< -threshold && abs(z)<min_threshold && abs(y)<min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 57 | { |
cvmanuel | 2:5fb1ae9832d3 | 58 | rled = 1.0f - abs(x); |
cvmanuel | 2:5fb1ae9832d3 | 59 | gled = 1.0f - abs(y); |
cvmanuel | 2:5fb1ae9832d3 | 60 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 61 | pc.printf("DOWN\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 62 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 63 | }else |
cvmanuel | 2:5fb1ae9832d3 | 64 | if(x> threshold && abs(z)<min_threshold && abs(y)<min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 65 | { |
cvmanuel | 2:5fb1ae9832d3 | 66 | rled = 1.0f - abs(x); |
cvmanuel | 2:5fb1ae9832d3 | 67 | gled = 1.0f - abs(y); |
cvmanuel | 2:5fb1ae9832d3 | 68 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 69 | pc.printf("UP\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 70 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 71 | }else |
cvmanuel | 2:5fb1ae9832d3 | 72 | if(z<-threshold && abs(y)<min_threshold && abs(x)<min_threshold) |
cvmanuel | 2:5fb1ae9832d3 | 73 | { |
cvmanuel | 2:5fb1ae9832d3 | 74 | rled = 1.0f - abs(x); |
cvmanuel | 2:5fb1ae9832d3 | 75 | gled = 1.0f - abs(y); |
cvmanuel | 2:5fb1ae9832d3 | 76 | bled = 1.0f - abs(z); |
cvmanuel | 2:5fb1ae9832d3 | 77 | pc.printf("OVER\n\r"); |
cvmanuel | 2:5fb1ae9832d3 | 78 | pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n\r", x, y, z); |
cvmanuel | 2:5fb1ae9832d3 | 79 | } |
cvmanuel | 2:5fb1ae9832d3 | 80 | else{ |
cvmanuel | 2:5fb1ae9832d3 | 81 | rled = 1; |
cvmanuel | 2:5fb1ae9832d3 | 82 | gled= 1; |
cvmanuel | 2:5fb1ae9832d3 | 83 | bled =1; |
cvmanuel | 2:5fb1ae9832d3 | 84 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 85 | Thread::wait(300); |
cvmanuel | 2:5fb1ae9832d3 | 86 | |
WilliamMarshQMUL | 0:a1caba5c4e48 | 87 | } |
WilliamMarshQMUL | 0:a1caba5c4e48 | 88 | } |