icstation

Infrared Matrix Password Input System with Arduino

Nov 18th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. I would like to introduce the Infrared Matrix Password Input System with Arduino, for more details:http://www.instructables.com/id/Infrared-Matrix-Password-Input-System-with-Arduino/
  2.  
  3. the code for this system as following:
  4.  
  5. #include <LiquidCrystal.h> //LCD library header files
  6. #include <IRremote.h>//Infrared library header files
  7. #include <Password.h> //Matrix keyboard password library header files
  8. #include <Keypad.h> //Matrix keyboard library header files
  9. #define uchar unsigned char
  10. #define uint unsigned int
  11. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//Define the LCD1602 pin connection
  12. Password password = Password( "1234" );//Set up the matrix keyboard password
  13.  
  14. int RECV_PIN = 7;//Infrared receiver tube feet
  15. const byte ROWS = 4; // Four rows
  16. const byte COLS = 4; // columns
  17. char keys[ROWS][COLS] =
  18. {
  19. {'1','2','3','A'},
  20. {'4','5','6','B'},
  21. {'7','8','9','C'},
  22. {'*','0','#','D'}
  23. };// Create the Keypad
  24.  
  25. byte rowPins[ROWS] = { 31,33,35,37};// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  26. byte colPins[COLS] = { 39,41,43,45};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  27.  
  28. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  29. //Initializes the internal keymap to be equal to userKeymap
  30.  
  31. IRrecv irrecv(RECV_PIN);//Define a receiver input pin
  32.  
  33. decode_results results;//Save the decoding of variables results
  34.  
  35. int i=0;
  36. long code_1,code_2,code_3,code_4,code_5;
  37.  
  38.  
  39. void setup()
  40. {
  41. lcd.begin(16, 2);
  42. lcd.setCursor(4,0);
  43. lcd.print("welcome");
  44. lcd.setCursor(4,1);
  45. irrecv.enableIRIn(); // Start the receiver
  46. keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  47. }
  48.  
  49. void loop()
  50. {
  51. keypad.getKey();
  52. infrared();
  53. }
  54.  
  55. void infrared()//Infrared password input
  56. {
  57. if (irrecv.decode(&results)) //Decoding is successful, the data in the results variables
  58. {
  59. if(results.value!=4294967295)//Whether the infrared remote control has been hold
  60. {
  61. if(i==0)
  62. {
  63. code_1=results.value;
  64. infrareddisplay(code_1);//LCD display the corresponding infrared buttons
  65. i=1;
  66. }
  67. else if(i==1)
  68. {
  69. code_2=results.value;
  70. infrareddisplay(code_2);
  71. i=2;
  72. }
  73. else if(i==2)
  74. {
  75. code_3=results.value;
  76. infrareddisplay(code_3);
  77. i=3;
  78. }
  79. else if(i==3)
  80. {
  81. code_4=results.value;
  82. infrareddisplay(code_4);
  83. i=4;
  84. }
  85. else if(i==4)
  86. {
  87. if(results.value==16748655)
  88. {
  89. infrared_password();
  90. }
  91. i=0;
  92. lcd.clear();
  93. lcd.setCursor(4,0);
  94. lcd.print("welcome");
  95. lcd.setCursor(4,1);
  96. }
  97. }
  98. irrecv.resume(); // Receive the next value
  99. }
  100. }
  101.  
  102. void infrared_password()//Infrared password comparison
  103. {
  104. if(code_1==16724175)//The first value is 1
  105. {
  106. if(code_2==16718055)//The second value of 2
  107. {
  108. if(code_3==16743045)//The third value is 3
  109. {
  110. if(code_4==16716015)//The fourth value is 4
  111. {
  112. lcd.clear();
  113. lcd.setCursor(4,0);
  114. lcd.print("Success!");
  115. delay(2000);
  116. lcd.clear();
  117. lcd.setCursor(4,0);
  118. lcd.print("welcome");
  119. lcd.setCursor(4,1);
  120. }
  121. else
  122. {
  123. lcd.clear();
  124. lcd.setCursor(4,0);
  125. lcd.print("Wrong");
  126. delay(2000);
  127. lcd.clear();
  128. lcd.setCursor(4,0);
  129. lcd.print("welcome");
  130. lcd.setCursor(4,1);
  131. }
  132. }
  133. else
  134. {
  135. lcd.clear();
  136. lcd.setCursor(4,0);
  137. lcd.print("Wrong");
  138. delay(2000);
  139. lcd.clear();
  140. lcd.setCursor(4,0);
  141. lcd.print("welcome");
  142. lcd.setCursor(4,1);
  143. }
  144. }
  145. else
  146. {
  147. lcd.clear();
  148. lcd.setCursor(4,0);
  149. lcd.print("Wrong");
  150. delay(2000);
  151. lcd.clear();
  152. lcd.setCursor(4,0);
  153. lcd.print("welcome");
  154. lcd.setCursor(4,1);
  155. }
  156. }
  157. else
  158. {
  159. lcd.clear();
  160. lcd.setCursor(4,0);
  161. lcd.print("Wrong");
  162. delay(2000);
  163. lcd.clear();
  164. lcd.setCursor(4,0);
  165. lcd.print("welcome");
  166. lcd.setCursor(4,1);
  167. }
  168. }
  169. void infrareddisplay(long x)//LCD display the corresponding infrared buttons
  170. {
  171. if(x==16738455)
  172. {
  173. lcd.print('0');
  174. }
  175. if(x==16724175)
  176. {
  177. lcd.print('1');
  178. }
  179. if(x==16718055)
  180. {
  181. lcd.print('2');
  182. }
  183. if(x==16743045)
  184. {
  185. lcd.print('3');
  186. }
  187. if(x==16716015)
  188. {
  189. lcd.print('4');
  190. }
  191. if(x==16726215)
  192. {
  193. lcd.print('5');
  194. }
  195. if(x==16734885)
  196. {
  197. lcd.print('6');
  198. }
  199. if(x==16728765)
  200. {
  201. lcd.print('7');
  202. }
  203. if(x==16730805)
  204. {
  205. lcd.print('8');
  206. }
  207. if(x==16732845)
  208. {
  209. lcd.print('9');
  210. }
  211. if(x==16748655)
  212. {
  213. infrared_password();
  214. }
  215. }
  216.  
  217. void keypadEvent(KeypadEvent eKey)//Matrix keyboard password
  218. {
  219. switch (keypad.getState())
  220. {
  221. case PRESSED:
  222. lcd.print(eKey);
  223. switch (eKey)
  224. {
  225. case '#': checkPassword(); break;
  226. case '*': password.reset(); break;
  227. default: password.append(eKey);
  228. }
  229. }
  230. }
  231.  
  232. void checkPassword()//Matrix keyboard password display
  233. {
  234. if (password.evaluate())
  235. {
  236. lcd.clear();
  237. lcd.setCursor(5,0);
  238. lcd.print("Success!");
  239. delay(2000);
  240. lcd.clear();
  241. lcd.setCursor(5,0);
  242. lcd.print("welcome");
  243. lcd.setCursor(5,1);
  244. }
  245. else
  246. {
  247. lcd.clear();
  248. lcd.setCursor(5,0);
  249. lcd.print("Wrong");
  250. delay(2000);
  251. lcd.clear();
  252. lcd.setCursor(5,0);
  253. lcd.print("welcome");
  254. lcd.setCursor(5,1);
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment