Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. int input_pins[5] = { 5, 6, 7, 8, 9 } ;
  2. int strobe_pins[5] = { 18, 15, 14, 16, 10 };
  3. unsigned long key_state[5][5];
  4. int keycode[5][5] = { { 'a', 'b', 'c', 'd', 'e' },
  5. { 'f', 'g', 'h', 'i', 'j' },
  6. { 'k', 'l', 'm', 'n', 'o' },
  7. { 'p', 'q', 'r', 's', 't' },
  8. { 'u', 'v', 'w', 'x', 'y' } };
  9. int strobe_row = 0;
  10. int q = 0;
  11.  
  12. void setup() {
  13. int cnt;
  14. int cnt2;
  15.  
  16. pinMode(17, OUTPUT);
  17.  
  18. for (cnt = 0; cnt < 5; cnt++) {
  19. pinMode(input_pins[cnt], INPUT_PULLUP);
  20. pinMode(strobe_pins[cnt], OUTPUT);
  21. digitalWrite(strobe_pins[cnt], HIGH);
  22.  
  23. for (cnt2 = 0; cnt2 < 5; cnt2++) key_state[cnt][cnt2] = 0;
  24. }
  25.  
  26. Keyboard.begin();
  27. Serial.begin(9600);
  28. Serial.println("Starting keyboard");
  29. }
  30.  
  31. bool debounce(unsigned long t_now, unsigned long t_prev) {
  32. unsigned long diff;
  33.  
  34. diff = t_now - t_prev; // need to check for underflow?
  35.  
  36. if (diff <= 20) return true;
  37. else return false;
  38. }
  39.  
  40. void loop() {
  41. unsigned long tick_now = millis();
  42. int cnt;
  43.  
  44. if (q == 0) digitalWrite(17, LOW);
  45. else if (q == 128) digitalWrite(17, HIGH);
  46. q++;
  47. if (q == 256) q = 0;
  48.  
  49. // since we use non zero to indicate pressed state, we need
  50. // to handle the edge case where millis() returns 0
  51.  
  52. if (tick_now == 0) tick_now = 1;
  53.  
  54. if (strobe_row >= 5) strobe_row = 0;
  55. digitalWrite(strobe_pins[strobe_row], LOW);
  56. delay(2); // give it some time to stabilize just in case
  57.  
  58. for (cnt = 0; cnt < 5; cnt++) {
  59. // ignore state change for pin if in debounce period
  60. if (key_state[strobe_row][cnt] != 0)
  61. if (debounce(tick_now, key_state[strobe_row][cnt]) == true)
  62. continue;
  63.  
  64. if (digitalRead(input_pins[cnt]) == HIGH) {
  65. Serial.println("released");
  66. if (key_state[strobe_row][cnt] != 0) {
  67. Keyboard.release(keycode[strobe_row][cnt]);
  68. key_state[strobe_row][cnt] = 0;
  69. }
  70. } else {
  71. if (key_state[strobe_row][cnt] == 0) {
  72. Keyboard.press(keycode[strobe_row][cnt]);
  73. key_state[strobe_row][cnt] = tick_now;
  74. }
  75. }
  76. }
  77.  
  78. digitalWrite(strobe_pins[strobe_row], HIGH);
  79. strobe_row++;
  80. delay(5);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement