Guest User

Untitled

a guest
Mar 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3.  
  4. #define ROWS 4
  5. #define COLS 4
  6.  
  7. char pressedKey = '';
  8. int rowPins[ROWS] = {1, 4, 5, 6};
  9. int colPins[COLS] = {12, 3, 2, 0};
  10.  
  11. char keys[ROWS][COLS] = {
  12. {'1', '2', '3', 'A'},
  13. {'4', '5', '6', 'B'},
  14. {'7', '8', '9', 'C'},
  15. {'*', '0', '#', 'D'}
  16. };
  17.  
  18. void init_keypad()
  19. {
  20. for (int c = 0; c < COLS; c++)
  21. {
  22. pinMode(colPins[c], OUTPUT);
  23. digitalWrite(colPins[c], HIGH);
  24. }
  25.  
  26. for (int r = 0; r < ROWS; r++)
  27. {
  28. pinMode(rowPins[0], INPUT);
  29. digitalWrite(rowPins[r], PUD_UP);
  30. }
  31. }
  32.  
  33. int findLowRow()
  34. {
  35. for (int r = 0; r < ROWS; r++)
  36. {
  37. if (digitalRead(rowPins[r]) == LOW)
  38. return r;
  39. }
  40.  
  41. return -1;
  42. }
  43.  
  44. char get_key()
  45. {
  46. int rowIndex;
  47.  
  48. for (int c = 0; c < COLS; c++)
  49. {
  50. digitalWrite(colPins[c], LOW);
  51.  
  52. rowIndex = findLowRow();
  53. if (rowIndex > -1)
  54. {
  55. if (!pressedKey)
  56. pressedKey = keys[rowIndex][c];
  57. return pressedKey;
  58. }
  59.  
  60. digitalWrite(colPins[c], HIGH);
  61. }
  62.  
  63. pressedKey = '';
  64. return 0;
  65. }
  66.  
  67. int main(void)
  68. {
  69. wiringPiSetup();
  70.  
  71. init_keypad();
  72.  
  73. while(1)
  74. {
  75. char x = get_key();
  76. if (x)
  77. printf("pressed: %cn", x);
  78. else
  79. printf("no key pressedn");
  80.  
  81. delay(250);
  82. }
  83.  
  84. return 0;
  85. }
Add Comment
Please, Sign In to add comment