Skip to content

Instantly share code, notes, and snippets.

@grze2000

grze2000/gamepad.ino

Created Jan 26, 2021
Embed
What would you like to do?
Arduino Gamepad
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 12);
const int pins[12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16};
const int btnNr[12] = {2, 0, 7, 4, 5, 6, 8, 9, 1, 10, 11, 3}; // depends on order in which the buttons are connected to Arduino
void setup() {
for(int i=0; i<sizeof(pins)/sizeof(pins[0]); i++) {
pinMode(pins[i], INPUT_PULLUP);
}
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
Joystick.begin();
}
void loop() {
Joystick.setXAxis(map(analogRead(A3), 0, 1023, 0, 255));
Joystick.setYAxis(map(analogRead(A2), 0, 1023, 0, 255));
Joystick.setZAxis(map(analogRead(A1), 0, 1023, 255, 0));
Joystick.setRzAxis(map(analogRead(A0), 0, 1023, 255, 0));
for(int i=0; i<sizeof(pins)/sizeof(pins[0]); i++) {
if(!digitalRead(pins[i])) {
Joystick.pressButton(btnNr[i]);
} else {
Joystick.releaseButton(btnNr[i]);
}
}
delay(10);
}
@grze2000

This comment has been minimized.

Copy link
Owner Author

@grze2000 grze2000 commented Feb 8, 2021

Arduino gamepad_schem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment