Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <EEPROM.h>
  4.  
  5. struct Key
  6. {
  7. int row, col;
  8. };
  9.  
  10. // Keyboard pins
  11. int KeyBoard7 = 4;
  12. int KeyBoard6 = 5;
  13. int KeyBoard5 = 6;
  14. int KeyBoard4 = 7;
  15. int KeyBoard3 = 12;
  16. int KeyBoard2 = 8;
  17. int KeyBoard1 = 9;
  18.  
  19. //Misc Pins
  20. int setButton = A1;
  21. int ledGreen = A3;
  22. int ledYellow = A5;
  23. int ledRed = A4;
  24. int buzzerPin = A2;
  25. int solenoidPin = A0;
  26.  
  27. //Keyboard inits
  28. int c, r;
  29. int row[] = {KeyBoard2, KeyBoard3, KeyBoard5, KeyBoard7};
  30. int col[] = {KeyBoard1, KeyBoard4, KeyBoard6};
  31.  
  32. int code[4];
  33.  
  34. int KeyMap[4][3] = {
  35. {1, 2, 3},
  36. {4, 5, 6},
  37. {7, 8, 9},
  38. {11, 0, 12}};
  39.  
  40. int C = 11;
  41. int E = 12;
  42.  
  43. //Load Code From EEPROM
  44. void LoadCode()
  45. {
  46.  
  47. Serial.println("Loading Code");
  48. for (int i = 0; i < 4; i++)
  49. {
  50. code[i] = EEPROM.read(i);
  51. Serial.print(code[i]);
  52. }
  53. Serial.println();
  54. }
  55.  
  56. //Setup
  57. void setup()
  58. {
  59. // Serial
  60. Serial.begin(9600);
  61.  
  62. // Keypad pins
  63. pinMode(KeyBoard6, INPUT_PULLUP);
  64. pinMode(KeyBoard4, INPUT_PULLUP);
  65. pinMode(KeyBoard1, INPUT_PULLUP);
  66. pinMode(KeyBoard7, OUTPUT);
  67. pinMode(KeyBoard5, OUTPUT);
  68. pinMode(KeyBoard3, OUTPUT);
  69. pinMode(KeyBoard2, OUTPUT);
  70.  
  71. for (r = 0; r < 4; r++)
  72. {
  73. digitalWrite(row[r], HIGH);
  74. }
  75.  
  76. //Set LEdpins
  77. pinMode(ledGreen, OUTPUT);
  78. pinMode(ledYellow, OUTPUT);
  79. pinMode(ledRed, OUTPUT);
  80.  
  81. //Set pin
  82. pinMode(setButton, INPUT_PULLUP);
  83. pinMode(solenoidPin, OUTPUT);
  84.  
  85. digitalWrite(ledGreen, 0);
  86. digitalWrite(ledYellow, 0);
  87. digitalWrite(ledRed, 0);
  88.  
  89. //Load code
  90. LoadCode();
  91. }
  92.  
  93. //Reads Key Coords from Keypad
  94. Key keyPad()
  95. {
  96. for (r = 0; r < 4; r++)
  97. {
  98. digitalWrite(row[r], LOW);
  99. for (c = 0; c < 3; c++)
  100. {
  101. if (digitalRead(col[c]) == LOW)
  102. {
  103. digitalWrite(row[r], HIGH);
  104. return Key{r, c};
  105. }
  106. }
  107. digitalWrite(row[r], HIGH);
  108. }
  109. return Key{9, 9};
  110. }
  111.  
  112. //Reads Key Coords to 4 integers
  113. void readKeyPad(int keyInput[])
  114. {
  115. for (int i = 0; i < 4; i++)
  116. {
  117. keyInput[i] = 0;
  118. }
  119. Serial.println("Reading Code");
  120. Key key = keyPad();
  121. int wait = 1;
  122. int i1 = 0;
  123. while (i1 < 4)
  124. {
  125. key = keyPad();
  126. if (key.row != 9 && key.col != 9)
  127. {
  128. keyInput[i1] = KeyMap[key.row][key.col];
  129. if (keyInput[i1] == C){
  130. digitalWrite(ledRed, 1);
  131. delay(100);
  132. digitalWrite(ledRed, 0);
  133. break;
  134. }
  135. if (keyInput[i1] == E && wait == 0)
  136. {
  137.  
  138. wait = 1;
  139. }
  140. if (wait == 0)
  141. {
  142. wait = 1;
  143. digitalWrite(ledYellow, 1);
  144. Serial.print(i1);
  145. Serial.print(" ");
  146. Serial.print(keyInput[i1]);
  147. Serial.print(" ");
  148. Serial.print(wait);
  149. Serial.println();
  150. tone(buzzerPin,750);
  151. i1++;
  152. }
  153. }
  154. else if (wait == 1)
  155. {
  156. noTone(buzzerPin);
  157. delay(10);
  158. wait = 0;
  159. digitalWrite(ledYellow, 0);
  160. }
  161. }
  162.  
  163. digitalWrite(ledYellow, 0);
  164. for (int i = 0; i < 4; i++)
  165. {
  166. Serial.print(keyInput[i]);
  167. }
  168. Serial.println();
  169. noTone(buzzerPin);
  170. }
  171.  
  172. //Changes Code[] to keyInput and save it to EEPROM
  173. void changeCode()
  174. {
  175. Serial.println("Changing Code");
  176. int keyInput[4];
  177. readKeyPad(keyInput);
  178.  
  179. for (int i = 0; i < 4; i++)
  180. {
  181. EEPROM.write(i, keyInput[i]);
  182. Serial.print(keyInput[i]);
  183. }
  184. Serial.println();
  185.  
  186. LoadCode();
  187. }
  188.  
  189. //Compares keyInput with Code[]
  190. int checkCode()
  191. {
  192.  
  193. int keyInput[4];
  194. readKeyPad(keyInput);
  195. int correctCode = 1;
  196. for (int i = 0; i < 4; i++)
  197. {
  198. if (keyInput[i] != code[i])
  199. {
  200. correctCode = 0;
  201. }
  202. }
  203.  
  204. if (correctCode)
  205. {
  206.  
  207. Serial.println("Code is Correct");
  208. return 1;
  209. }
  210. else
  211. {
  212. Serial.println("Code is Incorrect");
  213. return 0;
  214. }
  215. }
  216.  
  217. //Unlocks Safe With Buzzer sounds
  218. void unlockSafe(int duration)
  219. {
  220. //Serial.println("00");
  221. digitalWrite(solenoidPin, HIGH);
  222. delay(duration);
  223. digitalWrite(solenoidPin, LOW);
  224. }
  225.  
  226. //Main Loop
  227. void loop()
  228. {
  229. Key key = keyPad();
  230. int keyChar;
  231.  
  232. if (key.row != 9 && key.col != 9)
  233. {
  234. //Serial.println(KeyMap[key.row][key.col]);
  235. keyChar = KeyMap[key.row][key.col];
  236. }
  237.  
  238. if (keyChar == E)
  239. {
  240. //tone(pin, frequency, duration)
  241. if (checkCode())
  242. {
  243. digitalWrite(ledGreen, 1);
  244. tone(buzzerPin, 1000);
  245. unlockSafe(2000);
  246. digitalWrite(ledGreen, 0);
  247. noTone(buzzerPin);
  248. }
  249. else
  250. {
  251. digitalWrite(ledRed, 1);
  252. tone(buzzerPin, 100);
  253. delay(100);
  254. digitalWrite(ledRed, 0);
  255. delay(100);
  256. digitalWrite(ledRed, 1);
  257. delay(100);
  258. digitalWrite(ledRed, 0);
  259. tone(buzzerPin, 100);
  260. delay(100);
  261. digitalWrite(ledRed, 1);
  262. delay(100);
  263. digitalWrite(ledRed, 0);
  264. delay(100);
  265. digitalWrite(ledRed, 1);
  266. delay(100);
  267. digitalWrite(ledRed, 0);
  268. delay(100);
  269. digitalWrite(ledRed, 1);
  270. delay(100);
  271. digitalWrite(ledRed, 0);
  272. delay(100);
  273. digitalWrite(ledRed, 1);
  274. delay(100);
  275. digitalWrite(ledRed, 0);
  276. noTone(buzzerPin);
  277. }
  278. }
  279.  
  280. if (!digitalRead(setButton))
  281. {
  282. changeCode();
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement