Advertisement
Fahim_7861

new code

Feb 15th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. byte data_count = 0, master_count = 0;
  15. bool Pass_is_good;
  16. char customKey;
  17.  
  18.  
  19. const byte ROWS=4;
  20. const byte COLS=4;
  21.  
  22. char keys[ROWS][COLS]={
  23.  
  24. {'1','2','3','A'},
  25.  
  26. {'4','5','6','B'},
  27.  
  28. {'7','8','9','C'},
  29.  
  30. {'*','0','#','D'},
  31.  
  32.  
  33. };
  34.  
  35. byte rowPins[ROWS]={9,8,7,6};
  36. byte colPins[COLS]={5,4,3,2};
  37. bool door = true;
  38.  
  39. //connect to the column pinouts of the keypad
  40.  
  41. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
  42.  
  43. void setup()
  44. {serial.begin(9600);
  45. myservo.attach(11);
  46. ServoClose();
  47.  
  48.  
  49.  
  50. }
  51.  
  52. void loop()
  53. {
  54.  
  55. if (door == 0)
  56. {
  57. customKey = customKeypad.getKey();
  58.  
  59. serial.println(customKey);
  60.  
  61. if (customKey == '#')
  62.  
  63. {
  64.  
  65. ServoClose();
  66.  
  67. door = 1;
  68. }
  69. }
  70.  
  71. else Open();
  72. }
  73.  
  74. void clearData()
  75. {
  76. while (data_count != 0)
  77. { // This can be used for any array size,
  78. Data[data_count--] = 0; //clear array for new data
  79. }
  80. return;
  81. }
  82.  
  83. void ServoOpen()
  84. {
  85. for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
  86. // in steps of 1 degree
  87. myservo.write(pos); // tell servo to go to position in variable 'pos'
  88. delay(15); // waits 15ms for the servo to reach the position
  89. }
  90. }
  91.  
  92. void ServoClose()
  93. {
  94. for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
  95. myservo.write(pos); // tell servo to go to position in variable 'pos'
  96. delay(15); // waits 15ms for the servo to reach the position
  97. }
  98. }
  99.  
  100. void Open()
  101. {
  102.  
  103.  
  104. customKey = customKeypad.getKey();
  105. if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
  106. {
  107. Data[data_count] = customKey; // store char into data array
  108.  
  109. data_count++; // increment data array by 1 to store new char, also keep track of the number of chars entered
  110. }
  111.  
  112. if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
  113. {
  114. if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)
  115. {
  116.  
  117. ServoOpen();
  118.  
  119. door = 0;
  120. }
  121. else
  122. {
  123.  
  124. delay(1000);
  125. door = 1;
  126. }
  127. clearData();
  128. }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement