Advertisement
SemlerPDX

Game Controller - Rotary Encoders HSI Course & Heading Knobs

Jun 5th, 2019 (edited)
2,143
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.63 KB | Gaming | 0 0
  1. /* Simple HSI Knobs Sketch for Falcon BMS / DCS / FSX
  2.  *  for Arduino Micro/Leonardo / Sparkfun Pro Micro or equiv. clones
  3.  * by SemlerPDX June2019
  4.  * VETERANS-GAMING.COM
  5.  * ( https://veterans-gaming.com/blogs/entry/32-diy-custom-game-controller-2-dial-hsi-course-and-heading-knobs/ )
  6.  *
  7.  *  Pins:
  8.  *  Rotary Encoder 1 - (OUTA-OUTB-SW) = Arduino Pins (0,1,15)
  9.  *  Rotary Encoder 2 - (OUTA-OUTB-SW) = Arduino Pins (2,3,6)
  10.  *
  11.  *  Encoder Library
  12.  * https://www.pjrc.com/teensy/td_libs_Encoder.html
  13.  *
  14.  *  Joystick Library
  15.  * by Matthew Heironimus
  16.  * https://github.com/MHeironimus/ArduinoJoystickLibrary
  17.  */
  18.  
  19. #define ENCODER_USE_INTERRUPTS
  20. #define ENCODER_OPTIMIZE_INTERRUPTS
  21. #include <Encoder.h>
  22. #include <Joystick.h>
  23.  
  24. // Tell the Encoder Library which pins have encoders
  25. Encoder axisXRotation(0, 1);
  26. Encoder axisYRotation(2, 3);
  27.  
  28. // Rotary Encoder Push Button Pins
  29. int buttonArray[2] = {15, 6};
  30. const int numButtons = sizeof(buttonArray) / sizeof(buttonArray[0]);
  31.  
  32. // Rotary Encoder Interrupt Pins
  33. int EncoderPin0 = 0;
  34. int EncoderPin1 = 1;
  35. int EncoderPin2 = 2;
  36. int EncoderPin3 = 3;
  37.  
  38. // Delay Time between loops
  39. int debounceDelay = 260;
  40.  
  41. // Variables to compare current to old values
  42. int oldX = 0;
  43. int oldY = 0;
  44. int RxAxis_Value = 1;
  45. int RyAxis_Value = 1;
  46.  
  47. // Intervals for Jump/Warp Speed Rotations
  48. int JumpSpeed = 18;
  49. int WarpSpeed = 30;
  50.  
  51. // Set generic joystick using id 42 with 2 buttons and 2 axes
  52. Joystick_ Joystick(0x42,
  53.   0x04, 2, 0,
  54.   false, false, false, true, true, false,
  55.   false, false, false, false, false);  
  56.  
  57.  
  58. void setup() {
  59.  
  60.   // Set Encoder Pins as Pullups
  61.   pinMode(EncoderPin0, INPUT_PULLUP);
  62.   pinMode(EncoderPin1, INPUT_PULLUP);
  63.   pinMode(EncoderPin2, INPUT_PULLUP);
  64.   pinMode(EncoderPin3, INPUT_PULLUP);
  65.  
  66.   // Loop through buttons and set them as Pullups
  67.   for (int x = 0; x < numButtons; x++) {
  68.     pinMode(buttonArray[x], INPUT_PULLUP);
  69.   }
  70.  
  71.   // Set Range of custom Axes
  72.   Joystick.setRxAxisRange(0, 359);
  73.   Joystick.setRyAxisRange(0, 359);
  74.  
  75.   // Initialize Joystick Library
  76.   Joystick.begin(false);
  77.  
  78. }
  79.  
  80.  
  81. void loop() {
  82.  
  83.   // Loop through button pin values & set to Joystick
  84.   for (int x = 0; x < numButtons; x++) {
  85.     byte currentButtonState = !digitalRead(buttonArray[x]);
  86.     Joystick.setButton(x, currentButtonState);
  87.   }
  88.  
  89.   // Read "Heading" X Axis Rotation Encoder Knob
  90.   int newX = axisXRotation.read();
  91.   if (newX != oldX) {
  92.     RxAxis_Value = getAxisVal(newX, oldX, RxAxis_Value);
  93.     Joystick.setRxAxis(RxAxis_Value);
  94.     axisXRotation.write(newX);
  95.     oldX = newX;
  96.   }
  97.  
  98.   // Read "Course" Y Axis Rotation Encoder Knob
  99.   int newY = axisYRotation.read();
  100.   if (newY != oldY) {
  101.     RyAxis_Value = getAxisVal(newY, oldY, RyAxis_Value);
  102.     Joystick.setRyAxis(RyAxis_Value);
  103.     axisYRotation.write(newY);
  104.     oldY = newY;
  105.   }
  106.  
  107.   // Send Joystick info through USB
  108.   Joystick.sendState();
  109.   delay(debounceDelay);
  110. }
  111.  
  112.  
  113. // Function to return Rotation Value adjusted for the turning speed
  114. int speedVal(int val) {
  115.   if (val >= WarpSpeed) {
  116.     val = WarpSpeed;
  117.   } else if (val >= JumpSpeed) {
  118.     val = JumpSpeed;
  119.   } else {
  120.     val = 1;
  121.   }
  122.  
  123.   return val;
  124. }
  125.  
  126. // Function to return modified Rotation Axis Value
  127. int getAxisVal(int newVal, int oldVal, int val) {
  128.   if (newVal > oldVal) {
  129.     // Determine speed of increment & set output
  130.     newVal = newVal - oldVal;
  131.     val = val + speedVal(newVal);
  132.   } else if (newVal < oldVal) {
  133.     // Determine speed of decrement & set output
  134.     newVal = oldVal - newVal;
  135.     val = val - speedVal(newVal);
  136.   }
  137.  
  138.   // Correct Rotation Value within 360 degrees
  139.   val = (val % 360 + 360) % 360;
  140.   return val;
  141. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement