Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Logitech pedal-handbrake and shifter arduino combo
#include <Joystick.h>
// Create the Joystick
Joystick_ Joystick(0x03, JOYSTICK_TYPE_MULTI_AXIS, 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
You can’t perform that action at this time.