Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

main.ino 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <Arduino.h>
  2. #include <Keypad/Keypad.h>
  3. #include <LiquidCrystal.h>
  4. #define KP_ROWS 4
  5. #define KP_COLS 4
  6. char keys[KP_ROWS][KP_COLS] = {
  7. {'1','2','3','A'},
  8. {'4','5','6','B'},
  9. {'7','8','9','C'},
  10. {'*','0','#','.'}
  11. };
  12. byte rowPins[KP_ROWS] = {A3, A2, A0, A1};
  13. byte colPins[KP_COLS] = {8, 9, 10, 11};
  14. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);
  15. #define LCD_ROWS 2
  16. #define LCD_COLS 16
  17. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  18. #define SELF_TEST_LABEL "Self testing..."
  19. #define SELF_TEST_ERROR_LABEL "Error: "
  20. #define UID_LABEL "UID:"
  21. #define PWD_LABEL "PWD:"
  22. #define UID_MAX_LEN 12
  23. #define PWD_MAX_LEN 12
  24. String uid;
  25. String password;
  26. enum Status {
  27. Uid,
  28. Password,
  29. SelfTest
  30. };
  31. Status status = Uid;
  32. #define MAX_IDLE_TIME 20
  33. unsigned long lastActivity = 0;
  34. #define SELF_TEST_INTERVAL 60
  35. #define SELF_TEST_ERROR_INTERVAL 2
  36. #define MAX_SELF_TEST_TIME 5
  37. unsigned long lastSelfTest = 0;
  38. #define SELF_TEST_NO_ERROR '0'
  39. int lastSelfTestResult = SELF_TEST_NO_ERROR;
  40. void setup() {
  41. Serial.begin(9600);
  42. Serial.println("Starting...");
  43. lcd.begin(LCD_COLS, LCD_ROWS);
  44. lcd.blink();
  45. lcd.noAutoscroll();
  46. selfTest();
  47. }
  48. String makeChars(char c, int count) {
  49. char str[count + 1];
  50. memset(str, c, count);
  51. str[count] = 0;
  52. return String(str);
  53. }
  54. bool handleUidPassword(String& str, int maxLength, char key) {
  55. int len = str.length();
  56. if (key >= '0' && key <= '9') {
  57. if (len < maxLength) {
  58. str += key;
  59. return true;
  60. }
  61. }
  62. else if (key == 'C' || key == '*') {
  63. if (len > 0) {
  64. str = str.substring(0, len - 1);
  65. return true;
  66. }
  67. else if (status == Password) {
  68. status = Uid;
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. void loop() {
  75. unsigned long secs = millis() / 1000;
  76. if (handleKeyPad()) {
  77. lastActivity = secs;
  78. updateLcd();
  79. }
  80. if ((uid.length() != 0 || password.length() != 0) && secs - lastActivity >= MAX_IDLE_TIME) {
  81. askUidPassword();
  82. }
  83. if (uid.length() == 0 && password.length() == 0 && secs - lastSelfTest >= SELF_TEST_INTERVAL) {
  84. selfTest();
  85. }
  86. }
  87. bool handleKeyPad()
  88. {
  89. char key = keypad.getKey();
  90. if (key != NO_KEY) {
  91. if (status == Uid) {
  92. if (key == '#') {
  93. if (uid.length() != 0) {
  94. status = Password;
  95. return true;
  96. }
  97. }
  98. else {
  99. return handleUidPassword(uid, UID_MAX_LEN, key);
  100. }
  101. }
  102. else if (status == Password) {
  103. if (key == '#') {
  104. if (password.length() != 0) {
  105. login(uid, password);
  106. return true;
  107. }
  108. }
  109. else {
  110. return handleUidPassword(password, PWD_MAX_LEN, key);
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116. void updateLcd()
  117. {
  118. Serial.println("Update LCD");
  119. lcd.clear();
  120. if (status == Uid || status == Password)
  121. {
  122. lcd.setCursor(0, 0);
  123. lcd.print(UID_LABEL);
  124. lcd.print(uid);
  125. lcd.setCursor(0, 1);
  126. lcd.print(PWD_LABEL);
  127. lcd.print(makeChars('*', password.length()));
  128. if (status == Uid) {
  129. lcd.setCursor(strlen(UID_LABEL) + uid.length(), 0);
  130. }
  131. else {
  132. lcd.setCursor(strlen(PWD_LABEL) + password.length(), 1);
  133. }
  134. }
  135. else if (status == SelfTest) {
  136. lcd.print(SELF_TEST_LABEL);
  137. if (lastSelfTestResult != SELF_TEST_NO_ERROR) {
  138. lcd.setCursor(0, 1);
  139. lcd.print(SELF_TEST_ERROR_LABEL);
  140. lcd.print(lastSelfTestResult);
  141. }
  142. }
  143. }
  144. void askUidPassword()
  145. {
  146. status = Uid;
  147. uid = "";
  148. password = "";
  149. updateLcd();
  150. }
  151. void login(String uid, String password)
  152. {
  153. Serial.println(uid);
  154. Serial.println(password);
  155. askUidPassword();
  156. }
  157. void selfTest()
  158. {
  159. lastSelfTest = SELF_TEST_NO_ERROR;
  160. status = SelfTest;
  161. updateLcd();
  162. bool test = true;
  163. while (test) {
  164. unsigned long secs = millis() / 1000;
  165. Serial.println("Self Testing");
  166. while (Serial.available() == 0 && (millis() / 1000) - secs < MAX_SELF_TEST_TIME);
  167. if (Serial.available() > 0) {
  168. lastSelfTestResult = Serial.read();
  169. if (lastSelfTestResult == SELF_TEST_NO_ERROR) {
  170. test = false;
  171. }
  172. else {
  173. updateLcd();
  174. delay(SELF_TEST_ERROR_INTERVAL * 1000);
  175. }
  176. }
  177. }
  178. lastSelfTest = millis() / 1000;
  179. askUidPassword();
  180. }