Fahim_7861

cpi fina code 2

Feb 18th, 2021 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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 // Give enough room for six chars + NULL char
  9.  
  10. int pos = 0; // variable to store the servo position
  11.  
  12. char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
  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. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  19. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  20. int c=0;
  21.  
  22. bool cpress=false;
  23.  
  24. int i=0;
  25.  
  26. const byte ROWS=4;
  27. const byte COLS=4;
  28.  
  29. char keys[ROWS][COLS]={
  30.  
  31. {'1','2','3','A'},
  32.  
  33. {'4','5','6','B'},
  34.  
  35. {'7','8','9','C'},
  36.  
  37. {'*','0','#','D'},
  38.  
  39.  
  40. };
  41.  
  42. byte rowPins[ROWS]={9,8,7,6};
  43. byte colPins[COLS]={5,4,3,2};
  44. bool door = true;
  45.  
  46. int e=0;
  47.  
  48. //connect to the column pinouts of the keypad
  49.  
  50. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
  51.  
  52. void setup()
  53. {
  54. Serial.begin(9600);
  55. myservo.attach(11);
  56. ServoClose();
  57. cpress=false;
  58. lcd.begin(16,2);
  59.  
  60. c=0;
  61. e=0;
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68. void loop()
  69. {
  70. // char customKey = customKeypad.getKey();
  71.  
  72. // if(customKey)Serial.println(customKey);
  73. lcd.print("hello, world!");
  74. if (door == 0)
  75. {
  76. char customKey = customKeypad.getKey();
  77.  
  78. if(customKey)Serial.println(customKey);
  79.  
  80. if (customKey == '#')
  81.  
  82. {
  83.  
  84. ServoClose();
  85.  
  86. door = 1;
  87. }
  88. }
  89.  
  90. else Open();
  91. }
  92.  
  93. void clearData()
  94. {
  95. while (data_count != 0)
  96. { // This can be used for any array size,
  97. Data[data_count--] = 0; //clear array for new data
  98. }
  99. cpress=false;
  100. c=0;
  101. return;
  102. }
  103.  
  104. void ServoOpen()
  105. {
  106. for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
  107. // in steps of 1 degree
  108. myservo.write(pos); // tell servo to go to position in variable 'pos'
  109. delay(15); // waits 15ms for the servo to reach the position
  110. }
  111. cpress=false;
  112. c=0;
  113. }
  114.  
  115. void ServoClose()
  116. {
  117. for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
  118. myservo.write(pos); // tell servo to go to position in variable 'pos'
  119. delay(15); // waits 15ms for the servo to reach the position
  120. }
  121. cpress=false;
  122. c=0;
  123.  
  124.  
  125. }
  126.  
  127. void Open()
  128. {
  129.  
  130.  
  131. char customKey = customKeypad.getKey();
  132.  
  133.  
  134.  
  135. if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  136. {
  137. Serial.println(customKey);
  138.  
  139. if(customKey == 'C')
  140. {
  141. //Serial.println(535665);
  142. c=0;
  143. cpress=true;
  144. Serial.println(0);
  145. e=0;
  146.  
  147. }
  148.  
  149. else if(cpress)
  150. {
  151. current[c] = customKey; // store char into data array
  152.  
  153.  
  154. EEPROM.write(c++, customKey);
  155.  
  156.  
  157.  
  158. if(c==6)
  159. {
  160. c=0;
  161. cpress=false;
  162. Serial.println(current);
  163. }
  164.  
  165.  
  166. Serial.println(1);
  167. }
  168.  
  169. else {
  170. Data[data_count] = customKey; // store char into data array
  171.  
  172. Serial.println(2);
  173.  
  174. data_count++;
  175. }// increment data array by 1 to store new char, also keep track of the number of chars entered
  176. }
  177.  
  178. if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  179. {
  180. Serial.println(Master);
  181. Serial.println(Data);
  182.  
  183.  
  184.  
  185. for(int i=0; i<6; i++)
  186. {
  187. current[i]=EEPROM.read(i);
  188. }
  189.  
  190. Serial.println(current);
  191.  
  192.  
  193.  
  194.  
  195. if (!strcmp(Data, Master) || !strcmp(Data, current)) // equal to (strcmp(Data, Master) == 0)
  196. {
  197.  
  198. ServoOpen();
  199.  
  200. door = 0;
  201. }
  202. else
  203. {
  204.  
  205. delay(1000);
  206. door = 1;
  207. }
  208. clearData();
  209. }
  210. }
Add Comment
Please, Sign In to add comment