Shoyun

Throttle Quadrant Code

Aug 17th, 2020
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. // LIBRARY HERE: https://github.com/MHeironimus/ArduinoJoystickLibrary
  2. // Code by Shoyun, 2020-08-17
  3.  
  4. #include <Joystick.h>
  5.  
  6. // Create the Joystick
  7. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  8.   JOYSTICK_TYPE_MULTI_AXIS, 4, 0,
  9.   false, false, true, false, false, false,
  10.   false, true, false, false, false);
  11.  
  12. // Constant that maps the physical pin to the joystick button.
  13. // Pin 2 = Button 1
  14. // Pin 3 = Button 2
  15. // Pin 4 = Button 3
  16. // Pin 5 = Button 4
  17.  
  18. // Last state of the buttons
  19. int lastButtonState[4] = {0,0,0,0};
  20.  
  21. void setup() {
  22.  
  23.   Joystick.setThrottleRange(0, 1023);
  24.   Joystick.setZAxisRange(0, 1023);
  25.  
  26.   // Initialize Button Pins
  27.   pinMode(2, INPUT_PULLUP);
  28.   pinMode(3, INPUT_PULLUP);
  29.   pinMode(4, INPUT_PULLUP);
  30.   pinMode(5, INPUT_PULLUP);
  31.  
  32.   // Initialize Joystick Library
  33.   Joystick.begin();
  34. }
  35.  
  36.  
  37. void loop() {
  38.  
  39.   // Read pin values
  40.   for (int index = 0; index < 4; index++)
  41.   {
  42.     int currentButtonState = !digitalRead(index + 2);
  43.     if (currentButtonState != lastButtonState[index])
  44.       {
  45.         Joystick.setButton(index, currentButtonState);
  46.         lastButtonState[index] = currentButtonState;
  47.       }
  48.       lastButtonState[index] = currentButtonState;
  49.   }
  50.  
  51.   Joystick.setThrottle(analogRead(A0));
  52.   Joystick.setZAxis(analogRead(A1));
  53.  
  54.   delay(50);
  55. }
Add Comment
Please, Sign In to add comment