Skip to content

Instantly share code, notes, and snippets.

@vsTerminus
Created May 18, 2021 06:53
Show Gist options
  • Save vsTerminus/82c0b0fe1d34867c6dc6cca95e8427f8 to your computer and use it in GitHub Desktop.
Save vsTerminus/82c0b0fe1d34867c6dc6cca95e8427f8 to your computer and use it in GitHub Desktop.
Replacement Arduino program for TechAffliction Mega Shifter
// Ground digital pins 2 and 3 to press joystick buttons 0 and 1
//
// Based on the "Simple example application that shows how to
// read four Arduino digital pins and map them to the USB Joystick library"
// by Matthew Heironimus
// 2015-11-20
//
// Modified to replace a broken "Mega Shifter" 18-speed shift knob controller
// by vsTerminus
// 2021-05-18
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
2, 0, // Button count, Hat switch count
false, false, false, // No X, Y, or Z
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
}
// Last state of the button
int lastButtonState[2] = {0,0};
int currentButtonState[2] = {0,0};
void loop() {
// Read pin values
currentButtonState[0] = digitalRead(2); // Range Select Up presses Button 0
currentButtonState[1] = !digitalRead(3); // Splitter Forward presses Button 1
for (int i = 0; i <= 1; i++)
{
if (currentButtonState[i] != lastButtonState[i])
{
Joystick.setButton(i, currentButtonState[i]);
lastButtonState[i] = currentButtonState[i];
}
}
delay(50);
}
@vsTerminus
Copy link
Author

vsTerminus commented May 18, 2021

Several years ago I purchased a "Mega Shifter" from a company called TechAffliction. Basically it was one guy who modified 18-speed shift knobs used in semis to work as USB controllers that you could bind in games like American Truck Simulator or European Truck Simulator 2.

Recently-ish while moving my wheel to an actual stand for the first time, the USB port broke and the shifter knob stopped working. I did try to email TechAffliction but it seems like he has just stopped answering all communications and orders, despite the site still being up.

Anyway, the USB port is part of a little Arduino board. Specifically it looks like he used the KeeYees Pro Micro ATmega32U4 5V 16MHz board, an Arduino Leonardo knockoff. So I bought a three pack and decided I could just cut and solder the wires to the new board and write a simple two-button USB controller app.

Turns out it's pretty straightforward. You need the Arduino IDE and you need to install the Arduino Joystick Library, then just use the code above and upload it to the board. Select "Arduino Leonardo" as the board type in the IDE.

The code is a modification of the sample code that comes with the Joystick library. By default the pins will read as

Range Select Up = Pressed
Splitter Aft = Pressed

I inverted the state of the Splitter (Pin 3) so that it reads

Range Select Up = Pressed
Splitter Forward = Pressed

This way it works exactly as the original did. Windows even sees is as "Mega Shifter" still.

Anyway, I hope this helps someone. I know I'm not the only one who has had this issue.

@OrpheeGT
Copy link

OrpheeGT commented Aug 14, 2021

Hello,

I had the same issue with USB plug dead.
I took your code to flash a new board and it works great,thank you.

But I have the EasyJake option, it is soldered on pin4. Could you please add it to the code ?
https://techaffliction.com/index.php?route=product/product&product_id=53

The joystick should then have 3 buttons shown on Windows.

Many thanks !

Edit : Actually I understood how it work. and added it myself.
Thanks!

// Ground digital pins 2, 3 and 4 to press joystick buttons 0,1 and 2
//
// Based on the "Simple example application that shows how to
// read four Arduino digital pins and map them to the USB Joystick library"
// by Matthew Heironimus
// 2015-11-20
//
// Modified to replace a broken "Mega Shifter" 18-speed shift knob controller
// by vsTerminus
// 2021-05-18
//--------------------------------------------------------------------

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
3, 0, // Button count, Hat switch count
false, false, false, // No X, Y, or Z
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering

void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);

// Initialize Joystick Library
Joystick.begin();
}

// Last state of the button
int lastButtonState[3] = {0,0,0};
int currentButtonState[3] = {0,0,0};

void loop() {

// Read pin values
currentButtonState[0] = digitalRead(2); // Range Select Up presses Button 0
currentButtonState[1] = !digitalRead(3); // Splitter Forward presses Button 1
currentButtonState[2] = !digitalRead(4); // Motobrake Up presses Button 2

for (int i = 0; i <= 2; i++)
{
if (currentButtonState[i] != lastButtonState[i])
{
Joystick.setButton(i, currentButtonState[i]);
lastButtonState[i] = currentButtonState[i];
}
}

delay(50);
}

@GeneralMarkusParkus
Copy link

Had this same USB issue and replaced the board but had no idea of the code to use with the hall sensors.
This totally saved the day!

Thanks you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment