Advertisement
pleasedontcode

**Keypad Control** rev_51

Oct 5th, 2024
56
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:45:07
  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 button which enable the insertion of PIN code.
  34. #### Feedback 2 ####
  35. - i need a physical button to enable pin enter into keypad
  36. ********* User code review feedback **********/
  37.  
  38. /****** DEFINITION OF LIBRARIES *****/
  39. #include <Deneyap_Servo.h>    // Library for controlling servo motors
  40. #include <Keypad.h>           // Library for keypad input
  41. #include <WiFi.h>            // Library for WiFi functionality
  42. #include <WebServer.h>        // Library for web server functionality
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47. void handleRoot();          // Function to handle root URL
  48.  
  49. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  50. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  51. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  52. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  53. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  54. const uint8_t buttonPin = 2; // Physical button pin for enabling PIN entry
  55.  
  56. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  57. const uint8_t keypad_Keypad3x4_C1_PIN_D8 = 8;
  58. const uint8_t keypad_Keypad3x4_C2_PIN_D9 = 9;
  59. const uint8_t keypad_Keypad3x4_C3_PIN_D10 = 10;
  60.  
  61. /***** DEFINITION OF PWM OUTPUT PINS *****/
  62. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  63.  
  64. /***** DEFINITION OF KEYPAD *****/
  65. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  66. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  67. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  68.     {'1', '2', '3'},
  69.     {'4', '5', '6'},
  70.     {'7', '8', '9'},
  71.     {'*', '0', '#'},
  72. };
  73.  
  74. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_C1_PIN_D8, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  75. Servo myServo; // Servo object
  76.  
  77. /***** DEFINITION OF VARIABLES *****/
  78. const char* ssid = "your_SSID"; // Your WiFi SSID
  79. const char* password = "your_PASSWORD"; // Your WiFi Password
  80. WebServer server(80); // Create a web server on port 80
  81.  
  82. bool doorLocked = true; // Initial state of the door
  83. const String correctPIN = "1234"; // Correct PIN code
  84. String inputPIN = ""; // User input PIN
  85. bool pinEntryActive = false; // Flag to indicate if PIN entry is active
  86.  
  87. void setup(void) {
  88.     Serial.begin(115200); // Start serial communication
  89.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attach the servo to the PWM pin
  90.     myServo.write(0); // Initially lock the door
  91.  
  92.     // Set up keypad pins
  93.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  94.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  97.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  98.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  99.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  100.     pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
  101.  
  102.     // Connect to WiFi
  103.     WiFi.begin(ssid, password);
  104.     while (WiFi.status() != WL_CONNECTED) {
  105.         delay(1000);
  106.         Serial.println("Connecting to WiFi...");
  107.     }
  108.     Serial.println("Connected to WiFi");
  109.  
  110.     // Set up web server
  111.     server.on("/", handleRoot); // Handle root URL
  112.     server.begin(); // Start the server
  113. }
  114.  
  115. void loop(void) {
  116.     server.handleClient(); // Handle client requests
  117.     char key = keypad.getKey(); // Get the pressed key
  118.  
  119.     // Check if the button is pressed to activate PIN entry
  120.     if (digitalRead(buttonPin) == LOW) {
  121.         pinEntryActive = true; // Activate PIN entry mode
  122.         inputPIN = ""; // Reset input
  123.         Serial.println("PIN entry activated. Please enter your PIN.");
  124.         delay(1000); // Debounce delay
  125.     }
  126.  
  127.     if (key) {
  128.         if (pinEntryActive) {
  129.             if (key == '#') { // If '#' is pressed, check the PIN
  130.                 if (inputPIN == correctPIN) {
  131.                     doorLocked = !doorLocked; // Toggle lock state
  132.                     myServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
  133.                     Serial.println("PIN accepted. Door state changed.");
  134.                 } else {
  135.                     Serial.println("Incorrect PIN. Try again.");
  136.                 }
  137.                 pinEntryActive = false; // Deactivate PIN entry
  138.                 inputPIN = ""; // Reset input
  139.             } else {
  140.                 inputPIN += key; // Append key to input PIN
  141.             }
  142.         }
  143.     }
  144. }
  145.  
  146. void handleRoot() {
  147.     String message = "Door is currently " + String(doorLocked ? "Locked" : "Unlocked");
  148.     server.send(200, "text/html", message); // Send response to the client
  149. }
  150.  
  151. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement