Advertisement
pleasedontcode

**Servo Control** rev_01

Nov 15th, 2024
69
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 Uno
  14.     - Source Code created on: 2024-11-15 06:12:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize the EasyButton library to */
  21.     /* manage button presses on pin D2, controlling a */
  22.     /* servo motor on pin D3 for locking mechanisms, */
  23.     /* while interfacing with the DS3231 RTC for */
  24.     /* timekeeping. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  32. #include <DS3231.h> //https://github.com/NorthernWidget/DS3231
  33. #include <EasyButton.h> // Added EasyButton library
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t Button_PushButton_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t Lock_Servomotor_PWMSignal_PIN_D3 = 3;
  44. const int lockPin = 9; // Added for servo lock control
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. uint8_t Lock_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. float Lock_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. RTC_DS3231 rtc; // RTC instance
  54. Servo myServo; // Servo instance
  55. EasyButton button(Button_PushButton_PIN_D2); // EasyButton instance
  56.  
  57. // Button state variables
  58. bool isLocked = false;
  59. bool timerStarted = false;
  60. DateTime lockTime;
  61. unsigned long lastCheckTime = 0;
  62. const unsigned long checkInterval = 1000;
  63.  
  64. void setup(void)
  65. {
  66.     Serial.begin(9600); // Serial communication
  67.     pinMode(Lock_Servomotor_PWMSignal_PIN_D3, OUTPUT); // Servo pin setup
  68.  
  69.     if (!rtc.begin()) {
  70.         Serial.println("RTC not found!");
  71.         while (1);
  72.     }
  73.     if (!rtc.isrunning()) {
  74.         rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  75.     }
  76.  
  77.     button.begin(); // Initialize EasyButton
  78.  
  79.     // Recover lock status from RTC
  80.     uint8_t lockStatus = rtc.readnvram(0);
  81.     if (lockStatus == 1) {
  82.         isLocked = true;
  83.         lockTime = rtc.now();
  84.         timerStarted = true;
  85.         Serial.println("Recovered locked state from RTC.");
  86.     } else {
  87.         unlock();
  88.     }
  89. }
  90.  
  91. void loop(void)
  92. {
  93.     button.read(); // Read button state
  94.  
  95.     if (button.wasPressed() && !timerStarted) {
  96.         Serial.println("Button pressed, starting timer and locking.");
  97.         lock();
  98.         timerStarted = true;
  99.         lockTime = rtc.now();
  100.         rtc.writenvram(0, 1);
  101.     }
  102.  
  103.     // Check time only every checkInterval milliseconds
  104.     if (millis() - lastCheckTime >= checkInterval) {
  105.         lastCheckTime = millis();
  106.         DateTime now = rtc.now();
  107.         if (timerStarted) {
  108.             if (now >= lockTime + TimeSpan(30)) {
  109.                 Serial.println("30 seconds passed, unlocking.");
  110.                 unlock();
  111.                 timerStarted = false;
  112.                 rtc.writenvram(0, 0);
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. void lock() {
  119.     if (!isLocked) {
  120.         myServo.attach(lockPin);
  121.         myServo.write(90);
  122.         delay(500);
  123.         myServo.detach();
  124.         isLocked = true;
  125.         Serial.println("Locked.");
  126.     }
  127. }
  128.  
  129. void unlock() {
  130.     if (isLocked) {
  131.         myServo.attach(lockPin);
  132.         myServo.write(0);
  133.         delay(500);
  134.         myServo.detach();
  135.         isLocked = false;
  136.         Serial.println("Unlocked.");
  137.     }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement