Advertisement
pleasedontcode

**Servo Control** rev_113

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