Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino Thumbstick
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);
}
// calculate neutral position
xZero = analogRead(xPin);
yZero = analogRead(yPin);
thumbSt.xAxis = 0;
thumbSt.yAxis = 0;
}
void loop(){
xValue = analogRead(xPin) - xZero;
yValue = analogRead(yPin) - yZero;
if(abs(xValue) < deadzone) {
xValue = 0;
}
if(abs(yValue) < deadzone) {
yValue = 0;
}
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment