Last active
August 17, 2020 08:39
-
-
Save pixelbreaker/0c663152b4051b72788075901cf8e425 to your computer and use it in GitHub Desktop.
Logitech pedal-handbrake and shifter arduino combo
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> | |
// include library from https://github.com/MHeironimus/ArduinoJoystickLibrary | |
// Create the Joystick | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 8, 0, false, false, false, false, false, false, false, true, false, false, false); | |
#define HS_XAXIS_12 400 | |
#define HS_XAXIS_56 600 | |
#define HS_YAXIS_135 800 | |
#define HS_YAXIS_246 300 | |
#define DI_REVERSE 1 | |
int b[16]; | |
int gear = 0; // Default value is neutral | |
void setup() { | |
// Handbrake | |
pinMode(A0, INPUT_PULLUP); | |
// G29 shifter analog inputs configuration | |
pinMode(A1, INPUT_PULLUP); // X axis | |
pinMode(A2, INPUT_PULLUP); // Y axis | |
pinMode(2, INPUT); // push down gearstick to go into reverse | |
for (int i = 0; i < 16; i++) b[i] = 0; | |
Joystick.begin(); | |
} | |
// Last state of the button | |
int lastButtonState = 0; | |
void loop() { | |
// handbrake | |
int pot = analogRead(A0); | |
int mapped = map(pot, 0, 1023, 0, 255); | |
Joystick.setThrottle(mapped); | |
// shifter | |
int x = analogRead(A1); // X axis | |
int y = analogRead(A2); // Y axis | |
int _isreverse = digitalRead(2); | |
int _gear_ = 0; | |
if ( _isreverse == 1 ) { | |
_gear_ = 8; | |
b[DI_REVERSE] = 1; | |
} else { | |
if (x < HS_XAXIS_12) // Shifter on the left? | |
{ | |
if (y > HS_YAXIS_135) _gear_ = 1; // 1st gear | |
if (y < HS_YAXIS_246) _gear_ = 2; // 2nd gear | |
} | |
else if (x > HS_XAXIS_56) // Shifter on the right? | |
{ | |
if (y > HS_YAXIS_135) _gear_ = 5; // 5th gear | |
if (y < HS_YAXIS_246) _gear_ = 6; // 6th gear | |
} | |
else // Shifter is in the middle | |
{ | |
if (y > HS_YAXIS_135) _gear_ = 3; // 3rd gear | |
if (y < HS_YAXIS_246) _gear_ = 4; // 4th gear | |
} | |
} | |
if (gear != 6) b[DI_REVERSE] = 0; // Reverse gear is allowed only on 6th gear position | |
if (_gear_ != gear ) { | |
gear = _gear_; | |
for (int i = 0; i <= 10 ; i++ ) Joystick.setButton(i, LOW); | |
Joystick.setButton(gear - 1, HIGH); | |
} | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment