Skip to content

Instantly share code, notes, and snippets.

@vsTerminus
Created May 18, 2021 06:53
Show Gist options
  • Select an option

  • Save vsTerminus/82c0b0fe1d34867c6dc6cca95e8427f8 to your computer and use it in GitHub Desktop.

Select an option

Save vsTerminus/82c0b0fe1d34867c6dc6cca95e8427f8 to your computer and use it in GitHub Desktop.

Revisions

  1. vsTerminus created this gist May 18, 2021.
    51 changes: 51 additions & 0 deletions MegaShifter.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    // 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);
    }