Joystick to keys
Joystick to keys
What I wanted to learn, and why it interested me: I wanted to learn how to use a joystick or other forms of input to control my keyboard/computer interface.
Final outcome: The final device is a joystick, where each of the 5 (up down left right button) maps to key binds on my laptop, allowing me to play Tetris.
Images of final creative/exploratory output
Final product, with tetris gameplay in the background.
Process images from development towards creative/exploratory output
There wasn't much process, it was some very straightforward wiring, and I got the micro to work.
Testing the keybinds with the joystick on tetris.
Process and reflection:
I first thought about what I wanted to achieve, which is input from the Arduino side to my computer keys. I initially thought about buttons, but I thought connecting a joystick is much more accessible, as it requires less wires, has 5 input on its own, and is a much more fitting input for a game-like function. (thinking about gaming controllers)
I learned about how to assign key binds to inputs from the Arduino, which was surprisingly easy. There was not much hiccups along the way, and the interface from the micro is fairly straightforward. From now on, I am much more confident about working with anything that interfaces with the computer.
Technical details
Electrical Schematic: The power is supply through ther USB, which is connected to my laptop, which also provides the input for the joystick to control my keys.
/*
Description:
This sketch turns an Arduino Micro into a simple USB game
controller using a joystick module. The joystick’s X and Y axes control
the arrow keys (left, right, up, down), and pressing the joystick button
sends a spacebar input. It connects to a joystick module with analog
outputs (VRx, VRy) and a pushbutton (SW). A safety “bailout” jumper is
included so the device only sends keyboard inputs when enabled. This allows me
to simply disconnect pin 3 if computer goes feral when connected to the micro
Pin mapping:
Arduino pin | role | details
-----------------------------------------------
A0 input joystick X-axis (VRx)
A1 input joystick Y-axis (VRy)
2 input joystick button (SW, active LOW)
3 input bailout switch (connect to GND to enable)
*/
#include <Keyboard.h>
const int BAIL = 3;
const int X_PIN = A0;
const int Y_PIN = A1;
const int SW_PIN = 2; // joystick button
const int DEAD = 150;
#ifndef KEY_UP_ARROW
#define KEY_UP_ARROW 0xDA
#define KEY_DOWN_ARROW 0xD9
#define KEY_LEFT_ARROW 0xD8
#define KEY_RIGHT_ARROW 0xD7
#endif
void setup() {
pinMode(BAIL, INPUT_PULLUP);
pinMode(SW_PIN, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// safety: only send keys if bailout is connected to GND
if (digitalRead(BAIL) == HIGH) {
Keyboard.releaseAll();
return;
}
int x = analogRead(X_PIN);
int y = analogRead(Y_PIN);
// LEFT / RIGHT
if (x < 512 - DEAD) Keyboard.press(KEY_LEFT_ARROW);
else Keyboard.release(KEY_LEFT_ARROW);
if (x > 512 + DEAD) Keyboard.press(KEY_RIGHT_ARROW);
else Keyboard.release(KEY_RIGHT_ARROW);
// UP / DOWN
if (y < 512 - DEAD) Keyboard.press(KEY_UP_ARROW);
else Keyboard.release(KEY_UP_ARROW);
if (y > 512 + DEAD) Keyboard.press(KEY_DOWN_ARROW);
else Keyboard.release(KEY_DOWN_ARROW);
// SPACEBAR (joystick press)
if (digitalRead(SW_PIN) == LOW) Keyboard.press(' ');
else Keyboard.release(' ');
}