Game Controller Assignment (HID USB)

For our first assignment for Tangible Interaction involved creating a controller for Lunar Lander. I decided to mimic the arcade control layout of similar games like Asteroids. Each action has a dedicated button and both hands were used. I tried both versions of Lunar Lander and felt the Atari remaster was more enjoyable for me. I thought it also would be fun to be able to switch between mouse and keyboard input.

In space you have to be resourceful.

This would have crashed Elon Musk’s rocket. There’s an Circuit Playground express connected to a breadboard as a hub and then plenty of buttons.

This is the controller layout with dual function buttons.

Relatively straightforward schematic. There are 6 momentary buttons.

To accommodate having a controller switch between keyboard and mouse output, I created a dedicated button controlled by the players foot. This allowed the player to start the game which required the mouse input and then to immediately jump into ship mode by releasing their foot.

SOURCE CODE

#include <Keyboard.h>
#include <Mouse.h>

//Button to Pin assignments
const int upButton = A0;
const int downButton = A1;
const int leftButton = A2;
const int rightButton = A3;
const int mouseButton = A4;
const int altButton = A5;
//Modifier button
int altSwitch = 0;
int prevSwitch = 0;

int range = 5;
int responseDelay = 10;

void setup() {
pinMode(upButton, INPUT_PULLUP); //Arduino reads pin 3 as input
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(mouseButton, INPUT_PULLUP);
pinMode(altButton, INPUT_PULLUP);

Serial.begin(9600);
Keyboard.begin();
Mouse.begin();
}

void loop() {

if (digitalRead(altButton) == HIGH){
int rightState = digitalRead(upButton);
int leftState = digitalRead(downButton);
int upState = digitalRead(rightButton);
int downState = digitalRead(leftButton);
int clickState = digitalRead(mouseButton);

// calculate the movement distance based on the button states:
int xDistance = (leftState – rightState) * range;
int yDistance = (upState – downState) * range;

// if X or Y is non-zero, move:
if ((xDistance != 0) || (yDistance != 0)) {
Mouse.move(xDistance, yDistance, 0);
}

// if the mouse button is pressed:
if (clickState == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}

delay(responseDelay);
} //end if statement (altSwitch == 0){

if (digitalRead(altButton) == LOW){
if (digitalRead(upButton) == HIGH) {
Keyboard.write(‘a’);
}
if (digitalRead(downButton) == HIGH) {
Keyboard.write(‘d’);
}
if (digitalRead(leftButton) == HIGH) {
Keyboard.write(‘w’);
}
if (digitalRead(rightButton) == HIGH) {
Keyboard.write(‘s’);
}
if (digitalRead(mouseButton) == HIGH) {
Keyboard.write(32);
}
}//end if altswitch = 1;
}

In my code I referenced and modified code available from:
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

https://www.arduino.cc/reference/en/language/functions/usb/keyboard/keyboardmodifiers/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.