Skip to content

Commit

Permalink
Added an example using a Funduino Joystick Shield
Browse files Browse the repository at this point in the history
  • Loading branch information
MHeironimus committed Jul 9, 2022
1 parent dabdcd4 commit 12cf2bb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Arduino Joystick Library

#### Version 2.1.0
#### Version 2.1.1

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.).

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

#### Used for Testing
Expand Down
85 changes: 85 additions & 0 deletions examples/FunduinoJoystickShield/FunduinoJoystickShield.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <Joystick.h>

const uint8_t buttonCount = 7;
Joystick_ controller(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, buttonCount,
0, true, true, false,
false, false, false,
false, false, false,
false, false);

int const BTN_A_PIN = 2;
int const BTN_B_PIN = 3;
int const BTN_C_PIN = 4;
int const BTN_D_PIN = 5;
int const BTN_E_PIN = 6;
int const BTN_F_PIN = 7;
int const BTN_K_PIN = 8;
int const AXIS_X_PIN = A0;
int const AXIS_Y_PIN = A1;

int const buttonPins[buttonCount] = {
BTN_A_PIN,
BTN_B_PIN,
BTN_C_PIN,
BTN_D_PIN,
BTN_E_PIN,
BTN_F_PIN,
BTN_K_PIN
};
int lastButtonValue[buttonCount];
int lastXAxisValue = -1;
int lastYAxisValue = -1;

void setup()
{
controller.setYAxisRange(0, 1023);
controller.setYAxisRange(1023, 0);
controller.begin(false);

for (int i = 0; i < buttonCount; i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
lastButtonValue[i] = -1;
}

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
bool sendUpdate = false;
for (int i = 0; i < buttonCount; i++)
{
const int buttonValue = digitalRead(buttonPins[i]);

if (buttonValue != lastButtonValue[i])
{
controller.setButton(i, !buttonValue);
lastButtonValue[i] = buttonValue;
sendUpdate = true;
}
}

const int currentXAxisValue = analogRead(AXIS_X_PIN);
if (currentXAxisValue != lastXAxisValue)
{
controller.setXAxis(currentXAxisValue);
lastXAxisValue = currentXAxisValue;
sendUpdate = true;
}

const int currentYAxisValue = analogRead(AXIS_Y_PIN);
if (currentYAxisValue != lastYAxisValue)
{
controller.setYAxis(currentYAxisValue);
lastYAxisValue = currentYAxisValue;
sendUpdate = true;
}

if (sendUpdate)
{
controller.sendState();
}
delay(50);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Joystick
version=2.1.0
version=2.1.1
author=Matthew Heironimus
maintainer=Matthew Heironimus <[email protected]>
sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad.
Expand Down

0 comments on commit 12cf2bb

Please sign in to comment.