Guest User

Teensy Based Button Box Code

a guest
Jan 20th, 2019
3,016
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Bounce.h>
  2. #include <elapsedMillis.h>
  3. #include <Rotary.h>
  4.  
  5. Bounce key_on = Bounce(0, 10);
  6. Bounce key_start = Bounce(1, 10);
  7. Bounce shift_up = Bounce(8,10);
  8. Bounce shift_down = Bounce(9,10);
  9. Bounce pbrake = Bounce(10,10);
  10.  
  11. Bounce r_button_1 = Bounce(11,10);
  12. Bounce r_button_2 = Bounce(12,10);
  13. Bounce r_button_3 = Bounce(26,10);
  14. Bounce r_button_4 = Bounce(13,10);
  15. Bounce r_button_5 = Bounce(14,10);
  16. Bounce r_button_6 = Bounce(15,10);
  17. Bounce r_button_7 = Bounce(16,10);
  18.  
  19. Bounce l_switch_1 = Bounce(17,10);
  20. Bounce l_switch_2 = Bounce(18,10);
  21. Bounce l_switch_3 = Bounce(19,10);
  22. Bounce l_switch_4 = Bounce(20,10);
  23.  
  24. Bounce enc_button_1 = Bounce(21,10);
  25. Bounce enc_button_2 = Bounce(22,10);
  26. Bounce enc_button_3 = Bounce(23,10);
  27.  
  28. elapsedMillis timeElapsed;
  29. unsigned int t_interval = 200; //interval to hold buttons on for when needed
  30. unsigned int enc_interval = 20; //interval for encoders
  31.  
  32. bool key_start_on = false;
  33.  
  34. Rotary rotary1 = Rotary(6, 7);
  35. Rotary rotary2 = Rotary(2, 3);
  36. Rotary rotary3 = Rotary(4, 5);
  37.  
  38. void setup() {
  39.  
  40. attachInterrupt(6, rotate, CHANGE);
  41. attachInterrupt(7, rotate, CHANGE);
  42. attachInterrupt(2, rotate, CHANGE);
  43. attachInterrupt(3, rotate, CHANGE);
  44. attachInterrupt(4, rotate, CHANGE);
  45. attachInterrupt(5, rotate, CHANGE);
  46.  
  47. pinMode(0, INPUT_PULLUP);
  48. pinMode(1, INPUT_PULLUP);
  49. pinMode(8, INPUT_PULLUP);
  50. pinMode(9, INPUT_PULLUP);
  51. pinMode(10, INPUT_PULLUP);
  52. pinMode(11, INPUT_PULLUP);
  53. pinMode(12, INPUT_PULLUP);
  54. pinMode(13, INPUT_PULLUP);
  55. pinMode(14, INPUT_PULLUP);
  56. pinMode(15, INPUT_PULLUP);
  57. pinMode(16, INPUT_PULLUP);
  58. pinMode(17, INPUT_PULLUP);
  59. pinMode(18, INPUT_PULLUP);
  60. pinMode(19, INPUT_PULLUP);
  61. pinMode(20, INPUT_PULLUP);
  62. pinMode(21, INPUT_PULLUP);
  63. pinMode(22, INPUT_PULLUP);
  64. pinMode(23, INPUT_PULLUP);
  65. pinMode(26, INPUT_PULLUP);
  66.  
  67. }
  68.  
  69. void loop() {
  70.  
  71. //UPDATE
  72.  
  73. key_on.update();
  74. key_start.update();
  75.  
  76. shift_up.update();
  77. shift_down.update();
  78.  
  79. pbrake.update();
  80.  
  81. r_button_1.update();
  82. r_button_2.update();
  83. r_button_3.update();
  84. r_button_4.update();
  85. r_button_5.update();
  86. r_button_6.update();
  87. r_button_7.update();
  88.  
  89. l_switch_1.update();
  90. l_switch_2.update();
  91. l_switch_3.update();
  92. l_switch_4.update();
  93.  
  94. enc_button_1.update();
  95. enc_button_2.update();
  96. enc_button_3.update();
  97.  
  98. //HANDLE KEY STARTER
  99.  
  100. if (key_on.fallingEdge()) { //Detects when key moves from OFF to ON
  101. Joystick.button(1, 1);
  102. timeElapsed = 0;
  103. }
  104. if (key_start.fallingEdge()) { //Detects when key moves from ON to START
  105. key_start_on = true;
  106. Joystick.button(1, 1);
  107. }
  108.  
  109. if (key_start.risingEdge()) { //Detects when key moves from START to ON
  110. key_start_on = false;
  111. Joystick.button(1, 0);
  112. }
  113. if (key_on.risingEdge()) { //Detects when key moves from ON to OFF
  114. timeElapsed = 0;
  115. Joystick.button(1, 1);
  116. }
  117.  
  118. if (timeElapsed > t_interval && key_start_on == false) {
  119. Joystick.button(1,0);
  120. }
  121.  
  122. //HANDLE SHIFTER
  123.  
  124. if (shift_up.fallingEdge()) { //Detects when the switch moves up
  125. timeElapsed = 0;
  126. Joystick.button(2,1);
  127. }
  128.  
  129. if (shift_down.fallingEdge()) { //Detects when the switch moves down
  130. timeElapsed = 0;
  131. Joystick.button(3,1);
  132. }
  133.  
  134. if (shift_down.risingEdge() || shift_up.risingEdge()) { //Detects when the switch moves to the center from either direction
  135. timeElapsed = 0;
  136. Joystick.button(4,1);
  137. }
  138.  
  139. if (timeElapsed > t_interval) {
  140. Joystick.button(2,0);
  141. Joystick.button(3,0);
  142. Joystick.button(4,0);
  143. }
  144.  
  145. //HANDLE PARKING BRAKE SWITCH
  146.  
  147. if (pbrake.fallingEdge()) {
  148. Joystick.button(5,1);
  149. timeElapsed = 0;
  150. }
  151.  
  152. if (pbrake.risingEdge()) {
  153. Joystick.button(5,1);
  154. timeElapsed = 0;
  155. }
  156.  
  157. if (timeElapsed > t_interval) {
  158. Joystick.button(5,0);
  159. }
  160.  
  161. //HANDLE BUTTONS
  162.  
  163. if (r_button_1.fallingEdge()) {
  164. Joystick.button(6,1);
  165. }
  166. if (r_button_1.risingEdge()) {
  167. Joystick.button(6,0);
  168. }
  169.  
  170. if (r_button_2.fallingEdge()) {
  171. Joystick.button(7,1);
  172. }
  173. if (r_button_2.risingEdge()) {
  174. Joystick.button(7,0);
  175. }
  176.  
  177. if (r_button_3.fallingEdge()) {
  178. Joystick.button(8,1);
  179. }
  180. if (r_button_3.risingEdge()) {
  181. Joystick.button(8,0);
  182. }
  183.  
  184. if (r_button_4.fallingEdge()) {
  185. Joystick.button(9,1);
  186. }
  187. if (r_button_4.risingEdge()) {
  188. Joystick.button(9,0);
  189. }
  190.  
  191. if (r_button_5.fallingEdge()) {
  192. Joystick.button(10,1);
  193. }
  194. if (r_button_5.risingEdge()) {
  195. Joystick.button(10,0);
  196. }
  197.  
  198. if (r_button_6.fallingEdge()) {
  199. Joystick.button(11,1);
  200. }
  201. if (r_button_6.risingEdge()) {
  202. Joystick.button(11,0);
  203. }
  204.  
  205. if (r_button_7.fallingEdge()) {
  206. Joystick.button(12,1);
  207. }
  208. if (r_button_7.risingEdge()) {
  209. Joystick.button(12,0);
  210. }
  211.  
  212. //HANDLE LED SWITCHES
  213.  
  214. if (l_switch_1.fallingEdge()) {
  215. Joystick.button(13,1);
  216. timeElapsed = 0;
  217. }
  218. if (l_switch_1.risingEdge()) {
  219. Joystick.button(13,1);
  220. timeElapsed = 0;
  221. }
  222.  
  223. if (l_switch_2.fallingEdge()) {
  224. Joystick.button(14,1);
  225. timeElapsed = 0;
  226. }
  227. if (l_switch_2.risingEdge()) {
  228. Joystick.button(14,1);
  229. timeElapsed = 0;
  230. }
  231.  
  232. if (l_switch_3.fallingEdge()) {
  233. Joystick.button(15,1);
  234. timeElapsed = 0;
  235. }
  236. if (l_switch_3.risingEdge()) {
  237. Joystick.button(15,1);
  238. timeElapsed = 0;
  239. }
  240.  
  241. if (l_switch_4.fallingEdge()) {
  242. Joystick.button(16,1);
  243. timeElapsed = 0;
  244. }
  245. if (l_switch_4.risingEdge()) {
  246. Joystick.button(16,1);
  247. timeElapsed = 0;
  248. }
  249.  
  250. if (timeElapsed > t_interval) {
  251. Joystick.button(13,0);
  252. Joystick.button(14,0);
  253. Joystick.button(15,0);
  254. Joystick.button(16,0);
  255. }
  256.  
  257. if (timeElapsed > enc_interval) {
  258. Joystick.button(20,0);
  259. Joystick.button(21,0);
  260. Joystick.button(22,0);
  261. Joystick.button(23,0);
  262. Joystick.button(24,0);
  263. Joystick.button(25,0);
  264. }
  265.  
  266. //HANDLE ENCODER BUTTONS
  267.  
  268. if (enc_button_1.fallingEdge()) {
  269. Joystick.button(17,1);
  270. }
  271. if (enc_button_1.risingEdge()) {
  272. Joystick.button(17,0);
  273. }
  274.  
  275. if (enc_button_2.fallingEdge()) {
  276. Joystick.button(18,1);
  277. }
  278. if (enc_button_2.risingEdge()) {
  279. Joystick.button(18,0);
  280. }
  281.  
  282. if (enc_button_3.fallingEdge()) {
  283. Joystick.button(19,1);
  284. }
  285. if (enc_button_3.risingEdge()) {
  286. Joystick.button(19,0);
  287. }
  288.  
  289. }
  290.  
  291. void rotate() {
  292.  
  293. // HANDLE ENCODER 1
  294. unsigned char result1 = rotary1.process();
  295. if (result1 == DIR_CW) {
  296. timeElapsed = 0;
  297. Joystick.button(20,1);
  298. Joystick.button(21,0);
  299. } else if (result1 == DIR_CCW) {
  300. timeElapsed = 0;
  301. Joystick.button(21,1);
  302. Joystick.button(20,0);
  303. }
  304.  
  305. // HANDLE ENCODER 2
  306. unsigned char result2 = rotary2.process();
  307. if (result2 == DIR_CW) {
  308. timeElapsed = 0;
  309. Joystick.button(22,1);
  310. Joystick.button(23,0);
  311. } else if (result2 == DIR_CCW) {
  312. timeElapsed = 0;
  313. Joystick.button(23,1);
  314. Joystick.button(22,0);
  315. }
  316.  
  317. // HANDLE ENCODER 3
  318. unsigned char result3 = rotary3.process();
  319. if (result3 == DIR_CW) {
  320. timeElapsed = 0;
  321. Joystick.button(24,1);
  322. Joystick.button(25,0);
  323. } else if (result3 == DIR_CCW) {
  324. timeElapsed = 0;
  325. Joystick.button(25,1);
  326. Joystick.button(24,0);
  327. }
  328.  
  329. }
RAW Paste Data