Skip to content

Commit c0d56d0

Browse files
committed
Merge remote-tracking branch 'nebhead/master' into nebhead-pr
2 parents 35096a4 + 2263e78 commit c0d56d0

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Simple arcade stick example that demonstraits how to read twelve Arduino
2+
// digital pins and map them to the Arduino Joystick library.
3+
//
4+
5+
// The digital pins 2 - 20 are grounded when they are pressed.
6+
// Pin 10, A10, Red = UP
7+
// Pin 15, D15, Yellow = RIGHT
8+
// Pin 16, D16, Orange = DOWN
9+
// Pin 14, D14, Green = LEFT
10+
11+
// Pin 9, A9 = Button 1
12+
// Pin 8, A8 = Button 2
13+
// Pin 7, D7 = Button 3
14+
// Pin 3, D3 = Button 4
15+
// Pin 2, D2 = Button 5
16+
// Pin 4, A6 = Button 6
17+
18+
// Pin 20, A2 = Select Button 1
19+
// Pin 19, A1 = Start Button 2
20+
21+
// Pin 5, D5 = Other Button
22+
// Pin 6, A7 = Other Button
23+
// Pin 18, A0 = Other Button
24+
// Pin 21, A3 = Other Button
25+
26+
// NOTE: This sketch file is for use with Arduino Leonardo and
27+
// Arduino Micro only.
28+
//
29+
// Original gamepad example by Matthew Heironimus
30+
// 2016-11-24
31+
// Adapted for arcade machine setup by Ben Parmeter
32+
// 2019-05-20
33+
//--------------------------------------------------------------------
34+
35+
#include <Joystick.h>
36+
37+
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
38+
8, 0, // Button Count, Hat Switch Count
39+
true, true, false, // X and Y, but no Z Axis
40+
false, false, false, // No Rx, Ry, or Rz
41+
false, false, // No rudder or throttle
42+
false, false, false); // No accelerator, brake, or steering
43+
44+
void setup() {
45+
// Initialize Button Pins
46+
pinMode(2, INPUT_PULLUP);
47+
pinMode(3, INPUT_PULLUP);
48+
pinMode(4, INPUT_PULLUP);
49+
pinMode(5, INPUT_PULLUP);
50+
pinMode(6, INPUT_PULLUP);
51+
pinMode(7, INPUT_PULLUP);
52+
pinMode(8, INPUT_PULLUP);
53+
pinMode(9, INPUT_PULLUP);
54+
pinMode(10, INPUT_PULLUP);
55+
pinMode(14, INPUT_PULLUP);
56+
pinMode(15, INPUT_PULLUP);
57+
pinMode(16, INPUT_PULLUP);
58+
pinMode(18, INPUT_PULLUP);
59+
pinMode(19, INPUT_PULLUP);
60+
pinMode(20, INPUT_PULLUP);
61+
pinMode(21, INPUT_PULLUP);
62+
63+
// Initialize Joystick Library
64+
Joystick.begin();
65+
Joystick.setXAxisRange(-1, 1);
66+
Joystick.setYAxisRange(-1, 1);
67+
}
68+
69+
// Last state of the buttons
70+
int lastButtonState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
71+
int buttonMap[16] = {10,15,16,14,9,8,7,3,2,4,20,19,5,6,18,21};
72+
73+
// ButtonMap = 0, Pin 10 = UP
74+
// ButtonMap = 1, Pin 15 = RIGHT
75+
// ButtonMap = 2, Pin 16 = DOWN
76+
// ButtonMap = 3, Pin 14 = LEFT
77+
78+
// ButtonMap = 4, Pin 9 = Button 1
79+
// ButtonMap = 5, Pin 8 = Button 2
80+
// ButtonMap = 6, Pin 7 = Button 3
81+
// ButtonMap = 7, Pin 3 = Button 4
82+
// ButtonMap = 8, Pin 2 = Button 5
83+
// ButtonMap = 9, Pin 4 = Button 6
84+
85+
// ButtonMap = 10, Pin 20 = Select Button 1
86+
// ButtonMap = 11, Pin 19 = Start Button 2
87+
88+
// ButtonMap = 12, Pin 5 = Other Button
89+
// ButtonMap = 13, Pin 6 = Other Button
90+
// ButtonMap = 14, Pin 18 = Other Button
91+
// ButtonMap = 15, Pin 21 = Other Button
92+
93+
94+
void loop() {
95+
96+
// Read pin values
97+
for (int index = 0; index < 16; index++)
98+
{
99+
int currentButtonState = !digitalRead(buttonMap[index]);
100+
if (currentButtonState != lastButtonState[index])
101+
{
102+
switch (index) {
103+
case 0: // UP
104+
if (currentButtonState == 1) {
105+
Joystick.setYAxis(-1);
106+
} else {
107+
Joystick.setYAxis(0);
108+
}
109+
break;
110+
case 1: // RIGHT
111+
if (currentButtonState == 1) {
112+
Joystick.setXAxis(1);
113+
} else {
114+
Joystick.setXAxis(0);
115+
}
116+
break;
117+
case 2: // DOWN
118+
if (currentButtonState == 1) {
119+
Joystick.setYAxis(1);
120+
} else {
121+
Joystick.setYAxis(0);
122+
}
123+
break;
124+
case 3: // LEFT
125+
if (currentButtonState == 1) {
126+
Joystick.setXAxis(-1);
127+
} else {
128+
Joystick.setXAxis(0);
129+
}
130+
break;
131+
case 4: // Black Button 1
132+
Joystick.setButton(0, currentButtonState);
133+
break;
134+
case 5: // Black Button 2
135+
Joystick.setButton(1, currentButtonState);
136+
break;
137+
case 6: // Black Button 3
138+
Joystick.setButton(2, currentButtonState);
139+
break;
140+
case 7: // Black Button 4
141+
Joystick.setButton(3, currentButtonState);
142+
break;
143+
case 8: // Black Button 5
144+
Joystick.setButton(4, currentButtonState);
145+
break;
146+
case 9: // Black Button 6
147+
Joystick.setButton(5, currentButtonState);
148+
break;
149+
case 10: // Select Button
150+
Joystick.setButton(6, currentButtonState);
151+
break;
152+
case 11: // Start Button
153+
Joystick.setButton(7, currentButtonState);
154+
break;
155+
case 12: // Other Button 1
156+
Joystick.setButton(8, currentButtonState);
157+
break;
158+
case 13: // Other Button 2
159+
Joystick.setButton(9, currentButtonState);
160+
break;
161+
case 14: // Other Button 3
162+
Joystick.setButton(10, currentButtonState);
163+
break;
164+
case 15: // Other Button 4
165+
Joystick.setButton(11, currentButtonState);
166+
break;
167+
}
168+
lastButtonState[index] = currentButtonState;
169+
}
170+
}
171+
172+
delay(10);
173+
}

0 commit comments

Comments
 (0)