#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
RTC_DS3231 rtc; // Use RTC_DS1307 for DS1307 module
Servo myServo; // Create a servo object
// Pin definitions
const int servoPin = 23; // Servo connected to pin 9 (adjust as needed)
// Servo angle settings for different times (example)
const int morningAngle = 0; // 0 degrees at 6 AM
const int noonAngle = 90; // 90 degrees at 12 PM
const int eveningAngle = 180; // 180 degrees at 6 PM
void setup() {
Serial.begin(115200);
Wire.begin();
myServo.attach(servoPin); // Attach servo to the specified pin
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Halt if RTC is not found
}
// Uncomment the line below to set the RTC to the sketch's compilation time
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Set initial servo position based on current time
updateServoPosition();
}
void loop() {
DateTime now = rtc.now();
// Print current time
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Update servo position based on time
updateServoPosition();
delay(1000); // Update every second
}
void updateServoPosition() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
// Example: Move servo to specific angles at specific times
// You can customize the times and angles as needed
if (hour == 6 && minute == 0) { // 6:00 AM
myServo.write(morningAngle);
Serial.println("Servo set to morning position (0 degrees)");
} else if (hour == 12 && minute == 0) { // 12:00 PM
myServo.write(noonAngle);
Serial.println("Servo set to noon position (90 degrees)");
} else if (hour == 18 && minute == 0) { // 6:00 PM
myServo.write(eveningAngle);
Serial.println("Servo set to evening position (180 degrees)");
}
// Add more conditions for other times or angles as needed
}