aaron2008
Published © LGPL

Universal Esplora Game Controller

Create a game controller that is supported by nearly every joystick-controlled game, and that you can easily remap in the game's settings

IntermediateShowcase (no instructions)460
Universal Esplora Game Controller

Things used in this project

Hardware components

Arduino Esplora
Arduino Esplora
×1

Software apps and online services

Arduino IDE
Arduino IDE
Any game with joystick support
For exmaple Farming Simulator 17, 19 or 22. It must support joysticks that are connected via usb, as the esplora is emulating a such device to report the button positions and other inputs to the game and control it
Gamepad Tester
For testing recognition and button transmission without a supported game or if there is a problem with the game to check if the virtual joystick is recognized by the OS

Hand tools and fabrication machines

Joystick Library
Install it as in the instructions on gthub.
Arduino Esplora

Story

Read more

Code

JStest.ino

Arduino
Upload it to the Eplora using Arduino IDE
#include <Joystick.h>
#include <Esplora.h>
int lastButtonState[4] = {0, 0, 0, 0};
int currentButtonState, valJS, valJSprev, mode, Xprev, Yprev, Xval, Yval;
String chg;
boolean hasChanges = true;
Joystick_ Joystick(0x03, 0x04, 36, 0, true, true, true, true, true, true, false, false, false, false, false);;
void setup() {
  // put your setup code here, to run once:
  Joystick.begin(false);
  Serial.begin(9600);
  Esplora.tone(261);
  delay(200);
  Esplora.tone(65);
  delay(400);
  Esplora.tone(440);
  delay(200);
  Esplora.noTone();
}

void loop() {
  // put your main code here, to run repeatedly:
  Joystick.setXAxisRange(-512, 512);
  Joystick.setYAxisRange(-512, 512);
  Joystick.setRxAxisRange(-512, 512);
  Joystick.setRyAxisRange(-512, 512);
  Joystick.setRzAxisRange(-512, 512);
  Joystick.setZAxisRange(0, 5);
  Xprev = Xval;
  Yprev = Yval;
  Xval = Esplora.readJoystickX();
  Yval = Esplora.readJoystickY();
  mode = map(Esplora.readSlider(), 0, 1023, 1, 3);
  hasChanges = false;
  mode = map(Esplora.readSlider(), 0, 1023, 1, 3);
  if (round(Xval / 2) != round(Xprev / 2) || round(Yval / 2) != round(Yprev / 2))
  {
    if (mode == 1)
    {
      Joystick.setXAxis(Xval);
      Joystick.setYAxis(Yval);
      hasChanges = true;
      chg = "Joystick";
    }
    else
    {
      Joystick.setRxAxis(Xval);
      Joystick.setRyAxis(Yval);
      hasChanges = true;
      chg = "Joystick";
    }
  }
  for (int index = 0; index < 4; index++)
  {
    if (mode == 1)
    {
      currentButtonState = !Esplora.readButton(index + 1);
      if (currentButtonState != lastButtonState[index])
      {
        hasChanges = true;
        chg = "BTN";
        Joystick.setButton(index, currentButtonState);
        lastButtonState[index] = currentButtonState;
      }
    }
    if (mode != 1)
    {
      currentButtonState = !Esplora.readButton(index + 1);
      if (currentButtonState != lastButtonState[index])
      {
        hasChanges = true;
        chg = "BTN";
        Joystick.setButton(index + 11, currentButtonState);
        lastButtonState[index] = currentButtonState;
      }
    }
  }
  valJSprev = valJS;
  if (Esplora.readJoystickSwitch() == LOW)
  {

    valJS = 1;
  }
  else
  {
    valJS = 0;
  }
  if (mode == 1 && valJSprev != valJS)
  {
    Joystick.setButton(10, valJS);
    hasChanges = true;
    chg = "BTNJS";
  }
  else
  {
    if (mode != 1 && valJSprev != valJS)
    {
      Joystick.setButton(20, valJS);
      hasChanges = true;
      chg = "BTNJS";
    }
  }
  if (hasChanges)
  {
    Joystick.sendState();
    if(Serial)
    {
    Serial.println(chg);
    }
  }
  if (mode == 1)
  {
    Esplora.writeRGB(255, 0, 0);
  }
  else
  {
    Esplora.writeRGB(0, 255, 0);
  }
  delay(50);
}

Credits

aaron2008
0 projects • 0 followers

Comments