#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
Servo myServo;
const int soilMoisture1Pin = A0; // Soil Moisture Sensor 1 is connected to PIN A0 of Arduino Nano
const int soilMoisture2Pin = A1; // Soil Moisture Sensor 2 is connected to PIN A1 of Arduino Nano
const int relayPin = 8; // Relay is connected to PIN 8 of Arduino Nano
const int wateringTimes[] = {8, 9, 10, 11, 12, 13, 14, 15, 16}; // Array to hold watering times
const int servoPositions[] = {152, 137, 122, 107, 92, 77, 62, 47, 32}; // Corresponding servo positions for watering times
void setup() {
Serial.begin(9600);
myServo.attach(9); // Attach the Servo Motor to the PIN 9
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn the LCD Backlight ON.
pinMode(relayPin, OUTPUT); // Set Relay pin as OUTPUT
pinMode(soilMoisture1Pin, INPUT); // Set Soil Moisture Sensor 1 pin as INPUT
pinMode(soilMoisture2Pin, INPUT); // Set Soil Moisture Sensor 2 pin as INPUT
if (!rtc.begin()) {
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.print("RTC is NOT running!");
}
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int soilValue1 = analogRead(soilMoisture1Pin);
int soilValue2 = analogRead(soilMoisture2Pin);
Serial.print("Moisture Value 1: ");
Serial.print(soilValue1);
Serial.print(" | Moisture Value 2: ");
Serial.println(soilValue2);
lcd.setCursor(0, 1);
lcd.print("Moisture: ");
if (soilValue1 <= 250 && soilValue2 <= 250) {
lcd.print("WET");
digitalWrite(relayPin, HIGH);
} else if ((soilValue1 >= 300 && soilValue1 <= 400) || (soilValue2 >= 300 && soilValue2 <= 400)) {
lcd.print("NORMAL");
digitalWrite(relayPin, HIGH);
} else if (soilValue1 >= 700 || soilValue2 >= 700) {
lcd.print("DRY");
digitalWrite(relayPin, LOW);
}
for (int i = 0; i < sizeof(wateringTimes) / sizeof(wateringTimes[0]); i++) {
if (hour == wateringTimes[i]) {
myServo.write(servoPositions[i]);
break; // Once the appropriate time is found, exit the loop
}
}
lcd.setCursor(0, 0);
lcd.print("TIME ");
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.println(now.second());
delay(1000);
}