Skip to content

Commit e800604

Browse files
authored
Merge pull request MHeironimus#237 from MHeironimus/nebhead-pr
Version 2.0.8 - Community Updates 2
2 parents 35096a4 + 6160cff commit e800604

File tree

3 files changed

+178
-4
lines changed

3 files changed

+178
-4
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Arduino Joystick Library
22

3-
#### Version 2.0.7
3+
#### Version 2.0.8
44

55
This library can be used with Arduino IDE 1.6.6 (or above) to add one or more joysticks (or gamepads) to the list of HID devices an [Arduino Leonardo](https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) or [Arduino Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro) (or any Arduino clone that is based on the ATmega32u4) can support. This library will also work with the [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue), thanks to [@Palakis](https://github.com/Palakis). A complete list of supported boards can be found in the [Wiki](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Supported-Boards). This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).
66

@@ -33,9 +33,10 @@ The following example Arduino sketch files are included in this library:
3333
- `JoystickButton` - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
3434
- `JoystickKeyboard` - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
3535
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
36-
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
36+
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
3737
- `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
38-
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
38+
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
39+
- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino Pro Micro digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). NOTE: This sketch is for the Arduino Pro Micro only.
3940

4041
### Simple example
4142

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Simple arcade stick example that demonstrates how to read twelve
2+
// Arduino Pro Micro digital pins and map them to the
3+
// Arduino Joystick library.
4+
//
5+
6+
// The digital pins 2 - 20 are grounded when they are pressed.
7+
// Pin 10, A10, Red = UP
8+
// Pin 15, D15, Yellow = RIGHT
9+
// Pin 16, D16, Orange = DOWN
10+
// Pin 14, D14, Green = LEFT
11+
12+
// Pin 9, A9 = Button 1
13+
// Pin 8, A8 = Button 2
14+
// Pin 7, D7 = Button 3
15+
// Pin 3, D3 = Button 4
16+
// Pin 2, D2 = Button 5
17+
// Pin 4, A6 = Button 6
18+
19+
// Pin 20, A2 = Select Button 1
20+
// Pin 19, A1 = Start Button 2
21+
22+
// Pin 5, D5 = Other Button
23+
// Pin 6, A7 = Other Button
24+
// Pin 18, A0 = Other Button
25+
// Pin 21, A3 = Other Button
26+
27+
// NOTE: This sketch file is for use with Arduino Pro 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+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Joystick
2-
version=2.0.7
2+
version=2.0.8
33
author=Matthew Heironimus
44
maintainer=Matthew Heironimus <[email protected]>
55
sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad.

0 commit comments

Comments
 (0)