Guest User

Untitled

a guest
Aug 1st, 2026
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // Simple gamepad example that demonstraits how to read five Arduino
  2. // digital pins and map them to the Arduino Joystick library.
  3. //
  4. // The digital pins 2 - 6 are grounded when they are pressed.
  5. // Pin 2 = UP
  6. // Pin 3 = RIGHT
  7. // Pin 4 = DOWN
  8. // Pin 5 = LEFT
  9. // Pin 6 = FIRE
  10. //
  11. // NOTE: This sketch file is for use with Arduino Leonardo and
  12. // Arduino Micro only.
  13. //
  14. // by Matthew Heironimus
  15. // 2016-11-24
  16. //--------------------------------------------------------------------
  17.  
  18. #include <Joystick_ESP32S2.h>
  19. const float joystickCenter = 2047.5;
  20. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  21. 1, 0, // Button Count, Hat Switch Count
  22. true, true, false, // X and Y, but no Z Axis
  23. false, false, false, // No Rx, Ry, or Rz
  24. false, false, // No rudder or throttle
  25. false, false, false); // No accelerator, brake, or steering
  26.  
  27. float analogReadingToAxis(int AnalogValue)
  28. {
  29. return (4095 - (4095 / 2)) / 2047.5;
  30. }
  31.  
  32. void setup() {
  33.  
  34. // Initialize Joystick Library
  35. Joystick.begin();
  36. Joystick.setXAxisRange(-1, 1);
  37. Joystick.setYAxisRange(-1, 1);
  38. }
  39.  
  40. void loop() {
  41.  
  42. Joystick.setXAxis( analogReadingToAxis( analogRead(2) ) );
  43. Joystick.setYAxis( analogReadingToAxis( analogRead(3) ) );
  44.  
  45. delay(10);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment