#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
// Initialize RTC and Servo
RTC_DS3231 rtc;
Servo myServo;
// Define servo pin
const int servoPin = 9;
// Feeding times (24-hour format)
const int feedHour1 = 14;
const int feedMinute1 = 48;
const int feedHour2 = 14;
const int feedMinute2 = 50;
// Feeding duration in milliseconds
const int feedingDuration = 3000;
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
// Set the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Attach the servo to the pin
myServo.attach(servoPin);
// Set servo to initial position
myServo.write(0);
}
void loop() {
// Get the current time
DateTime now = rtc.now();
int currentHour = now.hour();
int currentMinute = now.minute();
// Check if it's feeding time
if ((currentHour == feedHour1 && currentMinute == feedMinute1) ||
(currentHour == feedHour2 && currentMinute == feedMinute2)) {
feedFish();
// Wait for a minute to avoid feeding multiple times within the same minute
delay(60000);
}
// Small delay to prevent excessive loop iterations
delay(1000);
}
void feedFish() {
Serial.println("Feeding fish...");
myServo.write(90); // Rotate servo to 90 degrees to dispense food
delay(feedingDuration); // Wait for the duration
myServo.write(0); // Rotate servo back to 0 degrees
}