Advertisement
Fahim_7861

FINAL CPI CODE

Feb 22nd, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <Servo.h>
  3. #include <EEPROM.h>
  4. #include <LiquidCrystal.h>
  5. Servo myservo;
  6.  
  7.  
  8. #define Password_Lenght 7
  9.  
  10. int pos = 0;
  11.  
  12. char Data[Password_Lenght];
  13. char Master[Password_Lenght] = "123456";
  14.  
  15. char current[Password_Lenght];
  16. byte data_count = 0, master_count = 0;
  17. bool Pass_is_good;
  18. LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
  19. int c=0;
  20.  
  21. bool cpress=false;
  22.  
  23. int i=0;
  24.  
  25. const byte ROWS=4;
  26. const byte COLS=4;
  27.  
  28. char keys[ROWS][COLS]={
  29.  
  30. {'1','2','3','A'},
  31.  
  32. {'4','5','6','B'},
  33.  
  34. {'7','8','9','C'},
  35.  
  36. {'*','0','#','D'},
  37.  
  38.  
  39. };
  40.  
  41. byte rowPins[ROWS]={9,8,7,6};
  42. byte colPins[COLS]={5,4,3,2};
  43. bool door = true;
  44.  
  45. int e=0;
  46.  
  47. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  48.  
  49.  
  50. void instruction()
  51. {
  52. lcd.clear();
  53.  
  54. lcd.print("Assalamualaikum");
  55.  
  56. delay(5000);
  57.  
  58. lcd.clear();
  59.  
  60.  
  61. lcd.setCursor(2,0);
  62. lcd.print("Instruction");
  63.  
  64. delay(2000);
  65.  
  66. lcd.clear();
  67.  
  68. lcd.setCursor(0,0);
  69.  
  70. lcd.print("To set or Change");
  71.  
  72. lcd.setCursor(0,1);
  73.  
  74. lcd.print("Password Press C");
  75.  
  76. delay(5000);
  77.  
  78. lcd.clear();
  79.  
  80. lcd.setCursor(1,0);
  81.  
  82. lcd.print("Enter Password");
  83. }
  84. void setup()
  85. {
  86. Serial.begin(9600);
  87. myservo.attach(11);
  88. ServoClose();
  89. cpress=false;
  90. lcd.begin(16,2);
  91.  
  92. instruction();
  93.  
  94.  
  95. c=0;
  96. e=0;
  97.  
  98.  
  99.  
  100.  
  101. }
  102.  
  103. void loop()
  104. {
  105.  
  106.  
  107.  
  108. if (door == 0)
  109. {
  110.  
  111. char customKey = customKeypad.getKey();
  112.  
  113. if(customKey)Serial.println(customKey);
  114.  
  115. if (customKey == '#')
  116. {
  117.  
  118.  
  119. ServoClose();
  120.  
  121. lcd.clear();
  122.  
  123. lcd.print("Door is Closed");
  124.  
  125. delay(2000);
  126.  
  127. lcd.clear();
  128.  
  129. lcd.setCursor(1,0);
  130.  
  131. lcd.print("Enter Password");
  132.  
  133. door = 1;
  134. }
  135.  
  136.  
  137. }
  138.  
  139. else {
  140.  
  141.  
  142. Open();
  143. }
  144.  
  145.  
  146.  
  147. }
  148.  
  149.  
  150.  
  151. void clearData()
  152. {
  153. while (data_count != 0)
  154. {
  155. Data[data_count--] = 0;
  156. }
  157. cpress=false;
  158. c=0;
  159. return;
  160. }
  161.  
  162. void ServoOpen()
  163. {
  164. for (pos = 180; pos >= 0; pos -= 5) {
  165. // in steps of 1 degree
  166. myservo.write(pos);
  167. delay(15);
  168. }
  169. cpress=false;
  170. c=0;
  171. }
  172.  
  173. void ServoClose()
  174. {
  175. for (pos = 0; pos <= 180; pos += 5) {
  176. myservo.write(pos);
  177. delay(5);
  178. }
  179. cpress=false;
  180. c=0;
  181.  
  182.  
  183. }
  184.  
  185. int master_key()
  186. {
  187. char str[7];
  188.  
  189.  
  190.  
  191.  
  192. for(int i=0; i<6;)
  193. {
  194. char customKey = customKeypad.getKey();
  195.  
  196. if(customKey)
  197. {
  198.  
  199. if(customKey=='B')
  200. {
  201. if(i>0)
  202. i--;
  203.  
  204. lcd.clear();
  205.  
  206.  
  207.  
  208. lcd.print("Enter Master Key");
  209.  
  210.  
  211.  
  212.  
  213. }
  214.  
  215. else{
  216.  
  217. str[i]= customKey;
  218.  
  219.  
  220.  
  221. i++;
  222.  
  223. }
  224.  
  225. lcd.setCursor(5,1);
  226. for(int c=0; c<i; c++)
  227. {
  228. lcd.print("*");
  229. }
  230.  
  231.  
  232. }
  233. }
  234.  
  235. str[6]='\0';
  236.  
  237.  
  238.  
  239. if(strcmp(str, Master)==0)
  240. return 1;
  241.  
  242. else return 0;
  243. }
  244.  
  245. void Open()
  246. {
  247. char customKey = customKeypad.getKey();
  248. if (customKey)
  249. {
  250. Serial.println(customKey);
  251.  
  252. if(customKey=='A'){
  253. instruction();
  254. }
  255. else if(customKey == 'C')
  256. {
  257.  
  258. lcd.clear();
  259.  
  260. lcd.print("Enter Master Key");
  261.  
  262.  
  263.  
  264. if(master_key())
  265. {
  266. c=0;
  267. cpress=true;
  268. Serial.println(0);
  269. e=0;
  270.  
  271.  
  272. lcd.clear();
  273. lcd.setCursor(1,0);
  274.  
  275. lcd.print("Enter New Pass");
  276. }
  277. else
  278. {
  279. lcd.clear();
  280.  
  281. lcd.print("Wrong Password");
  282.  
  283. delay(2000);
  284.  
  285. lcd.clear();
  286.  
  287. lcd.setCursor(1,0);
  288.  
  289. lcd.print("Enter Password");
  290.  
  291.  
  292.  
  293. }
  294.  
  295.  
  296.  
  297. }
  298.  
  299. else if(cpress)
  300. {
  301.  
  302. if(customKey=='B')
  303. {
  304. if(c>0)
  305. c--;
  306.  
  307. lcd.clear();
  308.  
  309. lcd.setCursor(1,0);
  310.  
  311. lcd.print("Enter New Pass");
  312.  
  313. lcd.setCursor(5,1);
  314.  
  315. for(int i=0; i<c; i++)
  316. {
  317. lcd.print("*");
  318. }
  319.  
  320. }
  321.  
  322. else{
  323. current[c] = customKey;
  324.  
  325.  
  326. EEPROM.write(c++, customKey);
  327.  
  328. lcd.setCursor(5,1);
  329.  
  330. for(int i=0; i<c; i++)
  331. {
  332. lcd.print("*");
  333. }
  334.  
  335.  
  336.  
  337. if(c==6)
  338. {
  339. c=0;
  340. cpress=false;
  341. Serial.println(current);
  342.  
  343. lcd.clear();
  344.  
  345. lcd.setCursor(2,0);
  346.  
  347. lcd.print("Successfully");
  348.  
  349.  
  350. lcd.setCursor(6,1);
  351.  
  352. lcd.print("Done");
  353.  
  354. delay(2000);
  355.  
  356. lcd.clear();
  357.  
  358. lcd.setCursor(1,0);
  359.  
  360. lcd.print("Enter Password");
  361. }
  362.  
  363.  
  364. Serial.println(1);
  365. }
  366. }
  367.  
  368. else {
  369.  
  370. if(customKey=='B')
  371. {
  372. if(data_count>0)
  373. data_count--;
  374.  
  375. lcd.clear();
  376.  
  377. lcd.setCursor(1,0);
  378.  
  379. lcd.print("Enter Password");
  380.  
  381.  
  382.  
  383.  
  384. }
  385.  
  386. else{
  387.  
  388. Data[data_count] = customKey;
  389.  
  390. // Serial.println(2);
  391.  
  392. data_count++;
  393. }
  394.  
  395.  
  396.  
  397.  
  398.  
  399. lcd.setCursor(5,1);
  400.  
  401. for(int i=0; i<data_count; i++)
  402. {
  403. lcd.print("*");
  404. }
  405.  
  406.  
  407. }
  408. }
  409.  
  410. if (data_count == Password_Lenght - 1)
  411. {
  412. Serial.println(Master);
  413. Serial.println(Data);
  414.  
  415.  
  416.  
  417. for(int i=0; i<6; i++)
  418. {
  419. current[i]=EEPROM.read(i);
  420. }
  421.  
  422. Serial.println(current);
  423.  
  424.  
  425.  
  426.  
  427. if (!strcmp(Data, Master) || !strcmp(Data, current))
  428. {
  429. lcd.clear();
  430.  
  431. lcd.print("Press # to close");
  432.  
  433. ServoOpen();
  434.  
  435. door = 0;
  436. }
  437. else
  438. {
  439. lcd.clear();
  440.  
  441. lcd.print("Wrong Password");
  442.  
  443. delay(2000);
  444.  
  445. lcd.clear();
  446.  
  447. lcd.print("Enter Password");
  448.  
  449.  
  450.  
  451. door = 1;
  452. }
  453. clearData();
  454. }
  455. }
  456.  
  457.  
  458.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement