519_La

Fork of Joystick_skeleton by Carter Sharer

Revision:
5:4e531d160998
Parent:
4:aaca0f94b646
Child:
6:7a0edb75e7fd
--- a/Joystick.cpp	Tue Oct 25 15:09:06 2016 +0000
+++ b/Joystick.cpp	Fri Nov 04 21:27:03 2016 +0000
@@ -16,33 +16,49 @@
     raw_hc = 0;
     raw_vc = 0; 
     
+    for (int i = 0; i<5; i++) {
+        raw_hc += horiz.read();
+        raw_vc += vert.read();
+    }
+    raw_hc /= 5;
+    raw_vc /= 5;
+    
+    
     //(2)Initalize the Rax Max to some value less then the real max value.  
     //We dont know what the max value will be until we read it, thats ok. 
     //But we can assume it will be greater then 0.8 (center + 0.3) so lets 
     //set it to that for now.  We will update this later if we see a larger value
     //Now do the same for the the Raw Min
     float delta = 0.3;
-    rawMinH = 0;
-    rawMaxH = 0;
-    rawMinV = 0;
-    rawMaxV = 0;
+    rawMinH = raw_hc - delta;
+    rawMaxH = raw_hc + delta;
+    rawMinV = raw_vc - delta;
+    rawMaxV = raw_vc + delta;
 }
 
 //Returns the scaled vertial value of joystick
 float Joystick::horizontal(void) {
     //(3)Get average val (5 samples)
-    float avg = horiz.read();
+    float avg = 0;
     
+    for (int i = 0; i < 5; i++) {
+        avg += horiz.read()
+    }
+    avg /= 5;
     //(4)Watch for Max and Min Values, if we see a new max/min update Raw Max/Min
-    rawMaxH = avg;    
-    rawMinH = avg;
+    if(avg > rawMaxH) {
+        rawMaxH = avg; 
+    }
+    if(avg < rawMinH) {
+        rawMinH = 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 = rawMaxH - raw_hc;
+    float range_neg = raw_hc - rawMinH;
     
     //(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