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 HW3 Controller Team!

Revision:
7:689f891d62cd
Parent:
6:753418e86c95
--- a/main.cpp	Mon Sep 21 08:21:05 2015 +0000
+++ b/main.cpp	Mon Sep 21 12:14:01 2015 +0000
@@ -1,7 +1,4 @@
 #include "mbed.h"
-//#include "MMA8451Q.h"
-//#include "USBMouse.h"
-//#include "USBKeyboard.h"
 #include "USBMouseKeyboard.h"
 #include "LSM303DLHC.h"
 #include "PinDetect.h"
@@ -10,7 +7,12 @@
 // define I2C Pins and address for KL25Z. Taken from default sample code.
 PinName const SDA = D14;
 PinName const SCL = D15;
-PinDetect button(D2);
+DigitalIn p1(D3);
+DigitalIn p2(D4);
+DigitalIn p3(D5);
+//PinDetect p1(D3);
+//PinDetect p2(D4);
+//PinDetect p3(D5);
 AnalogIn fsr(A5);
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
 
@@ -26,7 +28,8 @@
 // acc and mag values
 float ax, ay, az, mx, my, mz;
 float theta;
-
+int mousePress;
+float flt_force_prev;
 int oldx, oldy;
 
 int mouseTravelX = 0;
@@ -51,26 +54,63 @@
     return mean / (float)len;
 }
 
+
+
+
 int main(void){
     int x = 0;
     int y = 0;
+    int mouseClickDown = 0;
+    bool aim = false;
     while(1){
         fsrVal = fsr.read();
         lsm.read(&ax, &ay, &az, &mx, &my, &mz);
         
         float cur_angle = atan(ax / az);
-        float cur_force = fsrVal*500;
+        float cur_force = fsrVal*1000;
         
         float flt_force = filter(forces, 8, cur_force);
         float flt_angle = filter(angles, 8, cur_angle);
         
-
+        //mouseKey.printf("ax: %1.2f az: %1.2f fsrVal: %1.2f \n", ax, az, fsrVal);        
         
-        //mouseKey.printf("ax: %1.2f az: %1.2f fsrVal: %1.2f \n", ax, az, fsrVal);
+        //mouseKey.printf("fsrVal: %1.2f fsrValPrev: %1.2f flt_force: %1.2f\n",
+        //                fsrVal, fsrValPrev, flt_force);
+        if(p1 == 0){
+           mouseKey.keyCode('a'); 
+           
+        }
+        if (p2 == 0) {
+            mouseKey.click(MOUSE_LEFT);
+            wait(0.05);
+        }
         
-        //if((FsrVal > FsrValPrev && FsrVal > 0 && FsrValPrev > 0 && count <100) || (FsrVal > 0.8 && count <70)){
-        
-        
+
+        //if ((flt_force > 0.2)) {
+            
+        //mouseKey.printf("fsrVal %1.2f fsrValPrev %1.2f flt_force %1.2f \n", fsrVal, fsrValPrev,flt_force);
+        if(p3 == 0){
+            if (mouseClickDown == 0) {
+            mouseKey.press(MOUSE_LEFT);
+            mouseClickDown = 1;
+            }
+            //wait(0.02);
+        }
+            else {
+                if (mouseClickDown == 1) {
+                mouseKey.release(MOUSE_LEFT);
+                mouseClickDown = 0;
+                } 
+        }
+        if ((((flt_force_prev - flt_force) > 7) || flt_force == 0) && aim) {
+            //mouseKey.keyCode('c');
+            aim = false;
+        } else if ((flt_force >20)&&(!aim)) {
+            //mouseKey.keyCode('b');
+            aim = true;
+            //mouseKey.press(MOUSE_LEFT);
+        }
+            
             if (ax > 0 && az > 0) {
                 x = (int) (0.0 - cos(flt_angle) * flt_force);
                 y = (int) (sin(flt_angle) * flt_force);
@@ -86,9 +126,9 @@
             mouseMem((x-oldx),(y-oldy));
             oldx = x;
             oldy = y;
-        
-        
-        wait(0.02);
+            fsrValPrev = fsrVal;
+            flt_force_prev = flt_force;
+        wait(0.01);
     }
 }