Advertisement
Celot

Sketch Joystick

Feb 7th, 2023
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.52 KB | Software | 0 0
  1. //Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)
  2. //Beginners Guide, by Daniel Cantore
  3. //Example Code (Oct 2020), with in-depth commenting
  4.  
  5. //Initial Definitions and Setup
  6. //Libary Inclusion
  7. #include <Joystick.h>
  8.  
  9. //Define and Allocate Input Pins to memorable names
  10. #define joyX A0
  11. #define joyY A1
  12. #define joyRZ A2
  13. //#define joyThrottle A2
  14. #define joyButton1 A3
  15. #define joyButton2 A4
  16. #define joyButton3 A5
  17. #define joyButton4 0
  18. #define joyButton5 1
  19. #define joyButton6 2
  20. #define joyButton7 3
  21. #define joyButton8 4
  22. #define joyButton9 5
  23. #define joyButton10 6
  24. #define joyButton11 7
  25. #define joyButton12 8
  26. #define joyButton13 9
  27. #define joyButton14 10
  28. #define joyButton15 11
  29. #define joyButton16 12
  30. #define joyButton17 13
  31.  
  32. //Initializing Axis as Integers, at a 0 default value
  33. int xAxis_ = 0;
  34. int yAxis_ = 0;
  35. int rzAxis_ = 0;
  36. //int throttle_ = 0;
  37.  
  38. //Setting up Buttons
  39. //Updating a static variable gives greater stability than reading directly from the digital pin.
  40. //Giving Default Values to the Buttons for later use
  41.   int lastButton1State = 0;
  42.   int lastButton2State = 0;
  43.   int lastButton3State = 0;
  44.   int lastButton4State = 0;
  45.   int lastButton5State = 0;
  46.   int lastButton6State = 0;
  47.   int lastButton7State = 0;
  48.   int lastButton8State = 0;
  49.   int lastButton9State = 0;
  50.   int lastButton10State = 0;
  51.   int lastButton11State = 0;
  52.   int lastButton12State = 0;
  53.   int lastButton13State = 0;
  54.   int lastButton14State = 0;
  55.   int lastButton15State = 0;
  56.   int lastButton16State = 0;
  57.   int lastButton17State = 0;
  58.  
  59. //Defining the Joystick
  60. //The Joystick is defined in the following setup:
  61. //Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder, Include Throttle, Include Accelerator, Include Brake, Include Steering
  62. //Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
  63. //Joystick type: Define the type of joystick from the types supported. Types: DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
  64. //Button Count: Number of Buttons shown to HID system (default: 32)
  65. //Hat Switch Count: Number of Hat Switches, max 2. (default:2)
  66. //Include X Axis: Determines whether the X axis is avalible for used by the HID system, defined as a bool value (default:true)
  67. //Include Y Axis: Determines whether the Y axis is avalible for used by the HID system, defined as a bool value (default:true)
  68. //Include Z Axis: Determines whether the Z axis is avalible for used by the HID system, defined as a bool value (default:true)
  69. //Include Rx Axis: Determines whether the X Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
  70. //Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
  71. //Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
  72. //Include Rudder: Determines whether a Rudder axis is avalible for used by the HID system, defined as a bool value (default:true)
  73. //Include Throttle: Determines whether a Throttle axis is avalible for used by the HID system, defined as a bool value (default:true)
  74. //Include Accelerator: Determines whether an Accelerator axis is avalible for used by the HID system, defined as a bool value (default:true)
  75. //Include Brake: Determines whether a Brake axis is avalible for used by the HID system, defined as a bool value (default:true)
  76. //Include Steering: Determines whether a Steering axis is avalible for used by the HID system, defined as a bool value (default:true)
  77.  
  78. Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 17, 0,true,true,false,false,false,true,false,true,false,false,false);
  79.  
  80. //Set Auto Send State
  81. //Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
  82. const bool initAutoSendState = true;
  83.  
  84. void setup() {
  85.   //Initialize Buttons
  86.   //Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
  87.   pinMode(joyButton1, INPUT_PULLUP);
  88.   pinMode(joyButton2, INPUT_PULLUP);
  89.   pinMode(joyButton3, INPUT_PULLUP);
  90.   pinMode(joyButton4, INPUT_PULLUP);
  91.   pinMode(joyButton5, INPUT_PULLUP);
  92.   pinMode(joyButton6, INPUT_PULLUP);
  93.   pinMode(joyButton7, INPUT_PULLUP);
  94.   pinMode(joyButton8, INPUT_PULLUP);
  95.   pinMode(joyButton9, INPUT_PULLUP);
  96.   pinMode(joyButton10, INPUT_PULLUP);
  97.   pinMode(joyButton11, INPUT_PULLUP);
  98.   pinMode(joyButton12, INPUT_PULLUP);
  99.   pinMode(joyButton13, INPUT_PULLUP);
  100.   pinMode(joyButton14, INPUT_PULLUP);
  101.   pinMode(joyButton15, INPUT_PULLUP);
  102.   pinMode(joyButton16, INPUT_PULLUP);
  103.   pinMode(joyButton17, INPUT_PULLUP);
  104.  
  105.   //Start Joystick - Needed to start the Joystick function libary
  106.   Joystick.begin();
  107. }
  108.  
  109. void loop() {
  110.  
  111.   //Axis Reading during Runtime
  112.   //Setting Read functions for each axis and parsing correctly. The X axis will be used as an example for explanation
  113.  
  114.   //Reading the X Axis analog pin to the xAxis_ variable for processing
  115.   xAxis_ = analogRead(joyX);
  116.   //Mapping the X Axis data from a 0-1023 to 0-255 range for a smoother action
  117.   xAxis_ = map(xAxis_,0,1023,0,255);
  118.   //Set the Joystick X Axis value as the new, smoother, value
  119.   Joystick.setXAxis(xAxis_);
  120.  
  121.   yAxis_ = analogRead(joyY);
  122.   yAxis_ = map(yAxis_,0,1023,0,255);
  123.   Joystick.setYAxis(yAxis_);
  124.  
  125.   rzAxis_ = analogRead(joyRZ);
  126.   rzAxis_ = map(rzAxis_,0,1023,0,255);
  127.   Joystick.setRzAxis(rzAxis_);
  128.  
  129. //  throttle_ = analogRead(joyThrottle);
  130. //  throttle_ = map(throttle_,0,1023,0,255);
  131. //  Joystick.setThrottle(throttle_);
  132.  
  133.   //Button Reading during Runtime
  134.   //Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation
  135.  
  136.   //Reading the current Button digital pin to the Current Button State for processing
  137.   int currentButton1State = !digitalRead(joyButton1);
  138.   //If loop - Check that the button has actually changed.
  139.   if (currentButton1State != lastButton1State){
  140.     //If the button has changed, set the specified HID button to the Current Button State
  141.     Joystick.setButton(0, currentButton1State);
  142.     //Update the Stored Button State
  143.     lastButton1State = currentButton1State;
  144.   }
  145.  
  146.     int currentButton2State = !digitalRead(joyButton2);
  147.   if (currentButton2State != lastButton2State){
  148.     Joystick.setButton(1, currentButton2State);
  149.     lastButton2State = currentButton2State;
  150.   }
  151.     int currentButton3State = !digitalRead(joyButton3);
  152.   if (currentButton3State != lastButton3State){
  153.     Joystick.setButton(2, currentButton3State);
  154.     lastButton3State = currentButton3State;
  155.   }
  156.   int currentButton4State = !digitalRead(joyButton4);
  157.   if (currentButton4State != lastButton4State){
  158.     Joystick.setButton(3, currentButton4State);
  159.     lastButton4State = currentButton4State;
  160.   }
  161.   int currentButton5State = !digitalRead(joyButton5);
  162.   if (currentButton5State != lastButton5State){
  163.     Joystick.setButton(4, currentButton5State);
  164.     lastButton5State = currentButton5State;
  165.   }
  166.   int currentButton6State = !digitalRead(joyButton6);
  167.   if (currentButton6State != lastButton6State){
  168.     Joystick.setButton(5, currentButton6State);
  169.     lastButton6State = currentButton6State;
  170.   }
  171.   int currentButton7State = !digitalRead(joyButton7);
  172.   if (currentButton7State != lastButton7State){
  173.     Joystick.setButton(6, currentButton7State);
  174.     lastButton7State = currentButton7State;
  175.   }
  176.   int currentButton8State = !digitalRead(joyButton8);
  177.   if (currentButton8State != lastButton8State){
  178.     Joystick.setButton(7, currentButton8State);
  179.     lastButton8State = currentButton8State;
  180.   }
  181.   int currentButton9State = !digitalRead(joyButton9);
  182.   if (currentButton9State != lastButton9State){
  183.     Joystick.setButton(8, currentButton9State);
  184.     lastButton9State = currentButton9State;
  185.   }
  186.   int currentButton10State = !digitalRead(joyButton10);
  187.   if (currentButton10State != lastButton10State){
  188.     Joystick.setButton(9, currentButton10State);
  189.     lastButton10State = currentButton10State;
  190.   }
  191.   int currentButton11State = !digitalRead(joyButton11);
  192.   if (currentButton11State != lastButton11State){
  193.     Joystick.setButton(10, currentButton11State);
  194.     lastButton11State = currentButton11State;
  195.   }
  196.  
  197. //Pole Delay/Debounce
  198. //To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
  199. delay(10);
  200. }
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement