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);
}
@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