As I said, this is only a dirty code just for test purposes with a breadboard. So please ignore the other spaghetti codes in there for this moment, they work perfect so far for the other things
. I will sort it later! It's only about the encoder I don't understand.
// Requires Arduino Joystick Library https://github.com/MHeironimus/ArduinoJoystickLibrary
#include <Joystick.h>
//JOYSTICK SETTINGS
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
8, //number of buttons
0, //number of hat switches
//Set as many axis to "true" as you have potentiometers for
false, // y axis
false, // x axis
true, // z axis
true, // rx axis
false, // ry axis
false, // rz axis
false, // rudder
false, // throttle
false, // accelerator
false, // brake
false); // steering wheel
// put the max and min values from the analogRead in these arrays
// these are translated to a range of 0 - 1023
int axisLimits0[] = {171+40, 878};
int axisLimits1[] = {111, 843-50};
int axisLimits2[] = {0, 1023};
int axisLimits3[] = {0, 1023};
// turn axes on or off by setting these variables
bool a0Used = true;
bool a1Used = true;
bool a2Used = false;
bool a3Used = false;
// 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 - 3, print the pin values to the serial monitor
int setting = -1;
int currentButtonState0;
int lastButtonState0;
int currentButtonState1;
int lastButtonState1;
int currentButtonState2;
int lastButtonState2;
int currentButtonState3;
int lastButtonState3;
int currentButtonState4;
int lastButtonState4;
int currentButtonState5;
int lastButtonState5;
int currentButtonState6;
int lastButtonState6;
int encoderPinA = 6;
int encoderPinB = 7;
int encoderPos = 0;
int encoderPinALast = LOW;
int encoderPinANow = LOW;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
if(a0Used) pinMode(A0, INPUT_PULLUP);
if(a1Used) pinMode(A1, INPUT_PULLUP);
if(a2Used) pinMode(A2, INPUT_PULLUP);
if(a3Used) pinMode(A3, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
Joystick.setZAxisRange(0, 1024);
Joystick.setRxAxisRange(1024, 0);
if(setting >= 0) Serial.begin(9600);
}
void loop() {
//Encoder Trimmer
encoderPinANow = digitalRead(encoderPinA);
if ((encoderPinALast == HIGH) && (encoderPinANow == LOW)) {
if (digitalRead(encoderPinB) == HIGH) {
Joystick.setButton(4, HIGH);
} else {
Joystick.setButton(5, HIGH);
}
encoderPinALast = encoderPinANow;
}
int value = 0;
int pos = 0;
if(a0Used){
value = analogRead(A0);
pos = translateValue(value, axisLimits0[0], axisLimits0[1]);
Joystick.setZAxis(pos);
if(setting == 0) settingPrint(value, pos);
}
if(a1Used){
value = analogRead(A1);
pos = translateValue(value, axisLimits1[0], axisLimits1[1]);
Joystick.setRxAxis(pos);
if(setting == 1) settingPrint(value, pos);
}
if(a2Used){
value = analogRead(A2);
pos = translateValue(value, axisLimits2[0], axisLimits2[1]);
Joystick.setRyAxis(pos);
if(setting == 2) settingPrint(value, pos);
}
if(a3Used){
value = analogRead(A3);
pos = translateValue(value, axisLimits3[0], axisLimits3[1]);
Joystick.setRzAxis(pos);
if(setting == 3) settingPrint(value, pos);
}
// Thrust Revers
if (analogRead(A0) < 200) {
Joystick.setButton(6, HIGH);
} else {
Joystick.setButton(6, LOW);
}
if (analogRead(A1) > 800) {
Joystick.setButton(7, HIGH);
} else {
Joystick.setButton(7, LOW);
}
// Read Switches
int currentButtonState0 = !digitalRead(2); // Button 0
if (currentButtonState0 != lastButtonState0)
{
Joystick.setButton(0, currentButtonState0);
lastButtonState0 = currentButtonState0;
}
delay(5);
}
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);
}