Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.ino 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 - 2);
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. void loop() {
  71. unsigned long secs = millis() / 1000;
  72. if (handleKeyPad()) {
  73. lastActivity = secs;
  74. updateLcd();
  75. }
  76. if ((uid.length() != 0 || password.length() != 0) && secs - lastActivity >= MAX_IDLE_TIME) {
  77. askUidPassword();
  78. }
  79. if (uid.length() == 0 && password.length() == 0 && secs - lastSelfTest >= SELF_TEST_INTERVAL) {
  80. selfTest();
  81. }
  82. }
  83. bool handleKeyPad()
  84. {
  85. char key = keypad.getKey();
  86. if (key != NO_KEY) {
  87. if (status == Uid) {
  88. if (key == '#') {
  89. if (uid.length() != 0) {
  90. status = Password;
  91. return true;
  92. }
  93. }
  94. else {
  95. return handleUidPassword(uid, UID_MAX_LEN, key);
  96. }
  97. }
  98. else if (status == Password) {
  99. if (key == '#') {
  100. if (password.length() != 0) {
  101. login(uid, password);
  102. return true;
  103. }
  104. }
  105. else {
  106. return handleUidPassword(password, PWD_MAX_LEN, key);
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. void updateLcd()
  113. {
  114. Serial.println("Update LCD");
  115. lcd.clear();
  116. if (status == Uid || status == Password)
  117. {
  118. lcd.setCursor(0, 0);
  119. lcd.print(UID_LABEL);
  120. lcd.print(uid);
  121. lcd.setCursor(0, 1);
  122. lcd.print(PWD_LABEL);
  123. lcd.print(makeChars('*', password.length()));
  124. if (status == Uid) {
  125. lcd.setCursor(strlen(UID_LABEL) + uid.length(), 0);
  126. }
  127. else {
  128. lcd.setCursor(strlen(PWD_LABEL) + password.length(), 1);
  129. }
  130. }
  131. else if (status == SelfTest) {
  132. lcd.print(SELF_TEST_LABEL);
  133. if (lastSelfTestResult != SELF_TEST_NO_ERROR) {
  134. lcd.setCursor(0, 1);
  135. lcd.print(SELF_TEST_ERROR_LABEL);
  136. lcd.print(lastSelfTestResult);
  137. }
  138. }
  139. }
  140. void askUidPassword()
  141. {
  142. status = Uid;
  143. uid = "";
  144. password = "";
  145. updateLcd();
  146. }
  147. void login(String uid, String password)
  148. {
  149. Serial.println(uid);
  150. Serial.println(password);
  151. askUidPassword();
  152. }
  153. void selfTest()
  154. {
  155. lastSelfTest = SELF_TEST_NO_ERROR;
  156. status = SelfTest;
  157. updateLcd();
  158. bool test = true;
  159. while (test) {
  160. unsigned long secs = millis() / 1000;
  161. Serial.println("Self Testing");
  162. while (Serial.available() == 0 && (millis() / 1000) - secs < MAX_SELF_TEST_TIME);
  163. if (Serial.available() > 0) {
  164. lastSelfTestResult = Serial.read();
  165. if (lastSelfTestResult == SELF_TEST_NO_ERROR) {
  166. test = false;
  167. }
  168. else {
  169. updateLcd();
  170. delay(SELF_TEST_ERROR_INTERVAL * 1000);
  171. }
  172. }
  173. }
  174. lastSelfTest = millis() / 1000;
  175. askUidPassword();
  176. }