RTC_DS3231 rtc;
Servo myservo; // Create servo object
int pos = 0; // Variable to store the servo position
bool movedMorning = false;
bool movedEvening = false;
const int servoPin = 9; // Signal pin for the servo
void setup() {
Serial.begin(9600);
Wire.begin();
myservo.attach(servoPin); // Attaches the servo on pin 9 to the servo object
myservo.write(0); // Initial position
}
void loop() {
DateTime now = rtc.now();
// Check for the morning event (e.g., 8:00 AM)
if (now.hour() == 8 && now.minute() == 0 && !movedMorning) {
// Move the servo to 180 degrees
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
movedMorning = true;
movedEvening = false; // Reset evening flag for the next cycle
Serial.println("Morning move completed.");
}
// Check for the evening event (e.g., 8:00 PM/20:00)
if (now.hour() == 20 && now.minute() == 0 && !movedEvening) {
// Move the servo back to 0 degrees
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
movedEvening = true;
movedMorning = false; // Reset morning flag for the next cycle
Serial.println("Evening move completed.");
}
// A small delay to prevent the loop from running too fast,
// though the time checks handle event frequency.
delay(1000);
}
For one thing, the sketch isn't in a <CODE/> block, which makes it easier for others to read. That's an important consideration when you're looking for free help.
#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
RTC_DS3231 rtc;
Servo myservo; // Create servo object
int pos = 0; // Variable to store the servo position
bool movedMorning = false;
bool movedEvening = false;
const int servoPin = 9; // Signal pin for the servo
void setup() {
Serial.begin(9600);
Wire.begin();
myservo.attach(servoPin); // Attaches the servo on pin 9 to the servo object
myservo.write(0); // Initial position
}
void loop() {
DateTime now = rtc.now();
// Check for the morning event (e.g., 8:00 AM)
if( now.hour() == 8 && now.minute() == 0 && !movedMorning ) {
// Move the servo to 180 degrees
for( pos = 0; pos <= 180; pos += 1 ) {
myservo.write(pos);
delay(15);
}
movedMorning = true;
movedEvening = false; // Reset evening flag for the next cycle
Serial.println("Morning move completed.");
}
// Check for the evening event (e.g., 8:00 PM/20:00)
if( now.hour() == 20 && now.minute() == 0 && !movedEvening ) {
// Move the servo back to 0 degrees
for( pos = 180; pos >= 0; pos -= 1 ) {
myservo.write(pos);
delay(15);
}
movedEvening = true;
movedMorning = false; // Reset morning flag for the next cycle
Serial.println("Evening move completed.");
}
// A small delay to prevent the loop from running too fast,
// though the time checks handle event frequency.
delay(1000);
}
And now that it's properly formatted, it's fairly easy to see that whatever AI generated this code (a 78.92% score on the ZeroGPT AI detector, impressive!) forgot to call rtc.begin(); and then check to see if the DS3231 was even detected.