stspringer

Arduino button array

Jun 10th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #define DEBOUNCE 20 // button debouncer, how many ms to debounce, 5+ ms is usually plenty
  2.  
  3. // here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
  4. //byte buttons[] = {4, 9, 12, A0}; // the analog 0-5 pins are also known as 14-19
  5.  
  6. byte buttons[] = {2, 3,}; // the analog 0-5 pins are also known as 14-19
  7.  
  8. // This handy macro lets us determine how big the array up above is, by checking the size
  9. #define NUMBUTTONS sizeof(buttons)
  10. // we will track if a button is just pressed, just released, or 'currently pressed'
  11. volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
  12.  
  13. void setup() {
  14. byte i;
  15.  
  16. // set up serial port
  17. Serial.begin(9600);
  18. Serial.print("Button checker with ");
  19. Serial.print(NUMBUTTONS, DEC);
  20. Serial.println(" buttons");
  21.  
  22. // pin13 LED
  23. pinMode(13, OUTPUT);
  24. pinMode(5, OUTPUT);
  25. pinMode(6, OUTPUT);
  26. pinMode(7, OUTPUT);
  27.  
  28. // Make input & enable pull-up resistors on switch pins
  29. for (i=0; i < NUMBUTTONS; i++)
  30. { pinMode(buttons[i], INPUT_PULLUP);
  31. digitalWrite(buttons[i], HIGH);
  32. }
  33.  
  34. // Run timer2 interrupt every 15 ms
  35. TCCR2A = 0;
  36. TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
  37.  
  38. //Timer2 Overflow Interrupt Enable
  39. TIMSK2 |= 1<<TOIE2;
  40.  
  41. }
  42.  
  43. SIGNAL(TIMER2_OVF_vect) {
  44. check_switches();
  45. }
  46.  
  47. void check_switches()
  48. {
  49. static byte previousstate[NUMBUTTONS];
  50. static byte currentstate[NUMBUTTONS];
  51. static long lasttime;
  52. byte index;
  53.  
  54. if (millis() < lasttime){ // we wrapped around, lets just try again
  55. lasttime = millis();
  56. }
  57.  
  58. if ((lasttime + DEBOUNCE) > millis()) {
  59. // not enough time has passed to debounce
  60. return;
  61. }
  62. // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  63. lasttime = millis();
  64.  
  65. for (index = 0; index < NUMBUTTONS; index++){
  66. currentstate[index] = digitalRead(buttons[index]); // read the button
  67.  
  68. /*
  69. Serial.print(index, DEC);
  70. Serial.print(": cstate=");
  71. Serial.print(currentstate[index], DEC);
  72. Serial.print(", pstate=");
  73. Serial.print(previousstate[index], DEC);
  74. Serial.print(", press=");
  75. */
  76.  
  77. if (currentstate[index] == previousstate[index]) {
  78. if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
  79. // just pressed
  80. justpressed[index] = 1;
  81. digitalWrite(7, LOW);
  82. }
  83. else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
  84. // just released
  85. justreleased[index] = 1;
  86. digitalWrite(7, HIGH);
  87. }
  88. pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
  89. }
  90. //Serial.println(pressed[index], DEC);
  91. previousstate[index] = currentstate[index]; // keep a running tally of the buttons
  92. }
  93. }
  94.  
  95.  
  96. void loop() {
  97.  
  98.  
  99.  
  100. for (byte i = 0; i < NUMBUTTONS; i++){
  101.  
  102. if (justpressed[i]) {
  103. justpressed[i] = 0;
  104. Serial.print(i, DEC);
  105. Serial.println(" Just pressed");
  106. // remember, check_switches() will CLEAR the 'just pressed' flag
  107. }
  108. if (justreleased[i]) {
  109.  
  110. justreleased[i] = 0;
  111.  
  112. Serial.print(i, DEC);
  113. Serial.println(" Just released");
  114.  
  115. // remember, check_switches() will CLEAR the 'just pressed' flag
  116. }
  117. if (pressed[i]) {
  118. Serial.print(i, DEC);
  119. Serial.println(" pressed");
  120. // is the button pressed down at this moment
  121. }
  122. }
  123.  
  124. }
Add Comment
Please, Sign In to add comment