petran1420 wrote:
Hi @wermy
By any chance did you come across your sample code/4 pins to us for wiring this up?
Hey sorry for the delay. This was a work-in-progress (basically a couple chunks of sample code with some sort of auto-calibration stuff I was adding):
Code: Select all
const bool DEBUG = true; // set to true to debug the raw values
int xPin = A0;
int yPin = A1;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5; // smaller values will be set to 0
int xPosMax = 0;
int xNegMax = 0;
int yPosMax = 0;
int yNegMax = 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;
xPosMax = 0;
xNegMax = 0;
yPosMax = 0;
yNegMax = 0;
}
void loop() {
xValue = analogRead(xPin) - xZero;
yValue = analogRead(yPin) - yZero;
if (abs(xValue) < deadzone) {
xValue = 0;
}
if (abs(yValue) < deadzone) {
yValue = 0;
}
if (xValue > 0 && xValue > xPosMax) {
xPosMax = xValue;
} else if (xValue < 0 && xValue < xNegMax) {
xNegMax = xValue;
}
if (yValue > 0 && yValue > yPosMax) {
yPosMax = yValue;
} else if (yValue < 0 && yValue < yNegMax) {
yNegMax = yValue;
}
float xMax = abs(xPosMax);
if (xValue < 0) {
xMax = abs(xNegMax);
}
float yMax = abs(yPosMax);
if (yValue < 0) {
yMax = abs(yNegMax);
}
if (DEBUG && (xValue != 0 || yValue != 0)) {
Serial.print("X: ");
Serial.print((float)((float)xValue / xMax));
Serial.print(", Y: ");
Serial.print((float)((float)yValue / yMax));
Serial.print(", Raw X: ");
Serial.print(xValue);
Serial.print(", xPosMax: ");
Serial.print(xPosMax);
Serial.print(", xNegMax: ");
Serial.print(xNegMax);
Serial.print(", Raw Y: ");
Serial.print(yValue);
Serial.print(", yPosMax: ");
Serial.print(yPosMax);
Serial.print(", yNegMax: ");
Serial.println(yNegMax);
}
}
Again, this was something I whipped up in like half an hour so I don't think I'd just plop it into a GBZ or something just yet.

But what I did add seems to work fairly well: when it starts running you can make a full circle a few times with the joystick and it should sort of calibrate itself with regards to max values in x/y. You can use xValue/xMax and yValue/yMax to get a value between 0 and 1, which I *think* is what you need for an HID joystick. If not then it is easy enough to go from that to whatever range of values you need.
I used this as a reference (and I think I took some chunks of code from here too):
http://www.instructables.com/id/Add-a-l ... /?ALLSTEPS
Pins A0 and A1 will vary depending on what you're using. If it's a Teensy then you can look here to see which ones they are:
https://www.pjrc.com/teensy/pinout.html
Have fun!