Instantly share code, notes, and snippets.
grze2000 / gamepad.ino
Created 3 years ago
Code Revisions 1 Stars 4 Embed <script src="https://gi Download ZIP
Arduino Gamepad
gamepad.ino
1 #include <Joystick.h>
2
3 Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 12);
4
5 const int pins[12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16};
6 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
7
8 void setup() {
9 for(int i=0; i<sizeof(pins)/sizeof(pins[0]); i++) {
10 pinMode(pins[i], INPUT_PULLUP);
11 }
12 pinMode(A0, INPUT);
13 pinMode(A1, INPUT);
14 pinMode(A2, INPUT);
15 pinMode(A3, INPUT);
16
17 Joystick.begin();
18 }
19
20 void loop() {
21 Joystick.setXAxis(map(analogRead(A3), 0, 1023, 0, 255));
22 Joystick.setYAxis(map(analogRead(A2), 0, 1023, 0, 255));
23 Joystick.setZAxis(map(analogRead(A1), 0, 1023, 255, 0));
24 Joystick.setRzAxis(map(analogRead(A0), 0, 1023, 255, 0));
25 for(int i=0; i<sizeof(pins)/sizeof(pins[0]); i++) {
26 if(!digitalRead(pins[i])) {
27 Joystick.pressButton(btnNr[i]);
28 } else {
29 Joystick.releaseButton(btnNr[i]);
30 }
31 }
32 delay(10);
33 }
grze2000 commented on Feb 8, 2021 Author
Certified404Boi commented on Oct 27, 2023
Certified404Boi commented on Oct 27, 2023
Certified404Boi commented on Oct 27, 2023
Certified404Boi commented on Oct 27, 2023
#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
Author
grze2000 commented on Feb 8, 2021
Arduino gamepad_schem
SandulRanuga commented on Jan 28
what code do you use for the buttons?