pleasedontcode

Scheduled Servo rev_01

Aug 29th, 2025
549
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: Scheduled Servo
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-08-29 07:24:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* rotating at 1pm */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void rotateServoSweep(void); // new: performs a complete 0-180-0 sweep
  33.  
  34. /***** DEFINITION OF PWM OUTPUT PINS *****/
  35. const uint8_t servoo_Servomotor_PWMSignal_PIN_D3 = 3;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  38. Servo servomotor; // Servo object to control the PWM pin
  39.  
  40. // Timekeeping (software clock)
  41. unsigned long bootMillis; // time reference from startup
  42. int lastRotatedDay = -1;  // last day when the 1pm rotation occurred
  43.  
  44. void rotateServoSweep(void)
  45. {
  46.   // Rotates servo from 0 to 180 and back
  47.   for (int pos = 0; pos <= 180; pos++) {
  48.     servomotor.write(pos);
  49.     delay(15);
  50.   }
  51.   for (int pos = 180; pos >= 0; pos--) {
  52.     servomotor.write(pos);
  53.     delay(15);
  54.   }
  55. }
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.  
  61.   // Attach the servo to the PWM pin
  62.   servomotor.attach(servoo_Servomotor_PWMSignal_PIN_D3);
  63.  
  64.   // Optional: set an initial position
  65.   servomotor.write(90);
  66.  
  67.   // Initialize software clock reference
  68.   bootMillis = millis();
  69.  
  70.   // Note: When using the Servo library, explicit pinMode is not required.
  71.   // If you want to force a digital pin mode, you could uncomment the line below,
  72.   // but it's generally unnecessary with Servo.attach().
  73.   // pinMode(servoo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  74. }
  75.  
  76.  
  77. void loop(void)
  78. {
  79.   // put your main code here, to run repeatedly:
  80.  
  81.   // Compute a simple software time (UTC-like) starting from boot
  82.   unsigned long elapsedSec = (millis() - (unsigned long)bootMillis) / 1000;
  83.   int currentSecond = elapsedSec % 60;
  84.   int currentMinute = (elapsedSec / 60) % 60;
  85.   int currentHour = (elapsedSec / 3600) % 24;
  86.   int currentDay = elapsedSec / 86400;
  87.  
  88.   // Trigger rotation at 13:00:00 once per day
  89.   if (currentDay != lastRotatedDay && currentHour == 13 && currentMinute == 0 && currentSecond == 0) {
  90.     rotateServoSweep();
  91.     lastRotatedDay = currentDay;
  92.   }
  93.   else if (currentDay != lastRotatedDay) {
  94.     // Before 1pm (or any other time before rotation), perform a small idle sweep to keep motion visible
  95.     for (int pos = 0; pos <= 60; pos++) {
  96.       servomotor.write(pos);
  97.       delay(5);
  98.     }
  99.     for (int pos = 60; pos >= 0; pos--) {
  100.       servomotor.write(pos);
  101.       delay(5);
  102.     }
  103.   }
  104.   else {
  105.     // Already rotated today; idle to avoid a tight loop
  106.     delay(100);
  107.   }
  108. }
  109.  
  110. /* END CODE */
  111.  
Advertisement
Add Comment
Please, Sign In to add comment