Advertisement
pleasedontcode

**Servo Control** rev_115

Oct 7th, 2024
89
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 compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-10-07 22:00:22
  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 <XYZrobotServo.h> // Use the XYZrobotServo library instead of the standard Servo library
  34. #include <Keypad.h> // Keypad library
  35. #include <WiFi.h> // WiFi library for web server
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs();
  41. void handleClient();
  42. char getKey(); // Added function prototype for getKey()
  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. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. bool keypad_Keypad3x4_C1_PIN_D8_rawData = 0;
  58. bool keypad_Keypad3x4_C2_PIN_D9_rawData = 0;
  59. bool keypad_Keypad3x4_C3_PIN_D10_rawData = 0;
  60.  
  61. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  62. float keypad_Keypad3x4_C1_PIN_D8_phyData = 0.0;
  63. float keypad_Keypad3x4_C2_PIN_D9_phyData = 0.0;
  64. float keypad_Keypad3x4_C3_PIN_D10_phyData = 0.0;
  65.  
  66. const uint8_t KEYPAD3X4_ROWS = 4; // four rows
  67. const uint8_t KEYPAD3X4_COLS = 3; // three columns
  68. char Keys_Keypad3x4[KEYPAD3X4_ROWS][KEYPAD3X4_COLS] = {
  69.     {'1','2','3'},
  70.     {'4','5','6'},
  71.     {'7','8','9'},
  72.     {'*','0','#'},
  73. };
  74.  
  75. // Define the correct PIN code
  76. const String correctPin = "1234"; // Example PIN code
  77. String inputPin = ""; // To store user input
  78.  
  79. // Servo object
  80. XYZrobotServo myServo(Serial, 1); // Instantiate with Serial and ID 1
  81.  
  82. // WiFi credentials
  83. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  84. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  85.  
  86. WiFiServer server(80); // Create a server that listens on port 80
  87.  
  88. void setup(void)
  89. {
  90.     // Initialize servo
  91.     myServo.setPosition(0, 0); // Start with the door locked
  92.  
  93.     // Initialize keypad pins
  94.     pinMode(keypad_Keypad3x4_R1_PIN_D4, INPUT_PULLUP);
  95.     pinMode(keypad_Keypad3x4_R2_PIN_D5, INPUT_PULLUP);
  96.     pinMode(keypad_Keypad3x4_R3_PIN_D6, INPUT_PULLUP);
  97.     pinMode(keypad_Keypad3x4_R4_PIN_D7, INPUT_PULLUP);
  98.     pinMode(pinButton_PushButton_PIN_D2, INPUT_PULLUP);
  99.  
  100.     pinMode(keypad_Keypad3x4_C1_PIN_D8, OUTPUT);
  101.     pinMode(keypad_Keypad3x4_C2_PIN_D9, OUTPUT);
  102.     pinMode(keypad_Keypad3x4_C3_PIN_D10, OUTPUT);
  103.  
  104.     // Start WiFi connection
  105.     WiFi.begin(ssid, password);
  106.     while (WiFi.status() != WL_CONNECTED) {
  107.         delay(1000);
  108.     }
  109.     server.begin(); // Start the server
  110. }
  111.  
  112. void loop(void)
  113. {
  114.     updateOutputs(); // Refresh output data
  115.     handleClient(); // Handle incoming client requests
  116.  
  117.     // Check if a key is pressed
  118.     char key = getKey();
  119.     if (key) {
  120.         if (key == '#') { // If '#' is pressed, check the PIN
  121.             if (inputPin == correctPin) {
  122.                 uint16_t position = myServo.readPosition();
  123.                 if (position == 0) {
  124.                     myServo.setPosition(90, 0); // Unlock the door
  125.                 } else {
  126.                     myServo.setPosition(0, 0); // Lock the door
  127.                 }
  128.             }
  129.             inputPin = ""; // Reset input
  130.         } else {
  131.             inputPin += key; // Add key to input
  132.         }
  133.     }
  134. }
  135.  
  136. void updateOutputs()
  137. {
  138.     digitalWrite(keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C1_PIN_D8_rawData);
  139.     digitalWrite(keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C2_PIN_D9_rawData);
  140.     digitalWrite(keypad_Keypad3x4_C3_PIN_D10, keypad_Keypad3x4_C3_PIN_D10_rawData);
  141. }
  142.  
  143. void handleClient() {
  144.     WiFiClient client = server.available(); // Listen for incoming clients
  145.     if (client) {
  146.         String currentState = (myServo.readPosition() == 0) ? "Locked" : "Unlocked";
  147.         client.println("HTTP/1.1 200 OK");
  148.         client.println("Content-type:text/html");
  149.         client.println();
  150.         client.println("<html><body>");
  151.         client.println("<h1>Door is " + currentState + "</h1>");
  152.         client.println("</body></html>");
  153.         client.stop(); // Close the connection
  154.     }
  155. }
  156.  
  157. char getKey() {
  158.     // Implement keypad reading logic here
  159.     // This function should return the pressed key character
  160.     Keypad keypad = Keypad(makeKeymap(Keys_Keypad3x4),
  161.                            new byte[KEYPAD3X4_ROWS]{keypad_Keypad3x4_R1_PIN_D4, keypad_Keypad3x4_R2_PIN_D5, keypad_Keypad3x4_R3_PIN_D6, keypad_Keypad3x4_R4_PIN_D7},
  162.                            new byte[KEYPAD3X4_COLS]{keypad_Keypad3x4_C1_PIN_D8, keypad_Keypad3x4_C2_PIN_D9, keypad_Keypad3x4_C3_PIN_D10},
  163.                            KEYPAD3X4_ROWS, KEYPAD3X4_COLS);
  164.     char key = keypad.getKey();
  165.     return key; // Return the pressed key character
  166. }
  167.  
  168. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement