View difference between Paste ID: gEWNgm83 and drUnCfKN
SHOW: | | - or go back to the newest paste.
1-
/* Simple HSI Knobs Sketch for Falcon BMS / DCS / FSX
1+
/* Modified HSI Knobs Sketch for Falcon BMS / DCS / FSX
2
 *  with additional 3rd Rotary Encoder (Z Axis)
3-
 * by SemlerPDX June2019
3+
 *    *(coming next: a 3-way Toggle Switch add-on)
4
 *  for Arduino Micro/Leonardo / Sparkfun Pro Micro or equiv. clones
5-
 * ( https://veterans-gaming.com/blogs/entry/32-diy-custom-game-controller-2-dial-hsi-course-and-heading-knobs/ )
5+
 * by SemlerPDX Aug2019
6-
 *
6+
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-
 *
10+
11
 *  Rotary Encoder 1 - (OUTA-OUTB-SW) = Arduino Pins (0,1,15)
12-
 * https://www.pjrc.com/teensy/td_libs_Encoder.html
12+
13-
 *
13+
 *  Rotary Encoder 3 - (OUTA-OUTB-SW) = Arduino Pins (9,10,7)
14-
 *  Joystick Library
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-
// Tell the Encoder Library which pins have encoders
24+
25
#include <Encoder.h>
26
#include <Joystick.h>
27
28-
// Rotary Encoder Push Button Pins
28+
//Tell the Encoder Library which pins have encoders
29-
int buttonArray[2] = {15, 6};
29+
30-
const int numButtons = sizeof(buttonArray) / sizeof(buttonArray[0]);
30+
31
Encoder axisZRotation(9, 10);
32-
// Rotary Encoder Interrupt Pins
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-
// Delay Time between loops
38+
39
int EncoderPin2 = 2;
40
int EncoderPin3 = 3;
41-
// Variables to compare current to old values
41+
int EncoderPin4 = 9;
42
int EncoderPin5 = 10;
43
44
//Delay Time between loops
45
int debounceDelay = 260;
46
47-
// Intervals for Jump/Warp Speed Rotations
47+
//Variables to compare current to old values
48
int oldX = 0;
49
int oldY = 0;
50
int oldZ = 0;
51-
// Set generic joystick using id 42 with 2 buttons and 2 axes
51+
52
int RyAxis_Value = 1;
53-
  0x04, 2, 0,
53+
int RzAxis_Value = 1;
54-
  false, false, false, true, true, false,
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-
  // Set Encoder Pins as Pullups
60+
61
  0x04, 3, 0,
62
  false, false, false, true, true, true,
63
  false, false, false, false, false);  
64
65
66-
  // Loop through buttons and set them as Pullups
66+
67-
  for (int x = 0; x < numButtons; x++) {
67+
68
  //Set Encoder Pins as Pullups
69
  pinMode(EncoderPin0, INPUT_PULLUP);
70
  pinMode(EncoderPin1, INPUT_PULLUP);
71-
  // Set Range of custom Axes
71+
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-
  for (int x = 0; x < numButtons; x++) {
84+
  Joystick.setRzAxisRange(0, 359);
85
  
86
  // Initialize Joystick Library
87
  Joystick.begin(false);
88
89
}
90
91-
  if (newX != oldX) {
91+
92-
    RxAxis_Value = getAxisVal(newX, oldX, RxAxis_Value);
92+
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-
  if (newY != oldY) {
100+
101-
    RyAxis_Value = getAxisVal(newY, oldY, RyAxis_Value);
101+
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-
  // Send Joystick info through USB
107+
108
    axisXRotation.write(newX);
109
    oldX = newX;
110
111
  }else if (newX < oldX) {
112
    //Determine speed of decrement & set output
113-
// Function to return Rotation Value adjusted for the turning speed
113+
    int difX = oldX - newX;
114-
int speedVal(int val) {
114+
    RxAxis_Value = speedVal(difX, RxAxis_Value, 0);
115-
  if (val >= WarpSpeed) {
115+
116-
    val = WarpSpeed;
116+
117-
  } else if (val >= JumpSpeed) {
117+
118-
    val = JumpSpeed;
118+
119-
  } else {
119+
120-
    val = 1;
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-
// Function to return modified Rotation Axis Value
126+
    RyAxis_Value = speedVal(difY, RyAxis_Value, 1);
127-
int getAxisVal(int newVal, int oldVal, int val) {
127+
128-
  if (newVal > oldVal) {
128+
129-
    // Determine speed of increment & set output
129+
130-
    newVal = newVal - oldVal;
130+
131-
    val = val + speedVal(newVal);
131+
  }else if (newY < oldY) {
132-
  } else if (newVal < oldVal) {
132+
    //Determine speed of decrement & set output
133-
    // Determine speed of decrement & set output
133+
    int difY = oldY - newY;
134-
    newVal = oldVal - newVal;
134+
    RyAxis_Value = speedVal(difY, RyAxis_Value, 0);
135-
    val = val - speedVal(newVal);
135+
136
    axisYRotation.write(newY);
137
    oldY = newY;
138-
  // Correct Rotation Value within 360 degrees
138+
139-
  val = (val % 360 + 360) % 360;
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
}