Skip to content

Commit 12cf2bb

Browse files
committed
Added an example using a Funduino Joystick Shield
1 parent dabdcd4 commit 12cf2bb

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

README.md

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

3-
#### Version 2.1.0
3+
#### Version 2.1.1
44

55
This library can be used with Arduino IDE 1.6.6 or above (see [Wiki - Testing Details](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Testing-Details) for more information) 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 - Supported Boards](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

@@ -71,6 +71,7 @@ The following example Arduino sketch files are included in this library:
7171
- `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.
7272
- `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.
7373
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
74+
- `FunduinoJoystickShield` - Creates a simple Gamepad using a Funduino Joystick Shield (https://protosupplies.com/product/funduino-joystick-shield-v1-a/).
7475
- `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.
7576

7677
#### Used for Testing
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <Joystick.h>
2+
3+
const uint8_t buttonCount = 7;
4+
Joystick_ controller(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, buttonCount,
5+
0, true, true, false,
6+
false, false, false,
7+
false, false, false,
8+
false, false);
9+
10+
int const BTN_A_PIN = 2;
11+
int const BTN_B_PIN = 3;
12+
int const BTN_C_PIN = 4;
13+
int const BTN_D_PIN = 5;
14+
int const BTN_E_PIN = 6;
15+
int const BTN_F_PIN = 7;
16+
int const BTN_K_PIN = 8;
17+
int const AXIS_X_PIN = A0;
18+
int const AXIS_Y_PIN = A1;
19+
20+
int const buttonPins[buttonCount] = {
21+
BTN_A_PIN,
22+
BTN_B_PIN,
23+
BTN_C_PIN,
24+
BTN_D_PIN,
25+
BTN_E_PIN,
26+
BTN_F_PIN,
27+
BTN_K_PIN
28+
};
29+
int lastButtonValue[buttonCount];
30+
int lastXAxisValue = -1;
31+
int lastYAxisValue = -1;
32+
33+
void setup()
34+
{
35+
controller.setYAxisRange(0, 1023);
36+
controller.setYAxisRange(1023, 0);
37+
controller.begin(false);
38+
39+
for (int i = 0; i < buttonCount; i++)
40+
{
41+
pinMode(buttonPins[i], INPUT_PULLUP);
42+
lastButtonValue[i] = -1;
43+
}
44+
45+
pinMode(LED_BUILTIN, OUTPUT);
46+
digitalWrite(LED_BUILTIN, LOW);
47+
}
48+
49+
void loop()
50+
{
51+
bool sendUpdate = false;
52+
for (int i = 0; i < buttonCount; i++)
53+
{
54+
const int buttonValue = digitalRead(buttonPins[i]);
55+
56+
if (buttonValue != lastButtonValue[i])
57+
{
58+
controller.setButton(i, !buttonValue);
59+
lastButtonValue[i] = buttonValue;
60+
sendUpdate = true;
61+
}
62+
}
63+
64+
const int currentXAxisValue = analogRead(AXIS_X_PIN);
65+
if (currentXAxisValue != lastXAxisValue)
66+
{
67+
controller.setXAxis(currentXAxisValue);
68+
lastXAxisValue = currentXAxisValue;
69+
sendUpdate = true;
70+
}
71+
72+
const int currentYAxisValue = analogRead(AXIS_Y_PIN);
73+
if (currentYAxisValue != lastYAxisValue)
74+
{
75+
controller.setYAxis(currentYAxisValue);
76+
lastYAxisValue = currentYAxisValue;
77+
sendUpdate = true;
78+
}
79+
80+
if (sendUpdate)
81+
{
82+
controller.sendState();
83+
}
84+
delay(50);
85+
}

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.1.0
2+
version=2.1.1
33
author=Matthew Heironimus
44
maintainer=Matthew Heironimus <heironimus@live.com>
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)