![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Submitted by Angela Hsueh, Maya Mardini, Yi Tong Slingshot controller using a force sensor and accelerometer for an Angry Birds clone game.
Dependencies: LSM303DLHC MMA8451Q PinDetect USBDevice mbed
Fork of hw3_controller by
mouseCtrl.cpp@6:753418e86c95, 2015-09-21 (annotated)
- Committer:
- ahsueh
- Date:
- Mon Sep 21 08:21:05 2015 +0000
- Revision:
- 6:753418e86c95
- Child:
- 7:689f891d62cd
Working Revision;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ahsueh | 6:753418e86c95 | 1 | /* |
ahsueh | 6:753418e86c95 | 2 | #include "mbed.h" |
ahsueh | 6:753418e86c95 | 3 | //#include "MMA8451Q.h" |
ahsueh | 6:753418e86c95 | 4 | //#include "USBMouse.h" |
ahsueh | 6:753418e86c95 | 5 | //#include "USBKeyboard.h" |
ahsueh | 6:753418e86c95 | 6 | #include "USBMouseKeyboard.h" |
ahsueh | 6:753418e86c95 | 7 | #include "LSM303DLHC.h" |
ahsueh | 6:753418e86c95 | 8 | #include "PinDetect.h" |
ahsueh | 6:753418e86c95 | 9 | #include "math.h" |
ahsueh | 6:753418e86c95 | 10 | |
ahsueh | 6:753418e86c95 | 11 | // define I2C Pins and address for KL25Z. Taken from default sample code. |
ahsueh | 6:753418e86c95 | 12 | PinName const SDA = D14; |
ahsueh | 6:753418e86c95 | 13 | PinName const SCL = D15; |
ahsueh | 6:753418e86c95 | 14 | PinDetect button(D2); |
ahsueh | 6:753418e86c95 | 15 | AnalogIn fsr(A5); |
ahsueh | 6:753418e86c95 | 16 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
ahsueh | 6:753418e86c95 | 17 | |
ahsueh | 6:753418e86c95 | 18 | //serial connection to PC via USB |
ahsueh | 6:753418e86c95 | 19 | Serial pc(USBTX, USBRX); |
ahsueh | 6:753418e86c95 | 20 | LSM303DLHC lsm(SDA, SCL); |
ahsueh | 6:753418e86c95 | 21 | USBMouseKeyboard mouseKey; |
ahsueh | 6:753418e86c95 | 22 | |
ahsueh | 6:753418e86c95 | 23 | |
ahsueh | 6:753418e86c95 | 24 | float FsrVal; |
ahsueh | 6:753418e86c95 | 25 | float FsrValPrev; |
ahsueh | 6:753418e86c95 | 26 | int count; |
ahsueh | 6:753418e86c95 | 27 | // acc and mag values |
ahsueh | 6:753418e86c95 | 28 | float ax, ay, az,mx, my, mz; |
ahsueh | 6:753418e86c95 | 29 | float theta; |
ahsueh | 6:753418e86c95 | 30 | |
ahsueh | 6:753418e86c95 | 31 | int main(void){ |
ahsueh | 6:753418e86c95 | 32 | while(1){ |
ahsueh | 6:753418e86c95 | 33 | FsrVal = fsr.read(); |
ahsueh | 6:753418e86c95 | 34 | FsrValPrev=0.1; |
ahsueh | 6:753418e86c95 | 35 | lsm.read(&ax, &ay, &az, &mx, &my, &mz); //get acceleration |
ahsueh | 6:753418e86c95 | 36 | theta = atan (abs(ay)/abs(az)); |
ahsueh | 6:753418e86c95 | 37 | if((FsrVal > FsrValPrev && FsrVal > 0 && FsrValPrev > 0 && count <100) || (FsrVal > 0.8 && count <70)){ |
ahsueh | 6:753418e86c95 | 38 | // lower left |
ahsueh | 6:753418e86c95 | 39 | if(ay>0 && az>0){ |
ahsueh | 6:753418e86c95 | 40 | mouseKey.printf("forward + downward \n"); |
ahsueh | 6:753418e86c95 | 41 | mouseKey.printf("y %1.2f z %1.2f ",ay, az); |
ahsueh | 6:753418e86c95 | 42 | mouseKey.printf("theta1 %1.2f\n",theta); |
ahsueh | 6:753418e86c95 | 43 | mouseKey.printf("vertical %1.2f\n\n",-(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 44 | mouseKey.move(1,-(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 45 | } |
ahsueh | 6:753418e86c95 | 46 | // upper left |
ahsueh | 6:753418e86c95 | 47 | if(ay<0 && az>0){ |
ahsueh | 6:753418e86c95 | 48 | mouseKey.printf("forward + upward \n"); |
ahsueh | 6:753418e86c95 | 49 | |
ahsueh | 6:753418e86c95 | 50 | mouseKey.printf("y %1.2f z %1.2f ",ay, az); |
ahsueh | 6:753418e86c95 | 51 | mouseKey.printf("theta2 %1.2f\n",theta); |
ahsueh | 6:753418e86c95 | 52 | mouseKey.printf("vertica2 %1.2f\n\n",(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 53 | mouseKey.move(1,(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 54 | } |
ahsueh | 6:753418e86c95 | 55 | mouseKey.move(1,0); |
ahsueh | 6:753418e86c95 | 56 | FsrValPrev = fsr.read(); |
ahsueh | 6:753418e86c95 | 57 | count++; |
ahsueh | 6:753418e86c95 | 58 | } |
ahsueh | 6:753418e86c95 | 59 | else if((FsrVal < FsrValPrev && count >0) || (FsrVal < 0.2 && count >0)){ |
ahsueh | 6:753418e86c95 | 60 | // lower left |
ahsueh | 6:753418e86c95 | 61 | if(ay>0 && az>0){ |
ahsueh | 6:753418e86c95 | 62 | mouseKey.printf("backward + downward \n"); |
ahsueh | 6:753418e86c95 | 63 | |
ahsueh | 6:753418e86c95 | 64 | mouseKey.printf("y %1.2f z %1.2f ",ay, az); |
ahsueh | 6:753418e86c95 | 65 | mouseKey.printf("theta1 %1.2f\n",theta); |
ahsueh | 6:753418e86c95 | 66 | mouseKey.printf("vertical1 %1.2f\n\n",-(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 67 | mouseKey.move(-2,-(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 68 | } |
ahsueh | 6:753418e86c95 | 69 | // upper left |
ahsueh | 6:753418e86c95 | 70 | if(ay<0 && az>0){ |
ahsueh | 6:753418e86c95 | 71 | mouseKey.printf("backward + upward \n"); |
ahsueh | 6:753418e86c95 | 72 | mouseKey.printf("y %1.2f z %1.2f ",ay, az); |
ahsueh | 6:753418e86c95 | 73 | mouseKey.printf("theta2 %1.2f\n",theta); |
ahsueh | 6:753418e86c95 | 74 | mouseKey.printf("vertical2 %1.2f\n\n",(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 75 | mouseKey.move(-2,(floor)(sin(theta)*5)); |
ahsueh | 6:753418e86c95 | 76 | } |
ahsueh | 6:753418e86c95 | 77 | mouseKey.move(-2,0); |
ahsueh | 6:753418e86c95 | 78 | FsrValPrev = fsr.read(); |
ahsueh | 6:753418e86c95 | 79 | count=count-3; |
ahsueh | 6:753418e86c95 | 80 | } |
ahsueh | 6:753418e86c95 | 81 | wait(0.02); |
ahsueh | 6:753418e86c95 | 82 | } |
ahsueh | 6:753418e86c95 | 83 | } |
ahsueh | 6:753418e86c95 | 84 | */ |