Created
August 15, 2014 06:03
-
-
Save vindolin/1f1247572659537b3915 to your computer and use it in GitHub Desktop.
Arduino Thumbstick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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