Wednesday, October 3, 2018

The most basic DIY selfdestruct-controller 101 for Star Citizen with Arduino Leonardo

The noises in the background are from my 3d-printer creating parts for my "real" controller...

Creating a simple DIY input controller with Arduino Leonardo is pretty easy.

First you need to setup the hardware:

Therefor you need a controller board like the Leonardo that is able to send keyboard commands via USB. Then you have to create a circuit between 2 pins (usually ground pin and a signal pin) that you can open and close. The most basic version is simply wiring and unwiring those pins by hand. So this was made to demonstrate this in the most basic way i could think of.
Last but not least you need to connect the Board via USB to you computer to finish the hardware setup. Now you are able to program the board with software like Arduino IDE. After that you will be able to send keyboard commands from board to PC.


Now you can program the board. I won't describe how to use the Arduino IDE in detail as there are many tutorials in the web already - like this official one on the Arduino website.

The Code:


/*
  Input Pull-up Serial

  This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
  input on pin 2 and prints the results to the Serial Monitor.

  The circuit:
  - momentary switch attached from pin 2 to ground
  - built-in LED on pin 13

  Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
  20K-ohm resistor is pulled to 5V. This configuration causes the input to read
  HIGH when the switch is open, and LOW when it is closed.

  created 14 Mar 2012
  by Scott Fitzgerald

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/InputPullupSerial
*/

//include keyboard library for key commands
#include "Keyboard.h"

int lastVal = HIGH;

void setup() {
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    if (lastVal == LOW){
      Keyboard.release(KEY_BACKSPACE);
      delay(50);
    }
    lastVal = HIGH;
  } else {
    if (lastVal == HIGH){
      Keyboard.press(KEY_BACKSPACE);
      delay(50);
    }
    lastVal = LOW;
  }
}
 
This code is based on an tutorial on the Arduino website: https://www.arduino.cc/en/Tutorial/InputPullupSerial

What the code does is reading the signal on pin 2 and comparing it to its last value. This way we find out when the state of the signal changes. When the state changes from HIGH to LOW send the command that Backspace was pressed. When the state changes from LOW to HIGH we want the button to be released.
Now imagine to cut the wire into half and putting a push button in between and you have a complete Star Citizen controller once you put it into a box.

The Leonardo board with cable in pin 2 and the other end next to ground pin. Once it is put into ground the board immitates a pressed backspace button.