Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created October 17, 2023 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennanMKE/b8912aab6cc3e8377fc9457987ea5151 to your computer and use it in GitHub Desktop.
Save brennanMKE/b8912aab6cc3e8377fc9457987ea5151 to your computer and use it in GitHub Desktop.
Pro Micro project that uses an arcade joystick as arrow keys.
// Joystick Keyboard
// This project is for a digital joystick (SANWA JLF-TP-8YT)
// connected to a Pro Micro board.
#include "Keyboard.h"
const int pinUp = 2; // Orange
const int pinDown = 3; // Red
const int pinLeft = 4; // Yellow
const int pinRight = 5; // Green
// Ground is Black
const int keyboardLongDelay = 200;
const int keyboardShortDelay = 50;
const int pollingDelay = 50;
bool wasPressed = false;
void setup() {
pinMode(pinUp, INPUT_PULLUP);
pinMode(pinDown, INPUT_PULLUP);
pinMode(pinLeft, INPUT_PULLUP);
pinMode(pinRight, INPUT_PULLUP);
Keyboard.begin();
Serial.begin(9600); //This pipes to the serial monitor
Serial.println("Initialize Joystick arrow keys on Pro Micro");
}
void loop() {
bool didPress = false;
if (digitalRead(pinUp) == LOW && digitalRead(pinLeft) == LOW) {
Serial.println("Arrow UP + LEFT");
Keyboard.press(KEY_UP_ARROW);
Keyboard.press(KEY_LEFT_ARROW);
didPress = true;
} else if (digitalRead(pinUp) == LOW && digitalRead(pinRight) == LOW) {
Serial.println("Arrow UP + RIGHT");
Keyboard.press(KEY_UP_ARROW);
Keyboard.press(KEY_RIGHT_ARROW);
didPress = true;
} else if (digitalRead(pinDown) == LOW && digitalRead(pinLeft) == LOW) {
Serial.println("Arrow DOWN + LEFT");
Keyboard.press(KEY_DOWN_ARROW);
Keyboard.press(KEY_LEFT_ARROW);
didPress = true;
} else if (digitalRead(pinDown) == LOW && digitalRead(pinRight) == LOW) {
Serial.println("Arrow DOWN + RIGHT");
Keyboard.press(KEY_DOWN_ARROW);
Keyboard.press(KEY_RIGHT_ARROW);
didPress = true;
} else if (digitalRead(pinUp) == LOW) {
Keyboard.press(KEY_UP_ARROW);
Serial.println("Arrow UP");
didPress = true;
} else if (digitalRead(pinDown) == LOW) {
Keyboard.press(KEY_DOWN_ARROW);
Serial.println("Arrow DOWN");
didPress = true;
} else if (digitalRead(pinLeft) == LOW) {
Keyboard.press(KEY_LEFT_ARROW);
Serial.println("Arrow LEFT");
didPress = true;
} else if (digitalRead(pinRight) == LOW) {
Keyboard.press(KEY_RIGHT_ARROW);
Serial.println("Arrow RIGHT");
didPress = true;
}
if (didPress) {
if (wasPressed) {
delay(keyboardShortDelay);
} else {
delay(keyboardLongDelay);
}
Keyboard.releaseAll();
wasPressed = true;
} else {
wasPressed = false;
delay(pollingDelay); // Optional delay to reduce polling rate
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment