Advertisement
Guest User

ButtonboxSketch

a guest
Nov 21st, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <Joystick.h>
  3.  
  4. //DEFINITIONS
  5. #define ENABLE_PULLUPS
  6. #define NUMROTARIES 2 //replace "?" with number of rotary encoders you are using
  7. #define NUMBUTTONS 18 //replace "?"with number of buttong you are using
  8. #define NUMROWS 6 //replace "?" with number of rows you have
  9. #define NUMCOLS 4 //replace "?" with number of columns you have
  10.  
  11. //BUTTON MATRIX
  12. //first change number of rows and columns to match your button matrix,
  13. //then replace all "?" with numbers (starting from 0)
  14. byte buttons[NUMROWS][NUMCOLS] = {
  15. {0,1,2,3},
  16. {4,5,6,7},
  17. {8,9,10,11},
  18. {12,13,14,15},
  19. {16,17,18,19},
  20. {20,21,22,24}
  21.  
  22.  
  23.  
  24. };
  25.  
  26. struct rotariesdef {
  27. byte pin1;
  28. byte pin2;
  29. int ccwchar;
  30. int cwchar;
  31. volatile unsigned char state;
  32. };
  33.  
  34. //ROTARY ENCODERS
  35. //each line controls a different rotary encoder
  36. //the first two numbers refer to the pins the encoder is connected to
  37. //the second two are the buttons each click of the encoder wil press
  38. //do NOT exceed 31 for the final button number
  39. rotariesdef rotaries[NUMROTARIES] {
  40. {0,1,22,23,0}, //rotary 1
  41. {2,3,24,25,0} //rotary 2
  42.  
  43.  
  44. };
  45.  
  46. #define DIR_CCW 0x10
  47. #define DIR_CW 0x20
  48. #define R_START 0x0
  49.  
  50. #ifdef HALF_STEP
  51. #define R_CCW_BEGIN 0x1
  52. #define R_CW_BEGIN 0x2
  53. #define R_START_M 0x3
  54. #define R_CW_BEGIN_M 0x4
  55. #define R_CCW_BEGIN_M 0x5
  56. const unsigned char ttable[6][4] = {
  57. // R_START (00)
  58. {R_START_M, R_CW_BEGIN, R_CCW_BEGIN, R_START},
  59. // R_CCW_BEGIN
  60. {R_START_M | DIR_CCW, R_START, R_CCW_BEGIN, R_START},
  61. // R_CW_BEGIN
  62. {R_START_M | DIR_CW, R_CW_BEGIN, R_START, R_START},
  63. // R_START_M (11)
  64. {R_START_M, R_CCW_BEGIN_M, R_CW_BEGIN_M, R_START},
  65. // R_CW_BEGIN_M
  66. {R_START_M, R_START_M, R_CW_BEGIN_M, R_START | DIR_CW},
  67. // R_CCW_BEGIN_M
  68. {R_START_M, R_CCW_BEGIN_M, R_START_M, R_START | DIR_CCW},
  69. };
  70. #else
  71. #define R_CW_FINAL 0x1
  72. #define R_CW_BEGIN 0x2
  73. #define R_CW_NEXT 0x3
  74. #define R_CCW_BEGIN 0x4
  75. #define R_CCW_FINAL 0x5
  76. #define R_CCW_NEXT 0x6
  77.  
  78. const unsigned char ttable[7][4] = {
  79. // R_START
  80. {R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
  81. // R_CW_FINAL
  82. {R_CW_NEXT, R_START, R_CW_FINAL, R_START | DIR_CW},
  83. // R_CW_BEGIN
  84. {R_CW_NEXT, R_CW_BEGIN, R_START, R_START},
  85. // R_CW_NEXT
  86. {R_CW_NEXT, R_CW_BEGIN, R_CW_FINAL, R_START},
  87. // R_CCW_BEGIN
  88. {R_CCW_NEXT, R_START, R_CCW_BEGIN, R_START},
  89. // R_CCW_FINAL
  90. {R_CCW_NEXT, R_CCW_FINAL, R_START, R_START | DIR_CCW},
  91. // R_CCW_NEXT
  92. {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
  93. };
  94. #endif
  95.  
  96. //BUTTON MATRIX PART 2
  97. byte rowPins[NUMROWS] = {4,5,6,7,8,9}; //change "?" to the pins the rows of your button matrix are connected to
  98. byte colPins[NUMCOLS] = {10,14,15,16}; //change "?" to the pins the rows of your button matrix are connected to
  99.  
  100. Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
  101.  
  102. //JOYSTICK SETTINGS
  103. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  104. JOYSTICK_TYPE_JOYSTICK,
  105. 32, //number of buttons
  106. 0, //number of hat switches
  107. //Set as many axis to "true" as you have potentiometers for
  108. false, // y axis
  109. false, // x axis
  110. true, // z axis
  111. true, // rx axis
  112. false, // ry axis
  113. false, // rz axis
  114. false, // rudder
  115. false, // throttle
  116. false, // accelerator
  117. false, // brake
  118. false); // steering wheel
  119.  
  120. const int numReadings = 20;
  121.  
  122. int readings[numReadings]; // the readings from the analog input
  123. int index = 0; // the index of the current reading
  124. int total = 0; // the running total
  125. int currentOutputLevel = 0;
  126.  
  127. //POTENTIOMETERS PART 1
  128. //add all the axis' which are enabled above
  129. int zAxis_ = 0;
  130. int RxAxis_ = 0;
  131.  
  132.  
  133. //POTENTIOMETERS PART 2
  134. //Which pins are your potentiometers connected to?
  135. int potentiometerPin1 = A0; //Change "?" to the pin your potentiometer is connected to
  136. int potentiometerPin2 = A1;
  137. const bool initAutoSendState = true;
  138.  
  139.  
  140. void setup() {
  141. Joystick.begin();
  142. rotary_init();
  143. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  144. readings[thisReading] = 0;
  145. }
  146. }
  147.  
  148. void loop() {
  149.  
  150. CheckAllEncoders();
  151. CheckAllButtons();
  152. CheckAllPotentiometers();
  153.  
  154. }
  155.  
  156. //POTENTIOMETERS PART 3
  157. //change the details to match teh details above for each potentiometer you are using
  158. void CheckAllPotentiometers(){
  159.  
  160. //potentiometer 1
  161. currentOutputLevel = getAverageOutput(potentiometerPin1);
  162. zAxis_ = map(currentOutputLevel,0,1023,0,255);
  163. Joystick.setZAxis(zAxis_);
  164.  
  165. //potentiometer 2
  166. currentOutputLevel = getAverageOutput(potentiometerPin2);
  167. RxAxis_ = map(currentOutputLevel,0,1023,0,255);
  168. Joystick.setRxAxis(RxAxis_);
  169.  
  170.  
  171. }
  172.  
  173. int getAverageOutput(int pinToRead){
  174. index = 0;
  175. total = 0;
  176.  
  177. while (index < numReadings){
  178. readings[index] = analogRead(pinToRead);
  179. total = total + readings[index];
  180. index = index + 1;
  181. //delay (1);
  182. }
  183. return total / numReadings;
  184. }
  185.  
  186.  
  187. void CheckAllButtons(void) {
  188. if (buttbx.getKeys())
  189. {
  190. for (int i=0; i<LIST_MAX; i++)
  191. {
  192. if ( buttbx.key[i].stateChanged )
  193. {
  194. switch (buttbx.key[i].kstate) {
  195. case PRESSED:
  196. case HOLD:
  197. Joystick.setButton(buttbx.key[i].kchar, 1);
  198. break;
  199. case RELEASED:
  200. case IDLE:
  201. Joystick.setButton(buttbx.key[i].kchar, 0);
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. }
  208.  
  209.  
  210. void rotary_init() {
  211. for (int i=0;i<NUMROTARIES;i++) {
  212. pinMode(rotaries[i].pin1, INPUT);
  213. pinMode(rotaries[i].pin2, INPUT);
  214. #ifdef ENABLE_PULLUPS
  215. digitalWrite(rotaries[i].pin1, HIGH);
  216. digitalWrite(rotaries[i].pin2, HIGH);
  217. #endif
  218. }
  219. }
  220.  
  221.  
  222. unsigned char rotary_process(int _i) {
  223. //Serial.print("Processing rotary: ");
  224. //Serial.println(_i);
  225. unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
  226. rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
  227. return (rotaries[_i].state & 0x30);
  228. }
  229.  
  230. void CheckAllEncoders(void) {
  231. Serial.println("Checking rotaries");
  232. for (int i=0;i<NUMROTARIES;i++) {
  233. unsigned char result = rotary_process(i);
  234. if (result == DIR_CCW) {
  235. Serial.print("Rotary ");
  236. Serial.print(i);
  237. Serial.println(" <<< Going CCW");
  238. Joystick.setButton(rotaries[i].ccwchar, 1); delay(50); Joystick.setButton(rotaries[i].ccwchar, 0);
  239. };
  240. if (result == DIR_CW) {
  241. Serial.print("Rotary ");
  242. Serial.print(i);
  243. Serial.println(" >>> Going CW");
  244. Joystick.setButton(rotaries[i].cwchar, 1); delay(50); Joystick.setButton(rotaries[i].cwchar, 0);
  245. };
  246. }
  247. Serial.println("Done checking");
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement