Advertisement
pleasedontcode

Servo Control rev_02

Jun 19th, 2024
583
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 Mega
  14.     - Source Code created on: 2024-06-19 07:27:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a blind solar tracker system using DS3231 */
  21.     /* RTC for timekeeping and Servo motors for panel */
  22.     /* adjustment. Ensure precise control of servo */
  23.     /* positions based on real-time data. Utilize I2C */
  24.     /* communication for RTC and PWM signals for servos. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <DS3231.h>  // https://github.com/NorthernWidget/DS3231
  30. #include <Servo.h>   // https://github.com/arduino-libraries/Servo
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36.  
  37. /***** DEFINITION OF PWM OUTPUT PINS *****/
  38. const uint8_t servo1_Servomotor_PWMSignal_PIN_D2 = 2;
  39. const uint8_t servo2_Servomotor_PWMSignal_PIN_D3 = 3;
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t RTC_DS3231_I2C_PIN_SDA_D20 = 20;
  43. const uint8_t RTC_DS3231_I2C_PIN_SCL_D21 = 21;
  44. const uint8_t RTC_DS3231_I2C_SLAVE_ADDRESS = 104;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. uint8_t servo1_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  49. uint8_t servo2_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float servo1_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  54. float servo2_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. DS3231 myRTC;  // Initialize DS3231 object
  58. Servo servo1;  // Initialize Servo object for servo1
  59. Servo servo2;  // Initialize Servo object for servo2
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.     Wire.begin();
  65.     Serial.begin(57600);
  66.     delay(500);
  67.     Serial.println("Arduino Mega Ready!");
  68.  
  69.     // Attach the servo objects to the PWM pins
  70.     servo1.attach(servo1_Servomotor_PWMSignal_PIN_D2);
  71.     servo2.attach(servo2_Servomotor_PWMSignal_PIN_D3);
  72. }
  73.  
  74. void loop(void)
  75. {
  76.     // put your main code here, to run repeatedly:
  77.     updateOutputs(); // Refresh output data
  78.  
  79.     // Example of reading time from DS3231 and printing it
  80.     DateTime now = myRTC.now();
  81.     Serial.print(now.year());
  82.     Serial.print('/');
  83.     Serial.print(now.month());
  84.     Serial.print('/');
  85.     Serial.print(now.day());
  86.     Serial.print(' ');
  87.     Serial.print(now.hour());
  88.     Serial.print(':');
  89.     Serial.print(now.minute());
  90.     Serial.print(':');
  91.     Serial.print(now.second());
  92.     Serial.println();
  93.  
  94.     delay(1000); // Delay for 1 second
  95. }
  96.  
  97. void updateOutputs()
  98. {
  99.     // Write the raw data to the servos
  100.     servo1.write(servo1_Servomotor_PWMSignal_PIN_D2_rawData);
  101.     servo2.write(servo2_Servomotor_PWMSignal_PIN_D3_rawData);
  102. }
  103.  
  104. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement