Advertisement
pleasedontcode

**Title:** Lock System rev_62

Oct 5th, 2024
83
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: **Title:** Lock System
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 14:23:35
  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.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - add a physical button to enable introduction of pin on keypad
  34. ********* User code review feedback **********/
  35.  
  36. /****** DEFINITION OF LIBRARIES *****/
  37. #include <Deneyap_Servo.h>  // Library for controlling servo motors
  38. #include <Keypad.h> // Library for keypad input
  39. #include <WiFi.h>       // Library for WiFi connectivity
  40. #include <WebServer.h>  // Library for web server functionality
  41. #include "ButtonHandler.h" // Include the button handler header
  42.  
  43. /****** FUNCTION PROTOTYPES *****/
  44. void setup(void);
  45. void loop(void);
  46. void handleRoot(); // Function to handle root URL
  47.  
  48. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  49. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  50. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  51. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  52. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  53. const uint8_t buttonPin = 2; // Define the pin for the button
  54.  
  55. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  56. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  57. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  58. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  59.  
  60. /***** DEFINITION OF KEYPAD VARIABLES *****/
  61. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  62. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  63. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  64.     {'1','2','3'},
  65.     {'4','5','6'},
  66.     {'7','8','9'},
  67.     {'*','0','#'},
  68. };
  69.  
  70. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  71. Servo doorServo; // Servo object for door lock
  72. WebServer server(80); // Create a web server on port 80
  73. ButtonHandler pinButton(buttonPin); // Create an instance of ButtonHandler
  74.  
  75. // Define the correct PIN code
  76. const String correctPIN = "1234"; // Example PIN code
  77. String enteredPIN = ""; // Variable to store entered PIN
  78. bool isLocked = true; // Initial state of the door
  79. bool inputEnabled = false; // State to track if input is enabled
  80.  
  81. void setup(void)
  82. {
  83.     // Initialize the servo motor
  84.     doorServo.attach(9); // Attach the servo to pin 9
  85.     doorServo.write(isLocked ? 0 : 90); // Lock the door initially
  86.  
  87.     // Setup keypad pins
  88.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  89.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  90.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  91.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  92.    
  93.     // Initialize the button
  94.     pinButton.begin();
  95.  
  96.     // Setup WiFi connection
  97.     WiFi.begin("yourSSID", "yourPASSWORD"); // Replace with your WiFi credentials
  98.     while (WiFi.status() != WL_CONNECTED) {
  99.         delay(1000);
  100.     }
  101.    
  102.     // Start the web server
  103.     server.on("/", handleRoot); // Handle root URL
  104.     server.begin(); // Start the server
  105. }
  106.  
  107. void loop(void)
  108. {
  109.     // Check if the button is pressed
  110.     if (pinButton.isPressed()) {
  111.         inputEnabled = !inputEnabled; // Toggle input mode
  112.         delay(300); // Debounce delay
  113.     }
  114.  
  115.     // Check for keypad input only if input is enabled
  116.     if (inputEnabled) {
  117.         char key = keypad.getKey();
  118.         if (key) {
  119.             if (key == '#') { // If '#' is pressed, check the entered PIN
  120.                 if (enteredPIN == correctPIN) {
  121.                     // Toggle lock state
  122.                     isLocked = !isLocked;
  123.                     doorServo.write(isLocked ? 0 : 90); // Lock or unlock the door
  124.                 }
  125.                 enteredPIN = ""; // Reset entered PIN
  126.             } else if (key == '*') { // If '*' is pressed, reset the entered PIN
  127.                 enteredPIN = "";
  128.             } else {
  129.                 enteredPIN += key; // Append the key to the entered PIN
  130.             }
  131.         }
  132.     }
  133.  
  134.     // Handle web server requests
  135.     server.handleClient();
  136. }
  137.  
  138. void handleRoot() {
  139.     // Serve the HTML content
  140.     String html = "<html><body><h1>Door Lock System</h1>";
  141.     html += "<p>The door is currently " + String(isLocked ? "Locked" : "Unlocked") + "</p>";
  142.     html += "</body></html>";
  143.     server.send(200, "text/html", html); // Send the HTML response
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement