Created
December 4, 2020 00:45
-
-
Save tigert/7255ff8e9bc7a6627cf75e06acc5d160 to your computer and use it in GitHub Desktop.
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
// 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? :-) | |
// | |
// Update 2: add three buttons to pins 4-6. | |
#include <Joystick.h> | |
#include <Encoder.h> | |
//Joystick_ Joystick; | |
Joystick_ Joystick(0x4, | |
JOYSTICK_TYPE_JOYSTICK, 3, 0, // type, button count, hatswitch count | |
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 = -1; | |
// put the max and min values from the analogRead in these arrays | |
// these are translated to a range of 0 - 1023 | |
int axisLimits0[] = {82, 922}; | |
int axisLimits1[] = {122, 917}; | |
int axisLimits2[] = {174, 967}; | |
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; | |
// Constant that maps the phyical pin to the joystick button. | |
const int pinToButtonMap = 4; | |
// Last state of the button | |
int lastButtonState[4] = {0,0,0,0}; | |
void setup() { | |
// Initialize Button Pins | |
pinMode(4, INPUT_PULLUP); | |
pinMode(5, INPUT_PULLUP); | |
pinMode(6, INPUT_PULLUP); | |
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; | |
// Read button pin values | |
for (int index = 0; index < 4; index++) | |
{ | |
int currentButtonState = !digitalRead(index + pinToButtonMap); | |
if (currentButtonState != lastButtonState[index]) | |
{ | |
Joystick.setButton(index, currentButtonState); | |
lastButtonState[index] = currentButtonState; | |
} | |
} | |
// 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