Advertisement
pleasedontcode

**Door Lock** rev_78

Oct 5th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Door Lock**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-06 00:20:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The servo motor installed in the door will lock or */
  21.     /* unlock based on the correct PIN code received by */
  22.     /* the button. If the door is unlocked and the PIN is */
  23.     /* correct, it will lock. If the door is locked and */
  24.     /* the PIN is correct, it will unlock. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The webserver provides a web page about lock or */
  27.     /* unlock state of the door. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Deneyap_Servo.h>  // https://github.com/deneyapkart/deneyap-servo-arduino-library
  32. #include <Keypad.h>         // https://github.com/Chris--A/Keypad
  33. #include <EasyButton.h>     // https://github.com/evert-arias/EasyButton
  34. #include <LcdKeypad.h>      // https://github.com/dniklaus/arduino-display-lcdkeypad
  35. #include "DoorLockSystem.h"  // Include the header for the door lock system
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  43. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  44. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  45. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  46. const uint8_t pinButton_PushButton_PIN_D2 = 2;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  50. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  51. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  52.  
  53. // Define row and column pins for the keypad
  54. byte rowPins[KEYPAD3X4_ROWS] = { keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7 };
  55. byte colPins[KEYPAD3X4_COLS] = { keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10 };
  56.  
  57. void setup(void) {
  58.     // Attach the servo to the specified pin
  59.     doorServo.attach(3);  // Assuming the servo is connected to pin 3
  60.  
  61.     // Initialize the keypad
  62.     keypad.begin();
  63.  
  64.     // Initialize the button
  65.     button.begin();
  66.  
  67.     // Initialize the LCD keypad
  68.     lcdKeypad.begin(16, 2);
  69.     lcdKeypad.clear();
  70.     lcdKeypad.print("Enter PIN:");
  71. }
  72.  
  73. void loop(void) {
  74.     // Check if the button is pressed
  75.     button.update();
  76.     if (button.read()) {
  77.         // Read the entered PIN from the keypad
  78.         String enteredPIN = "";
  79.         char key;
  80.         while (true) {
  81.             key = keypad.getKey();
  82.             if (key) {
  83.                 enteredPIN += key;
  84.                 lcdKeypad.print(key);
  85.             }
  86.             // Check if the Enter key is pressed
  87.             if (key == '#') {
  88.                 break;
  89.             }
  90.         }
  91.         // Check the entered PIN
  92.         if (checkPIN(enteredPIN)) {
  93.             toggleLock();
  94.             lcdKeypad.clear();
  95.             lcdKeypad.print(isLocked ? "Locked" : "Unlocked");
  96.         } else {
  97.             lcdKeypad.clear();
  98.             lcdKeypad.print("Wrong PIN");
  99.         }
  100.     }
  101.     // Add a small delay to avoid bouncing
  102.     delay(100);
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement