Advertisement
safwan092

+966540842818

Aug 28th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <Keypad.h>
  3.  
  4. Servo myservo;
  5.  
  6. const int ROW_NUM = 4; //four rows
  7. const int COLUMN_NUM = 4; //three columns
  8.  
  9. char keys[ROW_NUM][COLUMN_NUM] = {
  10. {'1', '2', '3', 'A'},
  11. {'4', '5', '6', 'B'},
  12. {'7', '8', '9', 'C'},
  13. {'*', '0', '#', 'D'}
  14. };
  15.  
  16. byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
  17. byte pin_column[COLUMN_NUM] = {5, 4, 3 , 2}; //connect to the column pinouts of the keypad
  18.  
  19. Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
  20.  
  21. const String password_1 = "1234"; // change your password here
  22. const String password_2 = "5642"; // change your password here
  23. const String password_3 = "4545"; // change your password here
  24. String input_password;
  25.  
  26. //*********************************************************************
  27. //||||||||||||||||||| Flags Setup to Know Password Status |||||||||||||
  28. //*********************************************************************
  29. int flag_p1 = 0; // 0 = password not entered yet
  30. int flag_p2 = 0; // 1 = password was entered
  31. int flag_p3 = 0;
  32. //*********************************************************************
  33.  
  34.  
  35. void setup() {
  36. Serial.begin(9600);
  37. input_password.reserve(5);
  38. myservo.attach(10); // maximum input characters is 5, change if needed
  39.  
  40. //*********************************************************************
  41. //||||||||||||||||||| Setting Flags to 0 when Restart |||||||||||||||||
  42. //*********************************************************************
  43. flag_p1 = 0;
  44. flag_p2 = 0;
  45. flag_p3 = 0;
  46. //*********************************************************************
  47. }
  48.  
  49. void loop() {
  50. char key = keypad.getKey();
  51.  
  52. if (key) {
  53. Serial.println(key);
  54.  
  55. if (key == 'C') {
  56. input_password = ""; // reset the input password
  57. }
  58. else if (key == '*') {
  59. //*********************************************************************
  60. //||||||||||||||||||| Conditions for Password #1 ||||||||||||||||||||||
  61. //*********************************************************************
  62. if (input_password == password_1 && flag_p1 == 0) {
  63. flag_p1 = 1;
  64. Serial.println("password is correct");
  65. runServo();
  66. }
  67. else if (input_password == password_1 && flag_p1 == 1) {
  68. Serial.println("password entered before, try diffrent password");
  69. }
  70. //*********************************************************************
  71. //||||||||||||||||||| Conditions for Password #2 ||||||||||||||||||||||
  72. //*********************************************************************
  73. else if (input_password == password_2 && flag_p2 == 0) {
  74. flag_p2 = 1;
  75. Serial.println("password is correct");
  76. runServo();
  77. }
  78. else if (input_password == password_2 && flag_p2 == 1) {
  79. Serial.println("password entered before, try diffrent password");
  80. }
  81. //*********************************************************************
  82. //||||||||||||||||||| Conditions for Password #3 ||||||||||||||||||||||
  83. //*********************************************************************
  84. else if (input_password == password_3 && flag_p3 == 0) {
  85. flag_p3 = 1;
  86. Serial.println("password is correct");
  87. runServo();
  88. }
  89. else if (input_password == password_3 && flag_p3 == 1) {
  90. Serial.println("password entered before, try diffrent password");
  91. }
  92. //*********************************************************************
  93. //|||||||||||||||||| Condition to reset All Flags |||||||||||||||||||||
  94. //*********************************************************************
  95. else if (input_password == "ABAD") {
  96. Serial.println("All Flags Reset");
  97. flag_p1 = 0;
  98. flag_p2 = 0;
  99. flag_p3 = 0;
  100. }
  101. //*********************************************************************
  102. //|||||||||||||||||| Condition for Wrong Password |||||||||||||||||||||
  103. //*********************************************************************
  104. else {
  105. Serial.println("password is incorrect, try again");
  106. }
  107. //*********************************************************************
  108.  
  109. input_password = ""; // reset the input password
  110. }
  111. else {
  112. input_password += key; // append new character to input password string
  113. }
  114. }
  115.  
  116. }//end of Loop
  117.  
  118.  
  119. //*********************************************************************
  120. //|||||||||||||||||| Function to run the Servo ||||||||||||||||||||||||
  121. //*********************************************************************
  122. void runServo() {
  123. Serial.println("ON");
  124. myservo.write(0);
  125. delay(3000);
  126. myservo.attach(10);
  127. delay(3000);
  128. myservo.detach();//Stop
  129. delay(2000);
  130. }
  131. //*********************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement