Advertisement
pleasedontcode

**Servo Control** rev_54

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: **Servo Control**
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-05 08:29: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. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Deneyap_Servo.h>  // Library to control servo motors
  32. #include <Keypad.h>         // Library to handle keypad input
  33. #include <WiFi.h>          // Library for WiFi connectivity
  34. #include <WebServer.h>      // Library to create a web server
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void handleRoot(void);
  40. void checkKeypad(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t keypad_Keypad3x4_R1_PIN_D4 = 4;
  44. const uint8_t keypad_Keypad3x4_R2_PIN_D5 = 5;
  45. const uint8_t keypad_Keypad3x4_R3_PIN_D6 = 6;
  46. const uint8_t keypad_Keypad3x4_R4_PIN_D7 = 7;
  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. /***** DEFINITION OF KEYPAD VARIABLES *****/
  54. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  55. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  56. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  57.     {'1', '2', '3'},
  58.     {'4', '5', '6'},
  59.     {'7', '8', '9'},
  60.     {'*', '0', '#'},
  61. };
  62.  
  63. // Create keypad object
  64. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4),
  65.                        new byte[KEYPAD3X4_ROWS]{keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7},
  66.                        new byte[KEYPAD3X4_COLS]{keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10},
  67.                        KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  68.  
  69. // Create servo object
  70. Servo doorServo;
  71.  
  72. // WiFi and web server setup
  73. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  74. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  75. WebServer server(80);
  76.  
  77. bool doorLocked = true; // Initial state of the door
  78. const char* correctPin = "1234"; // Correct PIN code
  79.  
  80. void setup(void) {
  81.     // Initialize pins
  82.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  83.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  84.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  85.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  86.    
  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.     // Attach servo to pin
  92.     doorServo.attach(3); // Attach servo to pin 3
  93.  
  94.     // Start WiFi
  95.     WiFi.begin(ssid, password);
  96.     while (WiFi.status() != WL_CONNECTED) {
  97.         delay(1000);
  98.     }
  99.  
  100.     // Start web server
  101.     server.on("/", handleRoot);
  102.     server.begin();
  103. }
  104.  
  105. void loop(void) {
  106.     server.handleClient(); // Handle web server requests
  107.     checkKeypad(); // Check for keypad input
  108. }
  109.  
  110. void handleRoot(void) {
  111.     String state = doorLocked ? "Locked" : "Unlocked";
  112.     String message = "<html><body><h1>Door is " + state + "</h1></body></html>";
  113.     server.send(200, "text/html", message);
  114. }
  115.  
  116. void checkKeypad(void) {
  117.     char key = keypad.getKey();
  118.     static String inputPin = "";
  119.  
  120.     if (key) {
  121.         if (key == '#') {
  122.             // Check if the entered PIN is correct
  123.             if (inputPin == correctPin) {
  124.                 doorLocked = !doorLocked; // Toggle door state
  125.                 doorServo.write(doorLocked ? 0 : 90); // Lock or unlock the door
  126.             }
  127.             inputPin = ""; // Reset input
  128.         } else if (key == '*') {
  129.             inputPin = ""; // Clear input
  130.         } else {
  131.             inputPin += key; // Append key to input
  132.         }
  133.     }
  134. }
  135.  
  136. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement