Advertisement
pleasedontcode

**Keypad Control** rev_49

Oct 5th, 2024
57
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: **Keypad Control**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 07:40:37
  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>    // Library for controlling servo motors
  32. #include <Keypad.h>           // Library for keypad input
  33. #include <WiFi.h>            // Library for WiFi functionality
  34. #include <WebServer.h>        // Library for web server functionality
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void handleRoot();          // Function to handle root URL
  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.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  49. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  50. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  51.  
  52. /***** DEFINITION OF PWM OUTPUT PINS *****/
  53. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  54.  
  55. /***** DEFINITION OF KEYPAD *****/
  56. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  57. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  58. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  59.     {'1', '2', '3'},
  60.     {'4', '5', '6'},
  61.     {'7', '8', '9'},
  62.     {'*', '0', '#'},
  63. };
  64.  
  65. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  66. Servo myServo; // Servo object
  67.  
  68. /***** DEFINITION OF VARIABLES *****/
  69. const char* ssid = "your_SSID"; // Your WiFi SSID
  70. const char* password = "your_PASSWORD"; // Your WiFi Password
  71. WebServer server(80); // Create a web server on port 80
  72.  
  73. bool doorLocked = true; // Initial state of the door
  74. const String correctPIN = "1234"; // Correct PIN code
  75. String inputPIN = ""; // User input PIN
  76.  
  77. void setup(void) {
  78.     Serial.begin(115200); // Start serial communication
  79.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach the servo to the PWM pin
  80.     myServo.write(0); // Initially lock the door
  81.  
  82.     // Set up keypad pins
  83.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  84.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  85.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  86.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  87.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  88.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  89.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  90.  
  91.     // Connect to WiFi
  92.     WiFi.begin(ssid, password);
  93.     while (WiFi.status() != WL_CONNECTED) {
  94.         delay(1000);
  95.         Serial.println("Connecting to WiFi...");
  96.     }
  97.     Serial.println("Connected to WiFi");
  98.  
  99.     // Set up web server
  100.     server.on("/", handleRoot); // Handle root URL
  101.     server.begin(); // Start the server
  102. }
  103.  
  104. void loop(void) {
  105.     server.handleClient(); // Handle client requests
  106.     char key = keypad.getKey(); // Get the pressed key
  107.  
  108.     if (key) {
  109.         if (key == '#') { // If '#' is pressed, check the PIN
  110.             if (inputPIN == correctPIN) {
  111.                 doorLocked = !doorLocked; // Toggle lock state
  112.                 myServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
  113.                 inputPIN = ""; // Reset input
  114.             } else {
  115.                 inputPIN = ""; // Reset input on wrong PIN
  116.             }
  117.         } else {
  118.             inputPIN += key; // Append key to input PIN
  119.         }
  120.     }
  121. }
  122.  
  123. void handleRoot() {
  124.     String message = "Door is currently " + String(doorLocked ? "Locked" : "Unlocked");
  125.     server.send(200, "text/html", message); // Send response to the client
  126. }
  127.  
  128. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement