Closed mitko-kamburow closed 1 year ago
Hello. If I'm understanding you right, you want a button to toggle between controlling gears 1-6 and gears 7-12?
Start by modifying the array of gears at the top to include the higher gears. The number of button outputs for the joystick will scale with that.:
const int Gears[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -1 };
Then in the updateJoystick()
function, add the read for the button to modify the gear number coming out of the shifter:
pinMode(3, INPUT_PULLUP); // this should probably go in setup()
int currentGear = shifter.getGear();
if(digitalRead(3) == LOW && currentGear > 0) currentGear *= 2;
for (int i = 0; i < NumGears; i++) {
if (currentGear == Gears[i]) {
Joystick.pressButton(i);
}
else {
Joystick.releaseButton(i);
}
}
That's the gist. The toggle switch should also be debounced for best results.
I want to add a single button, the game has a function to switch between those two ranges.
Oh I see. In the joystick definition at the top:
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
NumGears + SendReverseRaw, // number of buttons (7 gears: reverse and 1-6)
0, // number of hat switches (none)
SendAnalogAxis, SendAnalogAxis, // include X and Y axes for analog output, if set above
false, false, false, false, false, false, false, false, false); // no other axes
The third line down sets the number of buttons in the output. Add 1 to that.
Define the pin as a global constant:
const int ButtonPin = 3
Then in setup()
:
pinMode(ButtonPin, INPUT_PULLUP);
And loop()
:
const bool ButtonState = !digitalRead(ButtonPin);
Joystick.setButton(n, ButtonState);
Where 'n' is the button number, indexed at 0. If you're using the standard 7 gears and not the "SendReverseRaw" feature, it will be 7.
You will also need to remove the shifter.gearChanged()
check within the loop, otherwise the joystick state won't be updated unless the gear changes as well. For best results add a debouncing library of your choice.
I'll try.
What do I need to type in the joystick definition? It doesn't work. I need to short pin 3 with GND, right?
I changed some lines and now everything is working.
I changed some lines and now everything is working.
Hello, Could you share what changes you make?
Thank you.
Hi, mechn34! That's my alt account. I don't remember what I have changed, but you may open an issue and I will try to help.
@mechn34 my version sketch. Shifter + 2 button.
I'm not a programmer, I'm an amateur. I added it like this.
const int Pin_ShifterX = A0;
const int Pin_ShifterY = A2;
const int Pin_ShifterRev = 2;
// add 2 pin for button
const int ButtonPin = 3;
const int ButtonPin2 = 4;
Change NumGears + SendReverseRaw, add +2
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, // default report (no additional pages)
JOYSTICK_TYPE_JOYSTICK, // so that this shows up in Windows joystick manager
NumGears + SendReverseRaw + 2, // number of buttons (7 gears: reverse and 1-6) // 2 add buttons
void setup() {
// Add 2 button
// Pin > GND
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
void loop() {
// add 2 button
// Pin 3 > GND
const bool ButtonState = !digitalRead(3);
Joystick.setButton(7, ButtonState);
// Pin 4 > GND
const bool ButtonState2 = !digitalRead(4);
Joystick.setButton(8, ButtonState2);
And change
// change true to false
if (SendAnalogAxis == false || shifter.gearChanged()) {
updateJoystick();
}
All transmission buttons and two additional buttons work now. One button to pin 3 and ground, the other to pin 4 and ground.
I'm not a programmer, I'm an amateur. I added it like this.
Me too. Thank you for your help.
I need a button to switch from 6 and 12 gears(for ETS2), but I don't know how to add support for it. I need it to be something like pin 3 connected to ground. I'm using the Shifter example.