Skip to content

Instantly share code, notes, and snippets.

@tigert
Last active November 17, 2020 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tigert/27685578033633a8bd6e72f8af21f543 to your computer and use it in GitHub Desktop.
Save tigert/27685578033633a8bd6e72f8af21f543 to your computer and use it in GitHub Desktop.
// This is a remix of USB joystick sketch by Mark Bennett at the arduino hub:
// https://create.arduino.cc/projecthub/markbennettuk/throttle-quadrant-and-trim-wheel-d746e8
// I use the Encoder library for my pitch trim wheel, limit the encoder internal counter to
// 0 to 1023 range in a crude way, and set that as the value for the trim axis.
//
// Update: read the potentiometer values three times to stabilize them a bit.
// Apparently there is a capacitor on the arduino that is shared by all
// analog input pins. This takes a short moment to recharge when you
// read another analog pin with a different voltage, thus the potentiometers
// were interfering with each other a bit. Doing a read (select the pin!),
// waiting a bit, and reading again seems to get rid of the fluctuations and
// interference I was experiencing. And while at it, lets do it once more, because
// why not? :-)
#include <Joystick.h>
#include <Encoder.h>
//Joystick_ Joystick;
Joystick_ Joystick(0x4,
JOYSTICK_TYPE_JOYSTICK, 0, 0,
true, true, true, // x, y, z
false, true, false, // Rx, Ry, Rz
false, false, false, false, false); // rudder, throttle, accel, brake, steering
Encoder trimWheel(3, 2);
int pitchTrim = 512;
// setting mode prints the pin value and translated value to the serial monitor
// int setting = -1; // no printing to the serial monitor
// int setting = 2; // values 0 - 5, print the pin values to the serial monitor
int setting = 2;
// put the max and min values from the analogRead in these arrays
// these are translated to a range of 0 - 1023
int axisLimits0[] = {51, 942};
int axisLimits1[] = {41, 912};
int axisLimits2[] = {122, 1004};
int axisLimits3[] = {0, 1023};
int axisLimits4[] = {0, 1023};
// turn axes on or off by setting these variables
bool a0Used = true; // throttle
bool a1Used = true; // prop rpm
bool a2Used = true; // mixture
bool a3Used = false; // these are not used yet.
bool a4Used = false;
bool a5Used = false;
void setup() {
if (a0Used) pinMode(A0, INPUT);
if (a1Used) pinMode(A1, INPUT);
if (a2Used) pinMode(A2, INPUT);
if (a3Used) pinMode(A3, INPUT);
if (a4Used) pinMode(A4, INPUT);
if (a5Used) pinMode(A5, INPUT);
Joystick.begin();
if (setting >= 0) Serial.begin(9600);
// reset elevator trim to center
trimWheel.write(512);
}
void loop() {
int value = 0;
int pos = 0;
// yes, this could all be done in one loop but hey, I have stuff to invent and this works..
if (a0Used) {
value = analogRead(A0);
delay(2);
value = analogRead(A0);
delay(2);
value = analogRead(A0);
pos = translateValue(value, axisLimits0[0], axisLimits0[1]);
Joystick.setXAxis(pos);
if (setting == 0) settingPrint(value, pos);
}
if (a1Used) {
value = analogRead(A1);
delay(2);
value = analogRead(A1);
delay(2);
value = analogRead(A1);
pos = translateValue(value, axisLimits1[0], axisLimits1[1]);
Joystick.setYAxis(pos);
if (setting == 1) settingPrint(value, pos);
}
if (a2Used) {
value = analogRead(A2);
delay(2);
value = analogRead(A2);
delay(2);
value = analogRead(A2);
pos = translateValue(value, axisLimits2[0], axisLimits2[1]);
Joystick.setZAxis(pos);
if (setting == 2) settingPrint(value, pos);
}
int trimValue = trimWheel.read();
if (trimValue < 0) {
trimWheel.write(0);
trimValue = 0;
}
if (trimValue > 1023) {
trimWheel.write(1023);
trimValue = 1023;
}
if (trimValue != pitchTrim) {
pitchTrim = trimValue;
Joystick.setRyAxis(pitchTrim);
}
// settingPrint(4, pitchTrim);
}
int translateValue(int v, int f1, int f2) {
// translates values to a 0 - 1023 range
int result = 0;
int start = 0;
float range = 0;
if (f1 < f2) {
start = f1;
range = f2 - f1;
}
else {
start = f2;
range = f1 - f2;
}
result = (v - start) * (1023 / range);
if (result < 0) result = 0;
if (result > 1023) result = 1023;
return result;
}
void settingPrint(int value, int pos) {
Serial.print(value);
Serial.print(" ");
Serial.println(pos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment