From 5b9d165cf0264795fbfd32b2f2b8b2902ab9a675 Mon Sep 17 00:00:00 2001 From: DAProgs Date: Wed, 14 Jan 2026 14:18:29 -0500 Subject: [PATCH] Upload files to "libraries/ArduinoJoystickLibrary/examples/GamepadExample" --- .../GamepadExample/GamepadExample.ino | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 libraries/ArduinoJoystickLibrary/examples/GamepadExample/GamepadExample.ino diff --git a/libraries/ArduinoJoystickLibrary/examples/GamepadExample/GamepadExample.ino b/libraries/ArduinoJoystickLibrary/examples/GamepadExample/GamepadExample.ino new file mode 100644 index 0000000..ff728b3 --- /dev/null +++ b/libraries/ArduinoJoystickLibrary/examples/GamepadExample/GamepadExample.ino @@ -0,0 +1,91 @@ +// Simple gamepad example that demonstraits how to read five Arduino +// digital pins and map them to the Arduino Joystick library. +// +// The digital pins 2 - 6 are grounded when they are pressed. +// Pin 2 = UP +// Pin 3 = RIGHT +// Pin 4 = DOWN +// Pin 5 = LEFT +// Pin 6 = FIRE +// +// NOTE: This sketch file is for use with Arduino Leonardo and +// Arduino Micro only. +// +// by Matthew Heironimus +// 2016-11-24 +//-------------------------------------------------------------------- + +#include + +Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, + 1, 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); + + // Initialize Joystick Library + Joystick.begin(); + Joystick.setXAxisRange(-1, 1); + Joystick.setYAxisRange(-1, 1); +} + +// Last state of the buttons +int lastButtonState[5] = {0,0,0,0,0}; + +void loop() { + + // Read pin values + for (int index = 0; index < 5; index++) + { + int currentButtonState = !digitalRead(index + 2); + 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: // FIRE + Joystick.setButton(0, currentButtonState); + break; + } + lastButtonState[index] = currentButtonState; + } + } + + delay(10); +} +