Created
April 17, 2024 16:04
-
-
Save stephenwilley/440fdc2b3b48ee066a28c53a3df98c9d to your computer and use it in GitHub Desktop.
Joystick Emulation for https://www.thingiverse.com/make:1006038
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
#include <Joystick.h> | |
//Potentiometer Setup | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK, | |
0, 0, // Button Count, Hat Switch Count | |
true, true, true, // X, Y, and Z Axis | |
true, false, false, // Rx, but no Ry or Rz (Rx represents throttle as fs2020 does not recognize the throttle axis!) | |
false, false, // No rudder or throttle (represented as Rx!) | |
false, false, false); // No accelerator, brake, or steering; | |
int throttle = 0; | |
int fuel = 0; | |
int flaps = 0; | |
int trimWheel = 512; | |
static int trimA = 2; | |
static int trimB = 3; | |
unsigned char encoder_A; | |
unsigned char encoder_B; | |
unsigned char encoder_A_prev=0; | |
unsigned long currentTime; | |
unsigned long loopTime; | |
void setup(){ | |
// Use pins 4, 5 and 6 as Ground and VCC substitutes | |
pinMode(4, OUTPUT); | |
pinMode(7, OUTPUT); | |
pinMode(6, OUTPUT); | |
digitalWrite(4, LOW); | |
digitalWrite(7, HIGH); | |
digitalWrite(6, HIGH); | |
// Now for the trim wheel bits | |
pinMode(trimA, INPUT_PULLUP); | |
pinMode(trimB, INPUT_PULLUP); | |
currentTime = millis(); | |
loopTime = currentTime; | |
Joystick.begin(); //Starts joystick | |
//Serial.begin(9600); | |
} | |
void loop(){ | |
throttle = analogRead(A0); | |
Joystick.setRxAxis(throttle); | |
fuel = analogRead(A1); | |
Joystick.setXAxis(fuel); | |
flaps = analogRead(A2); | |
Joystick.setYAxis(flaps); | |
currentTime = millis(); | |
if(currentTime >= (loopTime + 20)){ | |
encoder_A = digitalRead(trimA); // Read encoder pins | |
encoder_B = digitalRead(trimB); | |
if((!encoder_A) && (encoder_A_prev)){ | |
// A has gone from high to low | |
if(encoder_B) { | |
trimWheel += 4; | |
if (trimWheel > 1023) { trimWheel = 1023; } | |
} else { | |
trimWheel -= 4; | |
if (trimWheel < 0) { trimWheel = 0; } | |
} | |
} | |
} | |
encoder_A_prev = encoder_A; // Store value of A for next time | |
Joystick.setZAxis(trimWheel); | |
delay(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment