Advertisement
Fahim_7861

CPI final code

Feb 15th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <Servo.h>
  3.  
  4. Servo myservo;
  5.  
  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.  
  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. //connect to the column pinouts of the keypad
  46.  
  47. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
  48.  
  49. void setup()
  50. {
  51. Serial.begin(9600);
  52. myservo.attach(11);
  53. ServoClose();
  54. cpress=false;
  55. c=0;
  56.  
  57.  
  58.  
  59. }
  60.  
  61. void loop()
  62. {
  63. // char customKey = customKeypad.getKey();
  64.  
  65. // if(customKey)Serial.println(customKey);
  66.  
  67. if (door == 0)
  68. {
  69. char customKey = customKeypad.getKey();
  70.  
  71. if(customKey)Serial.println(customKey);
  72.  
  73. if (customKey == '#')
  74.  
  75. {
  76.  
  77. ServoClose();
  78.  
  79. door = 1;
  80. }
  81. }
  82.  
  83. else Open();
  84. }
  85.  
  86. void clearData()
  87. {
  88. while (data_count != 0)
  89. { // This can be used for any array size,
  90. Data[data_count--] = 0; //clear array for new data
  91. }
  92. cpress=false;
  93. c=0;
  94. return;
  95. }
  96.  
  97. void ServoOpen()
  98. {
  99. for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
  100. // in steps of 1 degree
  101. myservo.write(pos); // tell servo to go to position in variable 'pos'
  102. delay(15); // waits 15ms for the servo to reach the position
  103. }
  104. cpress=false;
  105. c=0;
  106. }
  107.  
  108. void ServoClose()
  109. {
  110. for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
  111. myservo.write(pos); // tell servo to go to position in variable 'pos'
  112. delay(15); // waits 15ms for the servo to reach the position
  113. }
  114. cpress=false;
  115. c=0;
  116.  
  117.  
  118. }
  119.  
  120. void Open()
  121. {
  122.  
  123.  
  124. char customKey = customKeypad.getKey();
  125.  
  126.  
  127.  
  128. if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  129. {
  130. Serial.println(customKey);
  131.  
  132. if(customKey == 'C')
  133. {
  134. //Serial.println(535665);
  135. c=0;
  136. cpress=true;
  137. Serial.println(0);
  138.  
  139. }
  140.  
  141. else if(cpress)
  142. {
  143. current[c] = customKey; // store char into data array
  144.  
  145.  
  146. c++;
  147.  
  148. if(c==6)
  149. {
  150. c=0;
  151. cpress=false;
  152. Serial.println(current);
  153. }
  154.  
  155.  
  156. Serial.println(1);
  157. }
  158.  
  159. else {
  160. Data[data_count] = customKey; // store char into data array
  161.  
  162. Serial.println(2);
  163.  
  164. data_count++;
  165. }// increment data array by 1 to store new char, also keep track of the number of chars entered
  166. }
  167.  
  168. if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  169. {
  170. Serial.println(Master);
  171. Serial.println(Data);
  172. Serial.println(current);
  173. if (!strcmp(Data, Master) || !strcmp(Data, current)) // equal to (strcmp(Data, Master) == 0)
  174. {
  175.  
  176. ServoOpen();
  177.  
  178. door = 0;
  179. }
  180. else
  181. {
  182.  
  183. delay(1000);
  184. door = 1;
  185. }
  186. clearData();
  187. }
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement