Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. // -- A simple Arduino 4x4 matrix keypad example
  2. // Made by Bastiaan van der Plaat
  3.  
  4. // ### SIMPLE ARDUINO KEYPAD LIBRARY ###
  5. // ~ This library works fine but there is some strange
  6. // ~ behaviour when you press multiple keys at the same time.
  7. // ~ But for the most time the keys will be returend separately
  8.  
  9. // Keypad size
  10. #define KEYPAD_ROWS 4
  11. #define KEYPAD_COLUMNS 4
  12.  
  13. // Keypad characters
  14. const char keypad_keys[KEYPAD_ROWS][KEYPAD_COLUMNS]= {
  15. { '1', '2', '3', 'A' },
  16. { '4', '5', '6', 'B' },
  17. { '7', '8', '9', 'C' },
  18. { '*', '0', '#', 'D' }
  19. };
  20.  
  21. // Keypad digital pins on the Arduino
  22. const byte keypad_row_pins[KEYPAD_ROWS] = { 5, 6, 7, 8 };
  23. const byte keypad_column_pins[KEYPAD_COLUMNS] = { 9, 10, 11, 12 };
  24.  
  25. // Keypad initialization function
  26. void keypad_init(void) {
  27. // Set all the row pins to output and high
  28. for (byte i = 0; i < KEYPAD_ROWS; i++) {
  29. pinMode(keypad_row_pins[i], OUTPUT);
  30. digitalWrite(keypad_row_pins[i], HIGH);
  31. }
  32.  
  33. // Set all the column pins to input pullup
  34. for (byte i = 0; i < KEYPAD_COLUMNS; i++) {
  35. pinMode(keypad_column_pins[i], INPUT_PULLUP);
  36. }
  37. }
  38.  
  39. // Keypad keys state
  40. byte keypad_keys_state[KEYPAD_ROWS][KEYPAD_COLUMNS] = { 0 };
  41.  
  42. // Keypad function witch returns the pressed key as a ASCII char
  43. char keypad_get_key(void) {
  44. // Go over all the row pins
  45. for (byte y = 0; y < KEYPAD_ROWS; y++) {
  46. // Set the active row pin low
  47. digitalWrite(keypad_row_pins[y], LOW);
  48. // Go over all the column pins
  49. for (byte x = 0; x < KEYPAD_COLUMNS; x++) {
  50. // Check if the column pin is also low
  51. if (digitalRead(keypad_column_pins[x]) == LOW) {
  52. // The check if the key is not pressed
  53. if (keypad_keys_state[y][x] == 0) {
  54. // Set the state to 200 for the release count down
  55. keypad_keys_state[y][x] = 200;
  56. // Set the active row high again
  57. digitalWrite(keypad_row_pins[y], HIGH);
  58. // Return the pressed character
  59. return keypad_keys[y][x];
  60. }
  61. }
  62. // If the key is released count a simple counter down from 200 to 0
  63. else if (keypad_keys_state[y][x] > 0) {
  64. keypad_keys_state[y][x]--;
  65. }
  66. }
  67. // Set the row pin high again
  68. digitalWrite(keypad_row_pins[y], HIGH);
  69. }
  70.  
  71. // When no key is pressed return 0
  72. return 0;
  73. }
  74.  
  75. // ### MAIN CODE ###
  76.  
  77. // The password and password buffer vars
  78. #define PASSWORD_LENGTH 4
  79. const char password[PASSWORD_LENGTH + 1] = "1234";
  80. char password_buffer[PASSWORD_LENGTH + 1];
  81. byte password_buffer_position = 0;
  82.  
  83. void setup() {
  84. // Init the serial communication
  85. Serial.begin(9600);
  86.  
  87. // Init the keypad
  88. keypad_init();
  89. }
  90.  
  91. void loop() {
  92. // Get the pressed keypad character
  93. char key = keypad_get_key();
  94.  
  95. // If the hashtag key is pressed
  96. if (key == '#') {
  97. // Check if the buffer is filled
  98. if (password_buffer_position == PASSWORD_LENGTH) {
  99. // Compare the password and the buffer with each other
  100. if (strcmp(password, password_buffer) == 0) {
  101. Serial.println("Good password!");
  102. } else {
  103. Serial.println("Wrong password!");
  104. }
  105.  
  106. // Clear the password buffer
  107. while (password_buffer_position != 0) {
  108. password_buffer[--password_buffer_position] = 0;
  109. }
  110. } else {
  111. Serial.println("Buffer not full!");
  112. }
  113. }
  114.  
  115. // If the asterisk is pressed clear the password buffer
  116. else if (key == '*') {
  117. while (password_buffer_position != 0) {
  118. password_buffer[--password_buffer_position] = 0;
  119. }
  120. Serial.println("Buffer cleared!");
  121. }
  122.  
  123. // Add any other key to the password buffer if there is room
  124. else if (key != 0) {
  125. if (password_buffer_position < PASSWORD_LENGTH) {
  126. password_buffer[password_buffer_position++] = key;
  127. Serial.println(password_buffer);
  128. } else {
  129. Serial.println("Buffer full!");
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement