Lightsaber project
Dependencies: MMA8451Q TSI mbed
main.cpp@0:7e186c670eda, 2016-04-01 (annotated)
- Committer:
- alexwynn41
- Date:
- Fri Apr 01 18:27:00 2016 +0000
- Revision:
- 0:7e186c670eda
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexwynn41 | 0:7e186c670eda | 1 | #include "mbed.h" |
alexwynn41 | 0:7e186c670eda | 2 | #include "MMA8451Q.h" |
alexwynn41 | 0:7e186c670eda | 3 | #include "TSISensor.h" |
alexwynn41 | 0:7e186c670eda | 4 | |
alexwynn41 | 0:7e186c670eda | 5 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
alexwynn41 | 0:7e186c670eda | 6 | PinName const SDA = PTE25; |
alexwynn41 | 0:7e186c670eda | 7 | PinName const SCL = PTE24; |
alexwynn41 | 0:7e186c670eda | 8 | #elif defined (TARGET_KL05Z) |
alexwynn41 | 0:7e186c670eda | 9 | PinName const SDA = PTB4; |
alexwynn41 | 0:7e186c670eda | 10 | PinName const SCL = PTB3; |
alexwynn41 | 0:7e186c670eda | 11 | #elif defined (TARGET_K20D50M) |
alexwynn41 | 0:7e186c670eda | 12 | PinName const SDA = PTB1; |
alexwynn41 | 0:7e186c670eda | 13 | PinName const SCL = PTB0; |
alexwynn41 | 0:7e186c670eda | 14 | #else |
alexwynn41 | 0:7e186c670eda | 15 | #error TARGET NOT DEFINED |
alexwynn41 | 0:7e186c670eda | 16 | #endif |
alexwynn41 | 0:7e186c670eda | 17 | |
alexwynn41 | 0:7e186c670eda | 18 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
alexwynn41 | 0:7e186c670eda | 19 | Timer t; |
alexwynn41 | 0:7e186c670eda | 20 | TSISensor tsi; |
alexwynn41 | 0:7e186c670eda | 21 | |
alexwynn41 | 0:7e186c670eda | 22 | int main(void) |
alexwynn41 | 0:7e186c670eda | 23 | { |
alexwynn41 | 0:7e186c670eda | 24 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
alexwynn41 | 0:7e186c670eda | 25 | PwmOut rled(LED1); |
alexwynn41 | 0:7e186c670eda | 26 | PwmOut gled(LED2); |
alexwynn41 | 0:7e186c670eda | 27 | PwmOut bled(LED3); |
alexwynn41 | 0:7e186c670eda | 28 | float led; |
alexwynn41 | 0:7e186c670eda | 29 | |
alexwynn41 | 0:7e186c670eda | 30 | while (true) { |
alexwynn41 | 0:7e186c670eda | 31 | float x0, y0, z0, x1, y1, z1; |
alexwynn41 | 0:7e186c670eda | 32 | x0 = abs(acc.getAccX()); |
alexwynn41 | 0:7e186c670eda | 33 | y0 = abs(acc.getAccY()); |
alexwynn41 | 0:7e186c670eda | 34 | z0 = abs(acc.getAccZ()); |
alexwynn41 | 0:7e186c670eda | 35 | wait(0.1); |
alexwynn41 | 0:7e186c670eda | 36 | x1 = abs(acc.getAccX()); |
alexwynn41 | 0:7e186c670eda | 37 | y1 = abs(acc.getAccY()); |
alexwynn41 | 0:7e186c670eda | 38 | z1 = abs(acc.getAccZ()); |
alexwynn41 | 0:7e186c670eda | 39 | led = 1.0 - tsi.readPercentage(); |
alexwynn41 | 0:7e186c670eda | 40 | if(x1 - x0 > 0.6 || y1 - y0 > 0.6 || z1 - z0 > 0.6 ){ |
alexwynn41 | 0:7e186c670eda | 41 | bled = 1.0f; |
alexwynn41 | 0:7e186c670eda | 42 | rled = 1.0f; |
alexwynn41 | 0:7e186c670eda | 43 | wait(0.1); |
alexwynn41 | 0:7e186c670eda | 44 | } |
alexwynn41 | 0:7e186c670eda | 45 | |
alexwynn41 | 0:7e186c670eda | 46 | bled = led; |
alexwynn41 | 0:7e186c670eda | 47 | rled = led; |
alexwynn41 | 0:7e186c670eda | 48 | gled = led; |
alexwynn41 | 0:7e186c670eda | 49 | //rled = 1.0f - x0; |
alexwynn41 | 0:7e186c670eda | 50 | //gled = 1.0f - y0; |
alexwynn41 | 0:7e186c670eda | 51 | //bled = 1.0f - z0; |
alexwynn41 | 0:7e186c670eda | 52 | wait(0.1f); |
alexwynn41 | 0:7e186c670eda | 53 | //printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); |
alexwynn41 | 0:7e186c670eda | 54 | } |
alexwynn41 | 0:7e186c670eda | 55 | } |