#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <SD.h>
#include <DHT.h>
#define DHTPIN1 2
#define DHTPIN2 3
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int soilSensor1 = A0;
const int soilSensor2 = A1;
const int soilSensor3 = A2;
const int relay1 = 4;
const int relay2 = 5;
const int relay3 = 6;
const int chipSelect = 53;
const int buttonPin = 7; // Pin connected to the button
int moistureThreshold = 50; // Initial soil moisture threshold percentage
void setup() {
Serial.begin(9600);
dht1.begin();
dht2.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
lcd.begin(20, 4);
lcd.backlight();
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Configure the button pin as input with internal pull-up resistor
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
void loop() {
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
int rawMoisture1 = analogRead(soilSensor1);
int rawMoisture2 = analogRead(soilSensor2);
int rawMoisture3 = analogRead(soilSensor3);
int moisture1 = map(rawMoisture1, 0, 1023, 0, 100);
int moisture2 = map(rawMoisture2, 0, 1023, 0, 100);
int moisture3 = map(rawMoisture3, 0, 1023, 0, 100);
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(buttonPin) == LOW) { // Confirm the button is still pressed
moistureThreshold += 10; // Increment the threshold by 10%
if (moistureThreshold > 100) {
moistureThreshold = 0; // Reset to 0% if it exceeds 100%
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Threshold:");
lcd.print(moistureThreshold);
lcd.print("%");
delay(1000); // Display the new threshold for 1 second
}
}
DateTime now = rtc.now();
String dataString = String(now.timestamp(DateTime::TIMESTAMP_FULL)) + "," +
String(t1) + "," + String(h1) + "," +
String(t2) + "," + String(h2) + "," +
String(moisture1) + "," +
String(moisture2) + "," +
String(moisture3);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
} else {
Serial.println("error opening datalog.txt");
}
lcd.setCursor(1, 0);
lcd.print("CHAMROEUN AE29 RUA ");
lcd.setCursor(0, 1);
lcd.print("T1:");
lcd.print(t1);
lcd.setCursor(11, 1);
lcd.print("T2:");
lcd.print(t2);
lcd.setCursor(0, 2);
lcd.print("H1:");
lcd.print(h1);
lcd.setCursor(11, 2);
lcd.print("%H2:");
lcd.print(h2);
lcd.print("%");
lcd.setCursor(0, 3);
lcd.print("M1:");
lcd.print(moisture1);
lcd.print("% M2:");
lcd.print(moisture2);
lcd.print("% M3:");
lcd.print(moisture3);
lcd.print("%");
if (moisture1 < moistureThreshold) {
digitalWrite(relay1, LOW);
} else {
digitalWrite(relay1, HIGH);
}
if (moisture2 < moistureThreshold) {
digitalWrite(relay2, LOW);
} else {
digitalWrite(relay2, HIGH);
}
if (moisture3 < moistureThreshold) {
digitalWrite(relay3, LOW);
} else {
digitalWrite(relay3, HIGH);
}
delay(9600);
}