#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <avr/wdt.h> // Include watchdog timer library
Servo servo1;
Servo servo2;
Servo servo3;
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int melody[] = {600, 500, 0, 600, 500, 0, 600, 500, 0};
int noteDurations[] = {300, 800, 800, 300, 800, 800, 300, 800, 800};
const int relayPin1 = 13;
const int relayPin2 = 6;
const int relayPin3 = 5;
const int speakerPin = 2;
const int threshold = 50;
unsigned long startTime = 0; // Variable to store the start time of the watering process
const unsigned long wateringDuration = 600000; // 10 minutes in milliseconds
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
pinMode(speakerPin, OUTPUT);
lcd.init();
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("BETECH-ECET");
lcd.setCursor(1, 1);
lcd.print("S/Y 2023-2024");
delay(5000);
servo1.attach(relayPin1);
servo2.attach(relayPin2);
servo3.attach(relayPin3);
servo1.write(00);
servo2.write(00);
servo3.write(00);
}
void loop() {
DateTime now = rtc.now();
int currentHour = now.hour();
int currentMinute = now.minute();
int currentSecond = now.second();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Diligan");
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(currentHour);
lcd.print(":");
lcd.print(currentMinute);
lcd.print(":");
lcd.print(currentSecond);
digitalWrite(speakerPin, LOW);
// Check if it's time to water EDUC
if ((currentHour == 5 && currentMinute == 00 && currentSecond == 00) || (currentHour == 7 && currentMinute == 00 && currentSecond == 00) ||
(currentHour == 9 && currentMinute == 00 && currentSecond == 00) || (currentHour == 11 && currentMinute == 00 && currentSecond == 00) ||
(currentHour == 13 && currentMinute == 00 && currentSecond == 00) || (currentHour == 15 && currentMinute == 00 && currentSecond == 00) ||
(currentHour == 19 && currentMinute == 33 && currentSecond == 00) || (currentHour == 19 && currentMinute == 00 && currentSecond == 00)) {
lcd.clear();
Serial.println("soil1 is DRY");
lcd.setCursor(4, 0);
lcd.print("CAUTION");
lcd.setCursor(1, 1);
lcd.print("Watering EDUC");
playTone();
delay(500);
servo1.write(90);
servo2.write(90);
lcd.clear();
startTime = millis();
// Store the start time of the watering process
}
// Check if it's time to water PARK
if ((currentHour == 5 && currentMinute == 50 && currentSecond == 00) || (currentHour == 14 && currentMinute == 5 && currentSecond == 00) ||
(currentHour == 19 && currentMinute == 45 && currentSecond == 00)) {
Serial.println("soil2 is DRY");
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("CAUTION");
lcd.setCursor(1, 1);
lcd.print("Watering PARK");
playTone();
delay(500);
servo1.write(90);
servo3.write(90);
lcd.clear();
startTime = millis(); // Store the start time of the watering process
}
// Check if it's time to stop watering EDUC (after 10 minutes)
if (startTime > 0 && millis() - startTime >= wateringDuration) {
servo1.write(00);
servo2.write(00);
servo3.write(00);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("Done");
delay(1000);
lcd.clear();
startTime = 0; // Reset the start time
}
// Display elapsed time since watering started
if (startTime > 0) {
lcd.setCursor(3, 0);
lcd.print("Nag didilig");
unsigned long elapsedTime = millis() - startTime;
unsigned long elapsedMinutes = elapsedTime / 60000; // Convert milliseconds to minutes
lcd.setCursor(0, 1);
lcd.print("Naka agi: ");
lcd.print(elapsedMinutes);
lcd.print(" min");
}
delay(1000);
}
void playTone() {
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int noteDuration = noteDurations[i] * 2;
if (melody[i] == 0) {
delay(noteDuration);
} else {
tone(speakerPin, melody[i], noteDuration);
delay(noteDuration * 1.2);
noTone(speakerPin);
}
}
}