Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Servo Control**
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-02-21 00:22:45
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The servo motor installed in the door will lock or */
- /* unlock based on the correct PIN code received by */
- /* the button. If the door is unlocked and the PIN is */
- /* correct, it will lock. If the door is locked and */
- /* the PIN is correct, it will unlock. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* The webserver provides a web page about lock or */
- /* unlock state of the door. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- #include <Deneyap_Servo.h> //https://github.com/deneyapkart/deneyap-servo-arduino-library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void checkButtonPress();
- void lockUnlockDoor();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t codeButton_PushButton_PIN_D9 = 9;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of EasyButton for the button
- EasyButton button(codeButton_PushButton_PIN_D9);
- // Create an instance of Servo for the servo motor
- Servo servoMotor;
- /***** STATE VARIABLES *****/
- bool doorLocked = true; // Initial state of the door is locked
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(codeButton_PushButton_PIN_D9, INPUT_PULLUP);
- pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
- // Initialize the button
- button.begin();
- // Attach the servo to the PWM pin
- servoMotor.attach(servo_Servomotor_PWMSignal_PIN_D2);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button.update(); // Update button state
- checkButtonPress(); // Check if the button is pressed
- updateOutputs(); // Refresh output data
- }
- void checkButtonPress()
- {
- if (button.isPressed()) {
- lockUnlockDoor(); // Check and toggle the lock state
- }
- }
- void lockUnlockDoor()
- {
- // Toggle the lock state
- doorLocked = !doorLocked;
- // Set the servo position based on the lock state
- if (doorLocked) {
- servo_Servomotor_PWMSignal_PIN_D2_rawData = SERVOMIN; // Lock the door
- } else {
- servo_Servomotor_PWMSignal_PIN_D2_rawData = SERVOMAX; // Unlock the door
- }
- }
- void updateOutputs()
- {
- // Write the PWM signal to the servo
- servoMotor.write(servo_Servomotor_PWMSignal_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement