Boundary mods

Dependencies:   USBDevice mbed

Fork of FinalProjectKeyboard by PecMan

Revision:
1:8546d208bd4f
Parent:
0:f23fd07d1d96
Child:
2:5fec7dd286d2
--- a/main.cpp	Wed Apr 27 01:38:09 2016 +0000
+++ b/main.cpp	Wed Apr 27 16:41:29 2016 +0000
@@ -3,68 +3,146 @@
 
 Serial pc(USBTX, USBRX);
 
-//AnalogIn inputx(PTB0); // AnalogIn inputx(PTB0);
-//AnalogIn inputy(PTB1); //AnalogIn inputy(PTB1);
+AnalogIn inputx(PTB0); // AnalogIn inputx(PTB0);
+AnalogIn inputy(PTB1); //AnalogIn inputy(PTB1);
 AnalogIn inputz(PTB2); //AnalogIn inputz(PTB2);
 
-//DigitalIn calibrate(PTD7);
-DigitalOut LED(LED1);
+Timeout debounceUp;
+Timeout debounceDown;
+Timeout middle1;
+Timeout middle2;
+
+
+PwmOut led1(LED1);
+PwmOut led2(LED2);
+PwmOut led3(LED3);
+
+int xR; 
+int yR; 
+int zR;
+
+int sumX;
+int sumY;
+int sumZ;
+ 
+
+int x1=0; // variable for initialization
+int y1=0; // variable for initialization
+int z1=0; // variable for initialization
+
+
+int x=0; // variable for x axes
+int y=0; // variable for y axes
+int z=0; // variable for z axes
+
+
+bool midFl1 = 1;
+bool midFl2 = 1;
+bool dwn = 0;
+bool up = 0;
+
+void DBUp(){
+    up = 0;
+    }
+
+void DBDown(){
+    dwn = 0;
+    }
+    
+void mid1(){
+    midFl1 = 1;
+    }
+
+void mid2(){
+    midFl2 = 1;
+    }
+
 
 int main() {
-LED=0;     
+//led1=0.9;     
 pc.baud(9600);   // baud rate: 9600 bps interaction with computer
 
-wait(5);
+wait(1);
 
-int z1=0; // variable for initialization
-z1 = inputz.read_u16();
+for(int i =0; i < 10;i++){
+    xR = inputx.read_u16();
+    yR = inputy.read_u16();
+    zR = inputz.read_u16();
+    sumX += xR;
+    sumY += yR;
+    sumZ += zR;
+}
 
-int z=0; // variable for z axes
+x1 = sumX/10;
+y1 = sumY/10;
+z1 = sumZ/10;
 
 //************Eric Algorithm******************
 int status[]={0,0}; //0 means break,1 is up, 2 is down. status(0) is last, status(1) is current
 int L=1; // 0 = not outputting, =1 is ouputting
 //********************************************
+
+
 USBKeyboard keyboard;
     while (true) {
-        
-         z = inputz.read_u16()-z1; //Calibration
-       
-        //if(calibrate){
-          //  pc.printf("Hey There\n\r");
-          // }
-        
-         //Establish Down
-         if (z<-1100) { 
-         status[0] = status[1];
-         status[1] = 1;
+        L=1;
+        z = inputz.read_u16()-z1; //Calibration
+        x = inputx.read_u16()-x1; //Calibration
+        y = inputy.read_u16()-y1; //Calibration
+//        pc.printf("X: %d Y: %d Z: %d \n\r", x,y,z);
+         //Establish Up
+        if (x > -800 && y < 1500 && up == 0) { 
+//            status[0] = status[1];
+//            status[1] = 1;
+//            pc.printf("Down");
+            keyboard.keyCode(UP_ARROW);
+            led1 = 1;
+            led2 = 0.9;
+            led3 = 1;
+            
+            up = 1;
+            
+            debounceUp.attach(&DBDown, 0.100);
+//            up = 0;
+
+        }
          
-         if (L==1) {
-         //pc.printf("Up\n\r"); //Output Up
-         keyboard.keyCode(UP_ARROW);
-         L=0;}}
-         
-         //Establish Up
-         if(z>2500) {
-         status[0] = status[1];
-         status[1] = 2;
-         
-         if (L==1) {
-         //pc.printf("Down\n\r"); //Output Down
-         keyboard.keyCode(DOWN_ARROW);
-         L=0;}}
+         //Establish Down
+        if(x < -3500 && y > 5000 && dwn == 0) {
+//            status[0] = status[1];
+//            status[1] = 2;
+//                pc.printf("Up");
+                keyboard.keyCode(DOWN_ARROW);
+                led1 = 1;
+                led2 = 1;
+                led3 = 0.9;    
+                dwn = 1;
+                debounceDown.attach(&DBUp, 0.100);
+//                dwn = 0;
+                
+        }
          
          //When not moving
-         if (z>-150 && z<150){
-         status[0] = status[1];
-         status[1] = 0;
-         
-         if (L==0) {
-         //keyboard.keyCode(' ');
-         //keyboard.printf("OK");
-         //pc.printf("Break\n\r"); //Just a Reference
-         L=1;}}
+         if (x < -2000 && x > -3000 && y < 4000 && y > 3000 && midFl1 == 1){
+//            dwn = 0;
+            up = 0;
+            midFl1 = 0;
+            middle1.attach(&mid1,0.50);
+            led1 = 0.9;
+            led2 = 1;
+            led3 = 1;
+        }
+        
+         if (x < -1000 && x > -2800 && y < 3000 && y > 2000 && midFl2 == 1){
+            dwn = 0;
+//            up = 0;
+            midFl2 = 0;
+            middle2.attach(&mid2,0.50);
+            led1 = 0.9;
+            led2 = 0.9;
+            led3 = 1;
+        }
          
         wait_ms(50);
-        }
+    }
 }