Advertisement
pleasedontcode

**Servo Control** rev_08

Feb 20th, 2025
319
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-21 00:22:45
  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 checkButtonPress();
  41. void lockUnlockDoor();
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t codeButton_PushButton_PIN_D9      = 9;
  45.  
  46. /***** DEFINITION OF PWM OUTPUT PINS *****/
  47. const uint8_t servo_Servomotor_PWMSignal_PIN_D2     = 2;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData       = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. float   servo_Servomotor_PWMSignal_PIN_D2_phyData       = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. // Create an instance of EasyButton for the button
  57. EasyButton button(codeButton_PushButton_PIN_D9);
  58.  
  59. // Create an instance of Servo for the servo motor
  60. Servo servoMotor;
  61.  
  62. /***** STATE VARIABLES *****/
  63. bool doorLocked = true; // Initial state of the door is locked
  64.  
  65. void setup(void)
  66. {
  67.     // put your setup code here, to run once:
  68.     pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
  69.     pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  70.  
  71.     // Initialize the button
  72.     button.begin();
  73.     // Attach the servo to the PWM pin
  74.     servoMotor.attach(servo_Servomotor_PWMSignal_PIN_D2);
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // put your main code here, to run repeatedly:
  80.     button.update(); // Update button state
  81.     checkButtonPress(); // Check if the button is pressed
  82.     updateOutputs(); // Refresh output data
  83. }
  84.  
  85. void checkButtonPress()
  86. {
  87.     if (button.isPressed()) {
  88.         lockUnlockDoor(); // Check and toggle the lock state
  89.     }
  90. }
  91.  
  92. void lockUnlockDoor()
  93. {
  94.     // Toggle the lock state
  95.     doorLocked = !doorLocked;
  96.  
  97.     // Set the servo position based on the lock state
  98.     if (doorLocked) {
  99.         servo_Servomotor_PWMSignal_PIN_D2_rawData = SERVOMIN; // Lock the door
  100.     } else {
  101.         servo_Servomotor_PWMSignal_PIN_D2_rawData = SERVOMAX; // Unlock the door
  102.     }
  103. }
  104.  
  105. void updateOutputs()
  106. {
  107.     // Write the PWM signal to the servo
  108.     servoMotor.write(servo_Servomotor_PWMSignal_PIN_D2_rawData);
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement