519_La

Fork of Joystick_skeleton by Carter Sharer

Revision:
6:7a0edb75e7fd
Parent:
5:4e531d160998
diff -r 4e531d160998 -r 7a0edb75e7fd Joystick.cpp
--- a/Joystick.cpp	Fri Nov 04 21:27:03 2016 +0000
+++ b/Joystick.cpp	Sun Nov 13 20:39:37 2016 +0000
@@ -42,14 +42,14 @@
     float avg = 0;
     
     for (int i = 0; i < 5; i++) {
-        avg += horiz.read()
+        avg += horiz.read();
     }
     avg /= 5;
     //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
     if(avg > rawMaxH) {
         rawMaxH = avg; 
     }
-    if(avg < rawMinH) {
+    else if(avg < rawMinH) {
         rawMinH = avg;
     }
     
@@ -66,48 +66,55 @@
     //scale (_max/_min). 
     float val;
     if(avg >= raw_hc) //Positive Range
-        val = 0; 
+        val = ((avg - raw_hc)/range_pos)*(_max - 1); 
     else  //Negative Range
-        val = 0; 
+        val = ((raw_hc - avg)/range_neg)*(_min + 1); 
          
     //(7)Here we will apply a dead zone.  If the |value| is <= our deadzone then 
     //set it to 0.  Otherwise we need to shift the value closer to 0 by dead zone
     
     
-    return horiz.read();
+    return val;
 }
 
 //(8) Impliment vertial the same as you did for horizontal
 //Returns the scaled horizontal value of joystick
 float Joystick::vertical(void) {
     //Get average value (5 samples)
-    float avg = vert.read();
+    float avg = 0;
     
+    for(int i = 0; i < 5; i++) {
+        avg += vert.read();
+    }
+    avg /= 5;
     //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
-    rawMaxV = avg;
-    rawMinV = avg;
+    if(avg > rawMaxV) {
+        rawMaxV = avg; 
+    }
+    if(avg < rawMinV) {
+        rawMinV = avg;
+    }
     
     //(5)Here we will calculate the total range (Travel) of the joystick 
     //using the rawMax/rawMin values we have seen thus far
     //Since the joystick is not symetrical we have to calculate two ranges
     //Calculate the range from [center, max] and [center, min]
-    float range_pos = 0;
-    float range_neg = 0;
+    float range_pos = rawMaxV - raw_vc;
+    float range_neg = raw_vc - rawMinV;
     
     //(6)Here we will calculate how much our current reading is in one 
     //of the ranges, this will give us a percentage of our _max value we 
     //set in setScale. Then we can apply the scale by multiplying it by our 
     //scale (_max/_min).
-    float val;
-    if(avg >= raw_vc) //find scaled pot value
-        val = 0;
-    else 
-        val = 0;
+    float val = 0;
+    if(avg >= raw_vc) //Positive Range
+        val = ((avg - raw_vc)/range_pos)*(_max - 1); 
+    else  //Negative Range
+        val = ((raw_vc - avg)/range_neg)*(_min + 1); 
     
     //(7)Here we will apply a dead zone.  If the |value| is <= our deadzone then 
     //set it to 0.  Otherwise we need to shift the value closer to 0 by dead zone    
-    
-    return vert.read();
+    return val;
 }
 
 //Set the Min and Max Values of joystick ex: -100, +100