From 12cf2bbdb8910619d32ba3bf4b7d669c8e813b99 Mon Sep 17 00:00:00 2001 From: Matthew Heironimus Date: Sat, 9 Jul 2022 15:09:40 -0500 Subject: [PATCH] Added an example using a Funduino Joystick Shield --- README.md | 3 +- .../FunduinoJoystickShield.ino | 85 +++++++++++++++++++ library.properties | 2 +- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 examples/FunduinoJoystickShield/FunduinoJoystickShield.ino diff --git a/README.md b/README.md index dcafd05..7801179 100644 --- a/README.md +++ b/README.md @@ -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.). @@ -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 diff --git a/examples/FunduinoJoystickShield/FunduinoJoystickShield.ino b/examples/FunduinoJoystickShield/FunduinoJoystickShield.ino new file mode 100644 index 0000000..71c277a --- /dev/null +++ b/examples/FunduinoJoystickShield/FunduinoJoystickShield.ino @@ -0,0 +1,85 @@ +#include + +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); +} diff --git a/library.properties b/library.properties index ec5d1cc..3e3fe88 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Joystick -version=2.1.0 +version=2.1.1 author=Matthew Heironimus maintainer=Matthew Heironimus sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad.