![](/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
Diff: mouseCtrl.cpp
- Revision:
- 6:753418e86c95
- Child:
- 7:689f891d62cd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mouseCtrl.cpp Mon Sep 21 08:21:05 2015 +0000 @@ -0,0 +1,84 @@ +/* +#include "mbed.h" +//#include "MMA8451Q.h" +//#include "USBMouse.h" +//#include "USBKeyboard.h" +#include "USBMouseKeyboard.h" +#include "LSM303DLHC.h" +#include "PinDetect.h" +#include "math.h" + +// define I2C Pins and address for KL25Z. Taken from default sample code. +PinName const SDA = D14; +PinName const SCL = D15; +PinDetect button(D2); +AnalogIn fsr(A5); +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +//serial connection to PC via USB +Serial pc(USBTX, USBRX); +LSM303DLHC lsm(SDA, SCL); +USBMouseKeyboard mouseKey; + + +float FsrVal; +float FsrValPrev; +int count; +// acc and mag values +float ax, ay, az,mx, my, mz; +float theta; + +int main(void){ + while(1){ + FsrVal = fsr.read(); + FsrValPrev=0.1; + lsm.read(&ax, &ay, &az, &mx, &my, &mz); //get acceleration + theta = atan (abs(ay)/abs(az)); + if((FsrVal > FsrValPrev && FsrVal > 0 && FsrValPrev > 0 && count <100) || (FsrVal > 0.8 && count <70)){ + // lower left + if(ay>0 && az>0){ + mouseKey.printf("forward + downward \n"); + mouseKey.printf("y %1.2f z %1.2f ",ay, az); + mouseKey.printf("theta1 %1.2f\n",theta); + mouseKey.printf("vertical %1.2f\n\n",-(floor)(sin(theta)*5)); + mouseKey.move(1,-(floor)(sin(theta)*5)); + } + // upper left + if(ay<0 && az>0){ + mouseKey.printf("forward + upward \n"); + + mouseKey.printf("y %1.2f z %1.2f ",ay, az); + mouseKey.printf("theta2 %1.2f\n",theta); + mouseKey.printf("vertica2 %1.2f\n\n",(floor)(sin(theta)*5)); + mouseKey.move(1,(floor)(sin(theta)*5)); + } + mouseKey.move(1,0); + FsrValPrev = fsr.read(); + count++; + } + else if((FsrVal < FsrValPrev && count >0) || (FsrVal < 0.2 && count >0)){ + // lower left + if(ay>0 && az>0){ + mouseKey.printf("backward + downward \n"); + + mouseKey.printf("y %1.2f z %1.2f ",ay, az); + mouseKey.printf("theta1 %1.2f\n",theta); + mouseKey.printf("vertical1 %1.2f\n\n",-(floor)(sin(theta)*5)); + mouseKey.move(-2,-(floor)(sin(theta)*5)); + } + // upper left + if(ay<0 && az>0){ + mouseKey.printf("backward + upward \n"); + mouseKey.printf("y %1.2f z %1.2f ",ay, az); + mouseKey.printf("theta2 %1.2f\n",theta); + mouseKey.printf("vertical2 %1.2f\n\n",(floor)(sin(theta)*5)); + mouseKey.move(-2,(floor)(sin(theta)*5)); + } + mouseKey.move(-2,0); + FsrValPrev = fsr.read(); + count=count-3; + } + wait(0.02); + } + } + */ \ No newline at end of file