Advertisement
SemlerPDX

Modified HSI Knobs Sketch for Falcon BMS / DCS / FSX

Sep 6th, 2019
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Modified HSI Knobs Sketch for Falcon BMS / DCS / FSX
  2.  *  with additional 3rd Rotary Encoder (Z Axis)
  3.  *    *(coming next: a 3-way Toggle Switch add-on)
  4.  *  for Arduino Micro/Leonardo / Sparkfun Pro Micro or equiv. clones
  5.  * by SemlerPDX Aug2019
  6.  * VETERANS-GAMING.COM
  7.  * ( in response to reply at:
  8.  *    http://veterans-gaming.com/index.php?/blogs/entry/32-diy-custom-game-controller-2-dial-hsi-course-and-heading-knobs/ )
  9.  *  
  10.  *  Pins:
  11.  *  Rotary Encoder 1 - (OUTA-OUTB-SW) = Arduino Pins (0,1,15)
  12.  *  Rotary Encoder 2 - (OUTA-OUTB-SW) = Arduino Pins (2,3,6)
  13.  *  Rotary Encoder 3 - (OUTA-OUTB-SW) = Arduino Pins (9,10,7)
  14.  *  
  15.  *  Encoder Library
  16.  * http://www.pjrc.com/teensy/td_libs_Encoder.html
  17.  *
  18.  *  Joystick Library
  19.  * by Matthew Heironimus
  20.  * https://github.com/MHeironimus/ArduinoJoystickLibrary
  21.  */
  22.  
  23. #define ENCODER_USE_INTERRUPTS
  24. #define ENCODER_OPTIMIZE_INTERRUPTS
  25. #include <Encoder.h>
  26. #include <Joystick.h>
  27.  
  28. //Tell the Encoder Library which pins have encoders
  29. Encoder axisXRotation(0, 1);
  30. Encoder axisYRotation(2, 3);
  31. Encoder axisZRotation(9, 10);
  32.  
  33. //Rotary Encoder Push Button Pins
  34. int buttonArray[3] = {15, 6, 7};
  35.  
  36. //Rotary Encoder Interrupt Pins
  37. int EncoderPin0 = 0;
  38. int EncoderPin1 = 1;
  39. int EncoderPin2 = 2;
  40. int EncoderPin3 = 3;
  41. int EncoderPin4 = 9;
  42. int EncoderPin5 = 10;
  43.  
  44. //Delay Time between loops
  45. int debounceDelay = 260;
  46.  
  47. //Variables to compare current to old values
  48. int oldX = 0;
  49. int oldY = 0;
  50. int oldZ = 0;
  51. int RxAxis_Value = 1;
  52. int RyAxis_Value = 1;
  53. int RzAxis_Value = 1;
  54.  
  55. //Intervals for Jump/Warp Speed Rotations
  56. int JumpSpeed = 18;
  57. int WarpSpeed = 30;
  58.  
  59. //Set generic joystick with id 42 with 3 buttons and 3 axes
  60. Joystick_ Joystick(0x42,
  61.   0x04, 3, 0,
  62.   false, false, false, true, true, true,
  63.   false, false, false, false, false);  
  64.  
  65.  
  66. void setup() {
  67.  
  68.   //Set Encoder Pins as Pullups
  69.   pinMode(EncoderPin0, INPUT_PULLUP);
  70.   pinMode(EncoderPin1, INPUT_PULLUP);
  71.   pinMode(EncoderPin2, INPUT_PULLUP);
  72.   pinMode(EncoderPin3, INPUT_PULLUP);
  73.   pinMode(EncoderPin4, INPUT_PULLUP);
  74.   pinMode(EncoderPin5, INPUT_PULLUP);
  75.  
  76.   //Loop through buttons and set them as Pullups
  77.   for(int x = 0; x < sizeof(buttonArray); x++) {
  78.     pinMode(buttonArray[x], INPUT_PULLUP);
  79.   }
  80.  
  81.   //Set Range of custom Axes
  82.   Joystick.setRxAxisRange(0, 359);
  83.   Joystick.setRyAxisRange(0, 359);
  84.   Joystick.setRzAxisRange(0, 359);
  85.  
  86.   // Initialize Joystick Library
  87.   Joystick.begin(false);
  88.  
  89. }
  90.  
  91.  
  92. void loop() {
  93.  
  94.   // Loop through button pin values & set to Joystick
  95.   for (int x = 0; x < sizeof(buttonArray); x++) {
  96.     byte currentButtonState = !digitalRead(buttonArray[x]);
  97.     Joystick.setButton(x, currentButtonState);
  98.   }
  99.  
  100.  
  101.   // Read "Heading" X Axis Rotation Encoder Knob
  102.   int newX = axisXRotation.read();
  103.   if (newX > oldX) {
  104.     //Determine speed of increment & set output
  105.     int difX = newX - oldX;
  106.     RxAxis_Value = speedVal(difX, RxAxis_Value, 1);
  107.     Joystick.setRxAxis(RxAxis_Value);
  108.     axisXRotation.write(newX);
  109.     oldX = newX;
  110.  
  111.   }else if (newX < oldX) {
  112.     //Determine speed of decrement & set output
  113.     int difX = oldX - newX;
  114.     RxAxis_Value = speedVal(difX, RxAxis_Value, 0);
  115.     Joystick.setRxAxis(RxAxis_Value);
  116.     axisXRotation.write(newX);
  117.     oldX = newX;
  118.   }
  119.  
  120.  
  121.   // Read "Course" Y Axis Rotation Encoder Knob
  122.   int newY = axisYRotation.read();
  123.   if (newY > oldY) {
  124.     //Determine speed of increment & set output
  125.     int difY = newY - oldY;
  126.     RyAxis_Value = speedVal(difY, RyAxis_Value, 1);
  127.     Joystick.setRyAxis(RyAxis_Value);
  128.     axisYRotation.write(newY);
  129.     oldY = newY;
  130.  
  131.   }else if (newY < oldY) {
  132.     //Determine speed of decrement & set output
  133.     int difY = oldY - newY;
  134.     RyAxis_Value = speedVal(difY, RyAxis_Value, 0);
  135.     Joystick.setRyAxis(RyAxis_Value);
  136.     axisYRotation.write(newY);
  137.     oldY = newY;
  138.   }
  139.  
  140.  
  141.   // Read "QNH" Z Axis Rotation Encoder Knob
  142.   int newZ = axisZRotation.read();
  143.   if (newZ > oldZ) {
  144.     //Determine speed of increment & set output
  145.     int difZ = newZ - oldZ;
  146.     RzAxis_Value = speedVal(difZ, RzAxis_Value, 1);
  147.     Joystick.setRzAxis(RzAxis_Value);
  148.     axisZRotation.write(newZ);
  149.     oldZ = newZ;
  150.  
  151.   }else if (newZ < oldZ) {
  152.     //Determine speed of decrement & set output
  153.     int difZ = oldZ - newZ;
  154.     RzAxis_Value = speedVal(difZ, RzAxis_Value, 0);
  155.     Joystick.setRzAxis(RzAxis_Value);
  156.     axisZRotation.write(newZ);
  157.     oldZ = newZ;
  158.   }
  159.  
  160.  
  161.   //Send Joystick info through USB
  162.   Joystick.sendState();
  163.   delay(debounceDelay);
  164. }
  165.  
  166.  
  167. //Function to set Rotation value adjusted for the turning speed
  168. int speedVal(int dif, int val, int dir){
  169.   if (dif >= WarpSpeed) {
  170.     if (dir == 1) {
  171.       val = val + WarpSpeed;
  172.     }else{
  173.       val = val - WarpSpeed;
  174.     }
  175.   }else if (dif >= JumpSpeed) {
  176.     if (dir == 1) {
  177.       val = val + JumpSpeed;
  178.     }else{
  179.       val = val - JumpSpeed;
  180.     }
  181.   }else{
  182.     if (dir == 1) {
  183.       val = val + 1;
  184.     }else{
  185.       val = val - 1;
  186.     }
  187.   }
  188.   //Correct Rotation within 360 deg.
  189.   if (val < 0) {
  190.     val = val + 360;
  191.   }else if (val >= 360) {        
  192.     val = val - 360;
  193.   }
  194.   return val;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement