// https://forum.arduino.cc/t/flight-sim-throttle-coding/1318665/
// two potentiometers mapped to 0 to 65 with 20 as the "idle" point
int throttle[] = {A0, A1}; // array of throttles
const int arraySize = sizeof(throttle) / sizeof(throttle[0]); // size of array
int throttleReading, throttleDegrees[arraySize], element, oldDegrees[arraySize];
#define DELAY 50 // delay between analog read
#define MININ 0 // analog input minimum value
#define MAXIN 1023 // analog input maximum balue
#define MINOUT 0 // analog input re-mapped to minimum degrees
#define MAXOUT 65 // analog input re-mapped to maximum degrees
void setup() {
Serial.begin(115200); // serial monitor communications
for (int i = 0; i < arraySize; i++)
pinMode(throttle[i], INPUT); // configure input pins
Serial.println("Move throttle A0 to IDLE (20 degrees).");
// wait for throttle to be "20 degrees"
while (map(analogRead(throttle[0]), MININ, MAXIN, MINOUT, MAXOUT) != 20);
}
void loop() {
if (element > arraySize - 1) { // count through throttle array elements
element = 0; // reset array counter
}
// Read 10 bit ADC input, 0 to 1023. Output 0 to 65 degrees
throttleReading = analogRead(throttle[element]); // 10 bit ADC = 0 to 1023
throttleDegrees[element] = map(throttleReading, MININ, MAXIN, MINOUT, MAXOUT); // output 0 to 65 degrees
if (oldDegrees[element] != throttleDegrees[element]) { // has degree changed
for (int i = 0; i < arraySize; i++) { // local variable to print all throttle traces
Serial.print(" |");
oldDegrees[i] = throttleDegrees[i]; // store new degree
Serial.print("| Trottle:"); // start trace
Serial.print(i); // throttle number
Serial.print(" |");
format(throttleDegrees[i]); // degrees output
Serial.print(throttleDegrees[i]);
Serial.print(" Degrees | ");
switch (throttleDegrees[i]) {
case 65: Serial.print("FFWD ^^"); break;
case 21 ... 64: Serial.print("fwd ^"); break;
case 20: Serial.print("IDLE =="); break;
case 1 ... 19: Serial.print("rev v"); break;
case 0: Serial.print("FREV vv"); break;
}
// Serial.print(" |"); // end of trace
if (i == arraySize - 1) // if last throttle...
Serial.println(" ||"); // ... add new line
delay(DELAY); // keep output from running off the monitor
}
}
element++; // next throttle array elemnt
}
void format(int i) { // prefix spaces
if (i > -10 && i < 0) Serial.print(" ");
if (i >= 0 && i <= 9) Serial.print(" ");
if (i > 9) Serial.print(" ");
}