ThumbState_t thumbSt;
const bool DEBUG = false; // set to true to debug the raw values
int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5; // smaller values will be set to 0
void setup(){
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
if(DEBUG) {
Serial.begin(9600);
}
// If at the neutral position you are reading a number too large, xZero will be positive, and get subtracted in the void loop, otherwise it will be negative, and get added.
xZero = analogRead(xPin)-2048;
yZero = analogRead(yPin)-2048;
thumbSt.xAxis = 0;
thumbSt.yAxis = 0;
}
void loop(){
xValue = analogRead(xPin) - xZero;
yValue = analogRead(yPin) - yZero;
//I'm pretty sure you need to keep the values between 0-4096, but I could be wrong
if( yValue < 0 ){
yValue = 0 ;
}
else if( yValue > 4096 ){
yValue = 4096;
}
if( xValue < 0 ){
xValue = 0;
}
else if( xValue > 4096 ){
xValue = 4096;
}
//I changed the deadzone stuff to work with values 0-4096
if(xValue< 2048+deadzone && xValue>2048-deadzone) {
xValue = 2048;
}
if(yValue< 2048+deadzone && yValue>2048-deadzone) {
yValue = 2048;
}
thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768); // here the axis is inverted
thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);
if(DEBUG) {
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
}
// Send to USB
Thumbstick.setState(&thumbSt);
}