#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Servo.h>
Servo motor;
RTC_DS1307 rtc;
char dayNames[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String day;
int date, month, year, hour, minute, second;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20x4 LCD display
void setup() {
Serial.begin(9600);
// Servo
motor.attach(5);
motor.write(0); // Start at 0 position
// LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
// RTC
if (!rtc.begin()) {
Serial.println("RTC NOT READABLE");
while (1); // Halt if RTC is not found
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Uncomment this if you want to set RTC from compile time
}
}
void loop() {
// RTC
DateTime now = rtc.now();
day = dayNames[now.dayOfTheWeek()];
date = now.day();
month = now.month();
year = now.year();
hour = now.hour();
minute = now.minute();
second = now.second();
Serial.println(String() + day + ", " + date + "-" + month + "-" + String(year));
Serial.println(String() + hour + ":" + minute + ":" + second);
// LCD Display the current day, date, and time
lcd.setCursor(0, 0);
lcd.print(day + ", " + String(date) + "-" + String(month) + "-" + String(year));
lcd.setCursor(0, 1);
lcd.print(String(hour) + ":" + String(minute) + ":" + String(second));
// Servo Movement Condition - Feed every 10 seconds
if (second % 10 == 0) { // Check if second is a multiple of 10
feeding(1); // Adjust feeding amount if needed
delay(1000); // Wait to avoid multiple triggers within the same second
}
// Servo Movement Condition - Feed at a specific time (e.g., 21:44:01)
if (hour == 21 && minute == 44 && second == 1) {
feeding(2);
delay(1000); // Wait to prevent multiple triggers within the same second
}
// Display "Time to clean me" at the start of every minute (when second is 0)
if (second == 0) {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("CLEEAN ME");
delay(2000); // Wait for 2 seconds to display the message
lcd.clear(); // Clear the screen after the message
}
}
void feeding(int amount) {
for (int i = 0; i < amount; i++) {
// Servo movement
motor.write(150); // Move servo to feeding position
delay(1000); // Wait for 1 second
motor.write(0); // Return servo to initial position
delay(1000); // Wait for 1 second
}
}