Skip to content

Instantly share code, notes, and snippets.

@vindolin
Created August 15, 2014 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vindolin/1f1247572659537b3915 to your computer and use it in GitHub Desktop.
Save vindolin/1f1247572659537b3915 to your computer and use it in GitHub Desktop.
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