Advertisement
pleasedontcode

**Servo Control** rev_101

Oct 6th, 2024
86
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-06 17:43:34
  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>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  32. #include <Keypad.h> //https://github.com/Chris--A/Keypad
  33. #include <WiFi.h> // Include WiFi library for web server
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs();
  39. void handleKeypadInput(char key);
  40. void startWebServer();
  41. void handleClient();
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t keypad_Keypad3x4_R1_PIN_D4        = 4;
  45. const uint8_t keypad_Keypad3x4_R2_PIN_D5        = 5;
  46. const uint8_t keypad_Keypad3x4_R3_PIN_D6        = 6;
  47. const uint8_t keypad_Keypad3x4_R4_PIN_D7        = 7;
  48. const uint8_t pinButton_PushButton_PIN_D2       = 2;
  49.  
  50. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  51. const uint8_t keypad_Keypad3x4_C1_PIN_D8        = 8;
  52. const uint8_t keypad_Keypad3x4_C2_PIN_D9        = 9;
  53. const uint8_t keypad_Keypad3x4_C3_PIN_D10       = 10;
  54.  
  55. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  56. bool    keypad_Keypad3x4_C1_PIN_D8_rawData      = 0;
  57. bool    keypad_Keypad3x4_C2_PIN_D9_rawData      = 0;
  58. bool    keypad_Keypad3x4_C3_PIN_D10_rawData     = 0;
  59.  
  60. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  61. float   keypad_Keypad3x4_C1_PIN_D8_phyData      = 0.0;
  62. float   keypad_Keypad3x4_C2_PIN_D9_phyData      = 0.0;
  63. float   keypad_Keypad3x4_C3_PIN_D10_phyData     = 0.0;
  64.  
  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. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  75. Servo doorServo; // Create a Servo object for the door
  76. Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4), keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7, KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  77.  
  78. // WiFi credentials
  79. const char* ssid = "your_SSID"; // Replace with your network SSID
  80. const char* password = "your_PASSWORD"; // Replace with your network password
  81.  
  82. WiFiServer server(80); // Create a server that listens on port 80
  83.  
  84. bool doorLocked = true; // Initial state of the door
  85. const String correctPIN = "1234"; // Define the correct PIN code
  86. String enteredPIN = ""; // Store the entered PIN
  87.  
  88. void setup(void)
  89. {
  90.     // put your setup code here, to run once:
  91.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  92.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  93.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  94.  
  95.     doorServo.attach(3); // Attach the servo on pin 3
  96.  
  97.     // Initialize WiFi
  98.     WiFi.begin(ssid, password);
  99.     while (WiFi.status() != WL_CONNECTED) {
  100.         delay(1000);
  101.     }
  102.     startWebServer(); // Start the web server
  103. }
  104.  
  105. void loop(void)
  106. {
  107.     // put your main code here, to run repeatedly:
  108.     char key = keypad.getKey();
  109.     if (key) {
  110.         handleKeypadInput(key);
  111.     }
  112.     handleClient(); // Handle incoming client requests
  113.     updateOutputs(); // Refresh output data
  114. }
  115.  
  116. void updateOutputs()
  117. {
  118.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  119.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  120.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  121. }
  122.  
  123. void handleKeypadInput(char key)
  124. {
  125.     if (key == '#') { // If '#' is pressed, check the entered PIN
  126.         if (enteredPIN == correctPIN) {
  127.             doorLocked = !doorLocked; // Toggle the lock state
  128.             doorServo.write(doorLocked ? 0 : 90); // Move servo to lock/unlock position
  129.         }
  130.         enteredPIN = ""; // Clear entered PIN
  131.     } else if (key == '*') { // Clear the entered PIN
  132.         enteredPIN = "";
  133.     } else {
  134.         enteredPIN += key; // Append the key to the entered PIN
  135.     }
  136. }
  137.  
  138. void startWebServer() {
  139.     server.begin(); // Start the server
  140. }
  141.  
  142. void handleClient() {
  143.     WiFiClient client = server.available(); // Check if a client has connected
  144.     if (client) {
  145.         String currentLine = ""; // Make a String to hold incoming data
  146.         while (client.connected()) {
  147.             if (client.available()) {
  148.                 char c = client.read(); // Read a byte
  149.                 currentLine += c; // Add it to the end of the currentLine
  150.                 if (c == '\n') { // If the line is complete
  151.                     client.println("HTTP/1.1 200 OK"); // Send response
  152.                     client.println("Content-type:text/html");
  153.                     client.println();
  154.                     client.println("<html><body>");
  155.                     client.println("<h1>Door is " + String(doorLocked ? "Locked" : "Unlocked") + "</h1>");
  156.                     client.println("</body></html>");
  157.                     client.println();
  158.                     break;
  159.                 }
  160.             }
  161.         }
  162.         client.stop(); // Close the connection
  163.     }
  164. }
  165.  
  166. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement