Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Last active July 28, 2026 15:49
Show Gist options
  • Select an option

  • Save renatocassino/ef9d4da59628cfb425a1ad0d75645959 to your computer and use it in GitHub Desktop.

Select an option

Save renatocassino/ef9d4da59628cfb425a1ad0d75645959 to your computer and use it in GitHub Desktop.
Arduino protoboard joystick
#include <Joystick.h>
const int NUM_BOTOES = 12;
const int pinos[NUM_BOTOES] = {2,3,4,5,6,7,8,9,10,11,12,13};
bool lastState[NUM_BOTOES];
unsigned long lastRead = 0;
const unsigned long INTERVAL = 10; // in ms
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_GAMEPAD,
NUM_BOTOES,
0,
false, false, false,
false, false, false,
false, false,
false, false, false
);
void setup() {
for (int i = 0; i < NUM_BOTOES; i++) {
pinMode(pinos[i], INPUT_PULLUP);
lastState[i] = false;
}
Joystick.begin();
}
void loop() {
if (millis() - lastRead >= INTERVAL) {
lastRead = millis();
for (int i = 0; i < NUM_BOTOES; i++) {
bool pressed = (digitalRead(pinos[i]) == LOW);
if (pressed != lastState[i]) {
Joystick.setButton(i, pressed ? 1 : 0);
lastState[i] = pressed;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment