From 2263e785853077b965f86cb6ac096e8d8c8b6f31 Mon Sep 17 00:00:00 2001 From: Ben Parmeter Date: Sun, 9 Jun 2019 11:10:16 -0700 Subject: [PATCH 1/3] Arcade Joystick Example Added an Arcade joystick example using a Sanwa JLF-TP-8YT-SK ball top stick, HAPP style arcade buttons with Arduino Pro Micro. --- .../ArcadeStickExample/ArcadeStickExample.ino | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino diff --git a/Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino b/Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino new file mode 100644 index 0000000..eb400b9 --- /dev/null +++ b/Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino @@ -0,0 +1,173 @@ +// Simple arcade stick example that demonstraits how to read twelve Arduino +// digital pins and map them to the Arduino Joystick library. +// + +// The digital pins 2 - 20 are grounded when they are pressed. +// Pin 10, A10, Red = UP +// Pin 15, D15, Yellow = RIGHT +// Pin 16, D16, Orange = DOWN +// Pin 14, D14, Green = LEFT + +// Pin 9, A9 = Button 1 +// Pin 8, A8 = Button 2 +// Pin 7, D7 = Button 3 +// Pin 3, D3 = Button 4 +// Pin 2, D2 = Button 5 +// Pin 4, A6 = Button 6 + +// Pin 20, A2 = Select Button 1 +// Pin 19, A1 = Start Button 2 + +// Pin 5, D5 = Other Button +// Pin 6, A7 = Other Button +// Pin 18, A0 = Other Button +// Pin 21, A3 = Other Button + +// NOTE: This sketch file is for use with Arduino Leonardo and +// Arduino Micro only. +// +// Original gamepad example by Matthew Heironimus +// 2016-11-24 +// Adapted for arcade machine setup by Ben Parmeter +// 2019-05-20 +//-------------------------------------------------------------------- + +#include + +Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, + 8, 0, // Button Count, Hat Switch Count + true, true, false, // X and Y, but no Z Axis + false, false, false, // No Rx, Ry, or Rz + false, false, // No rudder or throttle + false, false, false); // No accelerator, brake, or steering + +void setup() { + // Initialize Button Pins + pinMode(2, INPUT_PULLUP); + pinMode(3, INPUT_PULLUP); + pinMode(4, INPUT_PULLUP); + pinMode(5, INPUT_PULLUP); + pinMode(6, INPUT_PULLUP); + pinMode(7, INPUT_PULLUP); + pinMode(8, INPUT_PULLUP); + pinMode(9, INPUT_PULLUP); + pinMode(10, INPUT_PULLUP); + pinMode(14, INPUT_PULLUP); + pinMode(15, INPUT_PULLUP); + pinMode(16, INPUT_PULLUP); + pinMode(18, INPUT_PULLUP); + pinMode(19, INPUT_PULLUP); + pinMode(20, INPUT_PULLUP); + pinMode(21, INPUT_PULLUP); + + // Initialize Joystick Library + Joystick.begin(); + Joystick.setXAxisRange(-1, 1); + Joystick.setYAxisRange(-1, 1); +} + +// Last state of the buttons +int lastButtonState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; +int buttonMap[16] = {10,15,16,14,9,8,7,3,2,4,20,19,5,6,18,21}; + +// ButtonMap = 0, Pin 10 = UP +// ButtonMap = 1, Pin 15 = RIGHT +// ButtonMap = 2, Pin 16 = DOWN +// ButtonMap = 3, Pin 14 = LEFT + +// ButtonMap = 4, Pin 9 = Button 1 +// ButtonMap = 5, Pin 8 = Button 2 +// ButtonMap = 6, Pin 7 = Button 3 +// ButtonMap = 7, Pin 3 = Button 4 +// ButtonMap = 8, Pin 2 = Button 5 +// ButtonMap = 9, Pin 4 = Button 6 + +// ButtonMap = 10, Pin 20 = Select Button 1 +// ButtonMap = 11, Pin 19 = Start Button 2 + +// ButtonMap = 12, Pin 5 = Other Button +// ButtonMap = 13, Pin 6 = Other Button +// ButtonMap = 14, Pin 18 = Other Button +// ButtonMap = 15, Pin 21 = Other Button + + +void loop() { + + // Read pin values + for (int index = 0; index < 16; index++) + { + int currentButtonState = !digitalRead(buttonMap[index]); + if (currentButtonState != lastButtonState[index]) + { + switch (index) { + case 0: // UP + if (currentButtonState == 1) { + Joystick.setYAxis(-1); + } else { + Joystick.setYAxis(0); + } + break; + case 1: // RIGHT + if (currentButtonState == 1) { + Joystick.setXAxis(1); + } else { + Joystick.setXAxis(0); + } + break; + case 2: // DOWN + if (currentButtonState == 1) { + Joystick.setYAxis(1); + } else { + Joystick.setYAxis(0); + } + break; + case 3: // LEFT + if (currentButtonState == 1) { + Joystick.setXAxis(-1); + } else { + Joystick.setXAxis(0); + } + break; + case 4: // Black Button 1 + Joystick.setButton(0, currentButtonState); + break; + case 5: // Black Button 2 + Joystick.setButton(1, currentButtonState); + break; + case 6: // Black Button 3 + Joystick.setButton(2, currentButtonState); + break; + case 7: // Black Button 4 + Joystick.setButton(3, currentButtonState); + break; + case 8: // Black Button 5 + Joystick.setButton(4, currentButtonState); + break; + case 9: // Black Button 6 + Joystick.setButton(5, currentButtonState); + break; + case 10: // Select Button + Joystick.setButton(6, currentButtonState); + break; + case 11: // Start Button + Joystick.setButton(7, currentButtonState); + break; + case 12: // Other Button 1 + Joystick.setButton(8, currentButtonState); + break; + case 13: // Other Button 2 + Joystick.setButton(9, currentButtonState); + break; + case 14: // Other Button 3 + Joystick.setButton(10, currentButtonState); + break; + case 15: // Other Button 4 + Joystick.setButton(11, currentButtonState); + break; + } + lastButtonState[index] = currentButtonState; + } + } + + delay(10); +} From e7e04b696ae7e52f4db850a4104f1e2a606eb7e6 Mon Sep 17 00:00:00 2001 From: MHeironimus Date: Mon, 28 Mar 2022 11:34:22 -0500 Subject: [PATCH 2/3] Added ArcadeStickExample to list of included examples --- README.md | 7 ++++--- .../ArcadeStickExample/ArcadeStickExample.ino | 2 +- library.properties | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) rename {Joystick/examples => examples}/ArcadeStickExample/ArcadeStickExample.ino (95%) diff --git a/README.md b/README.md index 3fd94e9..fbba4a4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Arduino Joystick Library -#### Version 2.0.7 +#### Version 2.0.8 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.). @@ -33,9 +33,10 @@ 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. -- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded. +- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded. - `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded. -- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position. +- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position. +- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). ### Simple example diff --git a/Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino b/examples/ArcadeStickExample/ArcadeStickExample.ino similarity index 95% rename from Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino rename to examples/ArcadeStickExample/ArcadeStickExample.ino index eb400b9..25384ea 100644 --- a/Joystick/examples/ArcadeStickExample/ArcadeStickExample.ino +++ b/examples/ArcadeStickExample/ArcadeStickExample.ino @@ -1,4 +1,4 @@ -// Simple arcade stick example that demonstraits how to read twelve Arduino +// Simple arcade stick example that demonstrates how to read twelve Arduino // digital pins and map them to the Arduino Joystick library. // diff --git a/library.properties b/library.properties index f93ee1d..ce63f52 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Joystick -version=2.0.7 +version=2.0.8 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. From 6160cff132455d963afa632d5a342323217868f6 Mon Sep 17 00:00:00 2001 From: MHeironimus Date: Mon, 28 Mar 2022 11:59:16 -0500 Subject: [PATCH 3/3] Indicate that this ArcadeStickExample is for the Arduino Pro Micro only --- README.md | 2 +- examples/ArcadeStickExample/ArcadeStickExample.ino | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fbba4a4..10e5d8e 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The following example Arduino sketch files are included in this library: - `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded. - `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded. - `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position. -- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). +- `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. ### Simple example diff --git a/examples/ArcadeStickExample/ArcadeStickExample.ino b/examples/ArcadeStickExample/ArcadeStickExample.ino index 25384ea..1f9df66 100644 --- a/examples/ArcadeStickExample/ArcadeStickExample.ino +++ b/examples/ArcadeStickExample/ArcadeStickExample.ino @@ -1,5 +1,6 @@ -// Simple arcade stick example that demonstrates how to read twelve Arduino -// digital pins and map them to the Arduino Joystick library. +// Simple arcade stick example that demonstrates how to read twelve +// Arduino Pro Micro digital pins and map them to the +// Arduino Joystick library. // // The digital pins 2 - 20 are grounded when they are pressed. @@ -23,8 +24,7 @@ // Pin 18, A0 = Other Button // Pin 21, A3 = Other Button -// NOTE: This sketch file is for use with Arduino Leonardo and -// Arduino Micro only. +// NOTE: This sketch file is for use with Arduino Pro Micro only. // // Original gamepad example by Matthew Heironimus // 2016-11-24