Advertisement
pleasedontcode

**Servo Control** rev_09

Feb 23rd, 2025
238
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-23 11:07:03
  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"; // Define the correct PIN code
  58. String inputPIN = ""; // Variable to store the input PIN
  59. bool doorLocked = true; // Initial state of the door (locked)
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. // Create instances for EasyButton and Servo
  63. EasyButton button(codeButton_PushButton_PIN_D9);
  64. Servo servoMotor;
  65.  
  66. void setup(void)
  67. {
  68.     // put your setup code here, to run once:
  69.     pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  70.     pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  71.  
  72.     // Initialize the button
  73.     button.begin();
  74.     button.onPressed(handleButtonPress); // Set the button press handler
  75.  
  76.     // Initialize servo motor
  77.     servoMotor.attach(servo_Servomotor_PWMSignal_PIN_D2);
  78.  
  79.     // Start the web server
  80.     startWebServer();
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // put your main code here, to run repeatedly:
  86.     button.read(); // Read the button state
  87.     handleClient(); // Handle web server client requests
  88.     updateOutputs(); // Refresh output data
  89. }
  90.  
  91. void updateOutputs()
  92. {
  93.     // Update the servo position based on the lock state
  94.     if (doorLocked) {
  95.         servo_Servomotor_PWMSignal_PIN_D2_rawData = 0; // Locked position
  96.     } else {
  97.         servo_Servomotor_PWMSignal_PIN_D2_rawData = 180; // Unlocked position
  98.     }
  99.     analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  100. }
  101.  
  102. void handleButtonPress()
  103. {
  104.     // Simulate entering a PIN code (for demonstration purposes)
  105.     inputPIN += "1"; // Append a digit (this should be replaced with actual input handling)
  106.     if (inputPIN.length() == 4) { // Check if 4 digits are entered
  107.         if (inputPIN == correctPIN) {
  108.             doorLocked = !doorLocked; // Toggle the lock state
  109.         }
  110.         inputPIN = ""; // Reset the input PIN
  111.     }
  112. }
  113.  
  114. void startWebServer() {
  115.     // Code to initialize and start the web server
  116. }
  117.  
  118. void handleClient() {
  119.     // Code to handle incoming client requests and serve the web page
  120.     // This should display the current lock state of the door
  121. }
  122.  
  123. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement