519_La

Fork of Joystick_skeleton by Carter Sharer

Revision:
1:c42a77267f7b
Parent:
0:46523bf02e61
Child:
2:893fd930d3fb
diff -r 46523bf02e61 -r c42a77267f7b Joystick.cpp
--- a/Joystick.cpp	Tue Oct 18 20:26:41 2016 +0000
+++ b/Joystick.cpp	Thu Oct 20 22:16:14 2016 +0000
@@ -1,3 +1,7 @@
+//Joystick (FULL IMPLIMENTATION)
+//Author: Carter Sharer 
+//Date: 10/18/16
+
 #include "Joystick.h"
 #include "mbed.h"
 
@@ -30,7 +34,6 @@
     if(avg < rawMinH)
         rawMinH = avg;
     
-    
     //Here we will calculate the total range (Travel) of the joystick 
     //using the rawMax/rawMin values we have seen thus far
     //Calculate the range from [center, max] and [center, min]
@@ -42,11 +45,10 @@
     //Then we can scale this by multiplying it by our scale (_max/_min)
     float val;
     if(avg >= raw_hc) 
-        val = ((avg - raw_hc) / range_pos) * _max; // percent to max * max
+        val = (((avg - raw_hc) / range_pos) * _max) + DEAD_ZONE/2; // percent to max * max
     else 
-        val = ((raw_hc - avg) / range_neg) * _min; //percent to min * min
+        val = (((raw_hc - avg) / range_neg) * _min) - DEAD_ZONE/2; //percent to min * min
          
-    
     //Here we will apply a dead zone
     if((val <= DEAD_ZONE/2) && (val >= -DEAD_ZONE/2)) {
         return 0;
@@ -76,9 +78,9 @@
     
     float val;
     if(avg >= raw_vc) //find scaled pot value
-        val = (((avg - raw_vc) / range_pos) * _max); 
+        val = (((avg - raw_vc) / range_pos) * _max) + DEAD_ZONE/2; 
     else 
-        val = (((raw_vc - avg) / range_neg) * _min);
+        val = (((raw_vc - avg) / range_neg) * _min) - DEAD_ZONE/2;
     
     //If val is in dead zone range return 0
     if((val <= DEAD_ZONE/2) && (val >= -DEAD_ZONE/2)) {
@@ -95,6 +97,6 @@
 
 //Set the Min and Max Values of joystick ex: -100, +100
 void Joystick::setScale(float min, float max) {
-       _min = min; 
-       _max = max;
+       _min = min-1; //Add 1 for round off error
+       _max = max+1; //Add 1 for round off error
 }
\ No newline at end of file