Advertisement
pleasedontcode

**Servo Control** rev_10

Feb 26th, 2025
268
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: 2025-02-26 22:46:35
  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 <EasyButton.h> //https://github.com/evert-arias/EasyButton
  34. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void updateOutputs();
  40. void handleButtonPress();
  41. void startWebServer();
  42. void handleClient();
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t codeButton_PushButton_PIN_D9      = 9;
  46.  
  47. /***** DEFINITION OF PWM OUTPUT PINS *****/
  48. const uint8_t servo_Servomotor_PWMSignal_PIN_D2     = 2;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData       = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. float   servo_Servomotor_PWMSignal_PIN_D2_phyData       = 0.0;
  55.  
  56. /***** PIN CODE VARIABLES *****/
  57. const String correctPIN = "1234"; // Set your correct PIN code here
  58. String enteredPIN = ""; // Variable to store the entered PIN
  59. bool doorLocked = true; // Initial state of the door (locked)
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. EasyButton codeButton(codeButton_PushButton_PIN_D9);
  63. Servo myServo;
  64.  
  65. /****** SETUP FUNCTION *****/
  66. void setup(void)
  67. {
  68.     // Initialize serial communication
  69.     Serial.begin(115200);
  70.  
  71.     // Initialize button
  72.     codeButton.begin();
  73.     codeButton.onPressed(handleButtonPress);
  74.  
  75.     // Initialize servo
  76.     myServo.attach(servo_Servomotor_PWMSignal_PIN_D2);
  77.  
  78.     // Start web server
  79.     startWebServer();
  80. }
  81.  
  82. /****** LOOP FUNCTION *****/
  83. void loop(void)
  84. {
  85.     // Update button state
  86.     codeButton.read();
  87.  
  88.     // Handle web client
  89.     handleClient();
  90.  
  91.     // Refresh output data
  92.     updateOutputs();
  93. }
  94.  
  95. /****** BUTTON PRESS HANDLER FUNCTION *****/
  96. void handleButtonPress()
  97. {
  98.     // Simulate entering a PIN code (for demonstration purposes)
  99.     // In a real application, you would read the PIN from a keypad or similar
  100.     enteredPIN = correctPIN; // Simulating correct PIN entry
  101.  
  102.     if (enteredPIN == correctPIN)
  103.     {
  104.         // Toggle door lock state
  105.         doorLocked = !doorLocked;
  106.  
  107.         // Update servo position based on door state
  108.         if (doorLocked)
  109.         {
  110.             servo_Servomotor_PWMSignal_PIN_D2_rawData = 0; // Lock the door
  111.         }
  112.         else
  113.         {
  114.             servo_Servomotor_PWMSignal_PIN_D2_rawData = 180; // Unlock the door
  115.         }
  116.         Serial.println(doorLocked ? "Door Locked" : "Door Unlocked");
  117.     }
  118. }
  119.  
  120. /****** UPDATE OUTPUTS FUNCTION *****/
  121. void updateOutputs()
  122. {
  123.     myServo.write(servo_Servomotor_PWMSignal_PIN_D2_rawData);
  124. }
  125.  
  126. /****** START WEB SERVER FUNCTION *****/
  127. void startWebServer()
  128. {
  129.     // Code to initialize and start the web server
  130.     // This would typically include setting up routes and handlers
  131. }
  132.  
  133. /****** HANDLE CLIENT FUNCTION *****/
  134. void handleClient()
  135. {
  136.     // Code to handle incoming web client requests
  137.     // This would typically include sending the lock/unlock state to the client
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement