Advertisement
pleasedontcode

**Keypad Control** rev_117

Oct 7th, 2024
69
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-07 22:23:30
  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. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  34. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  35. #include <WiFi.h>  // Include WiFi library for web server
  36. #include <WebServer.h> // Include WebServer library
  37. #include "ServoControl.h" // Include the ServoControl header
  38. #include "WebServerControl.h" // Include the WebServerControl header
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  46. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  47. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  48. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  49. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  50.  
  51. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  52. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  53. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  54. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  55.  
  56. const char* ssid = "your_SSID";  // Replace with your network SSID
  57. const char* password = "your_PASSWORD";  // Replace with your network password
  58.  
  59. /****** DEFINITION OF OUTPUT VARIABLES *****/
  60. bool doorLocked = true; // Initial state of the door
  61. ServoControl servoControl(9); // Assuming the servo is connected to pin 9
  62. WebServerControl webServerControl; // Create web server control instance
  63.  
  64. const uint8_t KEYPAD3X4_ROWS = 4; //four rows
  65. const uint8_t KEYPAD3X4_COLS = 3; //three columns
  66. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  67.     {'1','2','3'},
  68.     {'4','5','6'},
  69.     {'7','8','9'},
  70.     {'*','0','#'},
  71. };
  72.  
  73. void setup(void)
  74. {
  75.     // put your setup code here, to run once:
  76.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  77.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  78.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  79.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  80.     pinMode(pinButton_PushButton_PIN_D2,    INPUT_PULLUP);
  81.  
  82.     pinMode(keypad_Keypad3x4_C1_PIN_D8,  OUTPUT);
  83.     pinMode(keypad_Keypad3x4_C2_PIN_D9,  OUTPUT);
  84.     pinMode(keypad_Keypad3x4_C3_PIN_D10,     OUTPUT);
  85.  
  86.     // Initialize WiFi
  87.     WiFi.begin(ssid, password);
  88.     while (WiFi.status() != WL_CONNECTED) {
  89.         delay(1000);
  90.     }
  91.     webServerControl.begin(); // Start the web server
  92. }
  93.  
  94. void loop(void)
  95. {
  96.     // put your main code here, to run repeatedly:
  97.     updateOutputs(); // Refresh output data
  98.     webServerControl.handleClient(); // Handle web server requests
  99.  
  100.     // Check for keypad input here and unlock/lock the door based on the PIN
  101.     char key = keypad.getKey();
  102.     if (key) {
  103.         handleKeypadInput(key); // Function to handle keypad input
  104.     }
  105. }
  106.  
  107. void updateOutputs()
  108. {
  109.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  110.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  111.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  112. }
  113.  
  114. void handleKeypadInput(char key)
  115. {
  116.     // Assuming '1234' is the correct PIN code
  117.     static String input;
  118.     if (key == '*') { // Reset input
  119.         input = "";
  120.     } else if (key == '#') { // Check PIN
  121.         if (input == "1234") { // Check if input matches the PIN
  122.             if (doorLocked) {
  123.                 servoControl.unlock(); // Unlock the door
  124.                 doorLocked = false;
  125.             } else {
  126.                 servoControl.lock(); // Lock the door
  127.                 doorLocked = true;
  128.             }
  129.             webServerControl.setLockState(doorLocked); // Update web server state
  130.         }
  131.     } else { // Append digit to input
  132.         input += key;
  133.     }
  134. }
  135.  
  136. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement