Advertisement
DIUPIUPAD

PIU code debouncing

Jun 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1.  
  2. /*
  3. This code contains the follow functions:
  4. - void setup(): Sets pins 4, 5, 6, 7 to input with pull-up resistors enabled and begins Keyboard functionality
  5. - void loop(): Main loop - reads pin voltages and sends out corresponding keystrokes via USB
  6. */
  7.  
  8. /*
  9. Pinout:
  10. - "HIGH" voltage button contacts - pins 4, 5, 6, 7, 8
  11. - "GND" voltage button contacts - GND pin
  12. */
  13.  
  14. /*
  15. August 17, 2018
  16. This is an example of one way to de-bounce buttons in code.
  17. This sketch doesn't really do anything, it only shows how to de-bounce buttons in code.
  18. */
  19.  
  20. #include <Keyboard.h>
  21.  
  22. int upleftStatus = 1;
  23. int upleftStatusPrev = 1;
  24. int downleftStatus = 1;
  25. int downleftStatusPrev = 1;
  26. int centerStatus = 1;
  27. int centerStatusPrev = 1;
  28. int uprightStatus = 1;
  29. int uprightStatusPrev = 1;
  30. int downrightStatus = 1;
  31. int downrightStatusPrev = 1;
  32.  
  33. int buttonPin4 = 4; // These are defining the pins for the button connections.
  34. int buttonPin5 = 5;
  35. int buttonPin6 = 6;
  36. int buttonPin7 = 7;
  37. int buttonPin8 = 8;
  38.  
  39.  
  40. // All five buttons share the variables below.
  41. bool buttonPressed = false; // This is the flag variable that must be false for button input to be accepted.
  42. // When a button is pushed, the above value is set to [true] and a start time [ millis() ] is saved in button_press_start_time.
  43. unsigned long button_press_start_time = 0; // This stores the system time that the button was pushed.
  44. unsigned long button_press_current_time = 0; // This stores the current time, to compare to the value above.
  45.  
  46. int button_debounce_time = 60; // This is the length of time (in milliseconds) to de-bounce the button inputs.
  47.  
  48. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  49. // Function prototypes:
  50. // These aren't really needed in this instance (with a one-file sketch in the Arduino IDE) but some compilers do require them.
  51. void buttonPressDelay();
  52. void doButton4stuff();
  53. void doButton5stuff();
  54. void doButton6stuff();
  55. void doButton7stuff();
  56. void doButton8stuff();
  57.  
  58. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  59.  
  60. void setup() {
  61. // All that is done here is setting the input pin states.
  62. pinMode(buttonPin4, INPUT_PULLUP);
  63. pinMode(buttonPin5, INPUT_PULLUP);
  64. pinMode(buttonPin6, INPUT_PULLUP);
  65. pinMode(buttonPin7, INPUT_PULLUP);
  66. pinMode(buttonPin8, INPUT_PULLUP);
  67. Keyboard.begin();
  68. }
  69.  
  70. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  71.  
  72. void loop() { // begin of main sketch loop
  73.  
  74. /*
  75. Whatever other code you want can go here, outside of the button-handling code.
  76. */
  77.  
  78. upleftStatus = digitalRead(4);
  79. downleftStatus = digitalRead(5);
  80. centerStatus = digitalRead(6);
  81. uprightStatus = digitalRead(7);
  82. downrightStatus = digitalRead(8);
  83.  
  84. //UPLEFT ARROW PRESSED
  85. if (upleftStatus != upleftStatusPrev && upleftStatus == LOW)
  86. {
  87. Keyboard.press('r');
  88. upleftStatusPrev = upleftStatus;
  89. }
  90. //UPLEFT ARROW RELEASED
  91. if (upleftStatus != upleftStatusPrev && upleftStatus == HIGH)
  92. {
  93. Keyboard.release('r');
  94. upleftStatusPrev = upleftStatus;
  95. }
  96.  
  97. //DOWNLEFT ARROW PRESSED
  98. if (downleftStatus != downleftStatusPrev && downleftStatus == LOW)
  99. {
  100. Keyboard.press('v');
  101. downleftStatusPrev = downleftStatus;
  102. }
  103. //DOWNLEFT ARROW RELEASED
  104. if (downleftStatus != downleftStatusPrev && downleftStatus == HIGH)
  105. {
  106. Keyboard.release('v');
  107. downleftStatusPrev = downleftStatus;
  108. }
  109.  
  110. //CENTER ARROW PRESSED
  111. if (centerStatus != centerStatusPrev && centerStatus == LOW)
  112. {
  113. Keyboard.press('g');
  114. centerStatusPrev = centerStatus;
  115. }
  116. //CENTER ARROW RELEASED
  117. if (centerStatus != centerStatusPrev && centerStatus == HIGH)
  118. {
  119. Keyboard.release('g');
  120. centerStatusPrev = centerStatus;
  121. }
  122.  
  123. //UPRIGHT ARROW PRESSED
  124. if (uprightStatus != uprightStatusPrev && uprightStatus == LOW)
  125. {
  126. Keyboard.press('y');
  127. uprightStatusPrev = uprightStatus;
  128. }
  129. //UPRIGHT ARROW RELEASED
  130. if (uprightStatus != uprightStatusPrev && uprightStatus == HIGH)
  131. {
  132. Keyboard.release('y');
  133. uprightStatusPrev = uprightStatus;
  134. }
  135.  
  136. //DOWNRIGHT ARROW PRESSED
  137. if (downrightStatus != downrightStatusPrev && downrightStatus == LOW)
  138. {
  139. Keyboard.press('n');
  140. downrightStatusPrev = downrightStatus;
  141. }
  142. //DOWNRIGHT ARROW RELEASED
  143. if (downrightStatus != downrightStatusPrev && downrightStatus == HIGH)
  144. {
  145. Keyboard.release('n');
  146. downrightStatusPrev = downrightStatus;
  147. }
  148.  
  149. if (buttonPressed == false) {
  150. // The 'if' conditional statement above only allows checking for button presses if the boolean indicator is set to [false].
  151. // The boolean indicator will only be false if there have been no button presses since the button_delay_time has elapsed from the previous button press.
  152.  
  153. // Below is reading the #4 button pin.
  154. // Since the pins were set to input_pullup, these should be checked for the low state.
  155. if (digitalRead(buttonPin4) == LOW) {
  156. buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  157. doButton4stuff(); // Whatever this button press does, is in this function.
  158. }
  159.  
  160. // Below is reading the #5 button pin.
  161. if (digitalRead(buttonPin5) == LOW) {
  162. buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  163. doButton5stuff(); // Whatever this button press does, is in this function.
  164. }
  165.  
  166. // Below is reading the #6 button pin.
  167. if (digitalRead(buttonPin6) == LOW) {
  168. buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  169. doButton6stuff(); // Whatever this button press does, is in this function.
  170. }
  171.  
  172. // Below is reading the #7 button pin.
  173. if (digitalRead(buttonPin7) == LOW) {
  174. buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  175. doButton7stuff(); // Whatever this button press does, is in this function.
  176. }
  177.  
  178. // Below is reading the #8 button pin.
  179. if (digitalRead(buttonPin8) == LOW) {
  180. buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  181. doButton8stuff(); // Whatever this button press does, is in this function.
  182. }
  183.  
  184.  
  185. } // end of ---> if (buttonPressed == false)
  186. else { // ---> if (buttonPressed == true)
  187. // The code below re-sets the buttonPressed variable to false, but only after the amount of milliseconds in {button_delay_time} have passed.
  188. // Other parts of the sketch can run during this wait time, but no more button inputs will be accepted until this time period expires.
  189. button_press_current_time = millis();
  190. if (button_press_current_time >= button_press_start_time) {
  191. if (button_press_current_time >= (button_press_start_time + button_debounce_time)) {
  192. buttonPressed = false;
  193. }
  194. }
  195. else { // if (button_press_current_time < button_press_start_time)
  196. // This test checks for the condition that happens when the system timer has rolled over.
  197. button_press_start_time = millis();
  198. }
  199. }
  200.  
  201. /*
  202. Whatever other code you want can go here, outside of the button-handling code.
  203. */
  204.  
  205. } // This is the end of the main sketch loop.
  206.  
  207. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  208.  
  209. // The function below is what gets called whenever any button is pushed. The delay time is the same for all buttons.
  210. void buttonPressDelay() {
  211. button_press_start_time = millis();
  212. buttonPressed = true;
  213. }
  214.  
  215. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  216.  
  217. void doButton4stuff() {
  218. //UPLEFT ARROW PRESSED
  219. if (upleftStatus != upleftStatusPrev && upleftStatus == LOW)
  220. {
  221. Keyboard.press('r');
  222. upleftStatusPrev = upleftStatus;
  223. }
  224. //UPLEFT ARROW RELEASED
  225. if (upleftStatus != upleftStatusPrev && upleftStatus == HIGH)
  226. {
  227. Keyboard.release('r');
  228. upleftStatusPrev = upleftStatus;
  229. }// Whatever button 4 should do, goes in here
  230. }
  231.  
  232. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  233.  
  234. void doButton5stuff() {
  235.  
  236. //DOWNLEFT ARROW PRESSED
  237. if (downleftStatus != downleftStatusPrev && downleftStatus == LOW)
  238. {
  239. Keyboard.press('v');
  240. downleftStatusPrev = downleftStatus;
  241. }
  242. //DOWNLEFT ARROW RELEASED
  243. if (downleftStatus != downleftStatusPrev && downleftStatus == HIGH)
  244. {
  245. Keyboard.release('v');
  246. downleftStatusPrev = downleftStatus;
  247. }
  248.  
  249. // Whatever button 5 should do, goes in here
  250. }
  251.  
  252. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  253.  
  254. void doButton6stuff() {
  255.  
  256. //CENTER ARROW PRESSED
  257. if (centerStatus != centerStatusPrev && centerStatus == LOW)
  258. {
  259. Keyboard.press('g');
  260. centerStatusPrev = centerStatus;
  261. }
  262. //CENTER ARROW RELEASED
  263. if (centerStatus != centerStatusPrev && centerStatus == HIGH)
  264. {
  265. Keyboard.release('g');
  266. centerStatusPrev = centerStatus;
  267. }
  268. // Whatever button 6 should do, goes in here
  269. }
  270.  
  271. //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  272.  
  273. void doButton7stuff() {
  274.  
  275. //UPRIGHT ARROW PRESSED
  276. if (uprightStatus != uprightStatusPrev && uprightStatus == LOW)
  277. {
  278. Keyboard.press('y');
  279. uprightStatusPrev = uprightStatus;
  280. }
  281. //UPRIGHT ARROW RELEASED
  282. if (uprightStatus != uprightStatusPrev && uprightStatus == HIGH)
  283. {
  284. Keyboard.release('y');
  285. uprightStatusPrev = uprightStatus;
  286. }
  287. // Whatever button 7 should do, goes in here
  288. }
  289.  
  290. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  291.  
  292. void doButton8stuff() {
  293.  
  294. //DOWNRIGHT ARROW PRESSED
  295. if (downrightStatus != downrightStatusPrev && downrightStatus == LOW)
  296. {
  297. Keyboard.press('n');
  298. downrightStatusPrev = downrightStatus;
  299. }
  300. //DOWNRIGHT ARROW RELEASED
  301. if (downrightStatus != downrightStatusPrev && downrightStatus == HIGH)
  302. {
  303. Keyboard.release('n');
  304. downrightStatusPrev = downrightStatus;
  305. }
  306. // Whatever button 8 should do, goes in here
  307. }
  308.  
  309. // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement