#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
DHT dht(27, DHT22);
////////////////////milli
unsigned long pre1 = 0;
unsigned long pre2 = 0;
unsigned long pre3 = 0;
unsigned long pre4 = 0;
unsigned long timer1 = 100;
unsigned long timer2 = 100;
unsigned long timer3 = 1000;
unsigned long timer4 = 1000;
unsigned long current1 = 0;
unsigned long current2 = 0;
unsigned long current3 = 0;
unsigned long current4 = 0;
int led1 = 0;
int led2 = 0;
int led3 = 0;
int led4 = 0;
////////////////////////////////////////
int x = 0;
int d;
int m;
int y;
int h;
int mi;
int s;
int dnum;
//////////////////////start time
int h1 = 0;
int m1 = 0;
int s1 = 0;
///////////////////stop time
int h2 = 0;
int m2 = 0;
int s2 = 0;
int pump = 5;
int fan = 4;
int lamp = 2;
int set = 14;
int up = 12;
int down = 13;
int st = 0;
int mode = 26;
int st1 = 0;
int temp;
int hum;
char daysOfTheWeek[7][6] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
pinMode(pump, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lamp, OUTPUT);
pinMode(mode, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
Serial.begin(115200);
dht.begin();
rtc.begin();
lcd.init();
lcd.backlight();
}
void loop() {
hum = dht.readHumidity();
temp = dht.readTemperature();
// date_time();
int sw = digitalRead(mode);
delay(200);
if (sw == 0) {
st1 = !st1;
}
if (st1 == 0) {
date_time();
} else {
ht();
}
Serial.println(st1);
int sw1 = digitalRead(set);
delay(500);
if (sw1 == 0) {
st++;
if (st >= 7) {
lcd.clear();
st = 0;
}
Serial.println(st);
delay(1000);
}
if (st == 0 && st1 == 0) {
date_time ();
// ht ();
} else if (st != 0 && st1 == 1) {
start ();
stop ();
}
Serial.print("Temp = ");
Serial.println(temp);
Serial.print("Hum = ");
Serial.println(hum);
dhum();
}////end loop
void date_time() {
DateTime now = rtc.now();
d = now.day();
m = now.month();
y = now.year();
h = now.hour();
mi = now.minute();
s = now.second();
dnum = now.dayOfTheWeek();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[dnum]);
lcd.setCursor(5, 0);
if (d <= 9) {
lcd.print("0");
}
lcd.print(d);
lcd.setCursor(7, 0);
lcd.print("/");
lcd.setCursor(8, 0);
if (m <= 9) {
lcd.print("0");
}
lcd.print(m);
lcd.setCursor(10, 0);
lcd.print("/");
y = y + 543;
lcd.setCursor(11, 0);
lcd.print(y);
lcd.setCursor(0, 1);
lcd.print("Time ");
lcd.setCursor(5, 1);
if (h <= 9) {
lcd.print("0");
}
lcd.print(h);
lcd.setCursor(7, 1);
lcd.print(":");
lcd.setCursor(8, 1);
if (mi <= 9) {
lcd.print("0");
}
lcd.print(mi);
lcd.setCursor(10, 1);
lcd.print(":");
lcd.setCursor(11, 1);
if (s <= 9) {
lcd.print("0");
}
lcd.print(s);
}
///////////////////////////Function Start
void start() {
if (st == 1) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
h1++;
if ( h1 >= 24) {
h1 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
h1--;
if ( h1 <= 0) {
h1 = 23;
}
}
lcd.setCursor(5, 0);
lcd.print(" ");
delay(200);
}
if (st == 2) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
m1++;
if ( m1 >= 60) {
m1 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
m1--;
if ( m1 <= 0) {
m1 = 59;
}
}
lcd.setCursor(9, 0);
lcd.print(" ");
delay(200);
}
if (st == 3) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
s1++;
if ( s1 >= 60) {
s1 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
s1--;
if ( s1 <= 0) {
s1 = 59;
}
}
lcd.setCursor(12, 0);
lcd.print(" ");
delay(200);
}
////////////////////////
lcd.setCursor(0, 0);
lcd.print("Start");
lcd.setCursor(6, 0);
if (h1 <= 9) {
lcd.print("0");
}
lcd.print(h1);
lcd.setCursor(8, 0);
lcd.print(":");
////////////////////////minute
lcd.setCursor(9, 0);
if (m1 <= 9) {
lcd.print("0");
}
lcd.print(m1);
lcd.setCursor(11, 0);
lcd.print(":");
////////////////////////second
lcd.setCursor(14, 0);
lcd.print(" ");
lcd.setCursor(12, 0);
if (s1 <= 9) {
lcd.print("0");
}
lcd.print(s1);
//lcd.setCursor(0, 0);
//lcd.print(String(h) + ":" + String(m) + ":" + String(s) + " ");
}////////////////////////end loop
///////////////////////////////////Function Stop
void stop() {
if (st == 4) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
h2++;
if ( h2 >= 24) {
h2 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
h2--;
if ( h2 <= 0) {
h2 = 23;
}
}
lcd.setCursor(6, 1);
lcd.print(" ");
delay(200);
}
if (st == 5) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
m2++;
if ( m2 >= 60) {
m2 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
m2--;
if ( m2 <= 0) {
m2 = 59;
}
}
lcd.setCursor(9, 1);
lcd.print(" ");
delay(200);
}
if (st == 6) {
int sw2 = digitalRead(up);
delay(50);
if (sw2 == 0) {
s2++;
if ( s2 >= 60) {
s2 = 0;
}
}
int sw3 = digitalRead(down);
delay(50);
if (sw3 == 0) {
s2--;
if ( s2 <= 0) {
s2 = 59;
}
}
lcd.setCursor(12, 1);
lcd.print(" ");
delay(200);
}
////////////////////////
lcd.setCursor(0, 1);
lcd.print("Stop");
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
if (h2 <= 9) {
lcd.print("0");
}
lcd.print(h2);
lcd.setCursor(8, 1);
lcd.print(":");
////////////////////////minute
lcd.setCursor(9, 1);
if (m2 <= 9) {
lcd.print("0");
}
lcd.print(m2);
lcd.setCursor(11, 1);
lcd.print(":");
////////////////////////second
lcd.setCursor(12, 1);
if (s2 <= 9) {
lcd.print("0");
}
lcd.print(s2);
lcd.setCursor(14, 1);
lcd.print(" ");
//lcd.setCursor(0, 0);
//lcd.print(String(h) + ":" + String(m) + ":" + String(s) + " ");
}
void ht() {
// hum = dht.readHumidity();
// temp = dht.readTemperature();
lcd.setCursor(0, 1);
lcd.print("Hum");
lcd.setCursor(5, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(hum);
lcd.setCursor(8, 1);
lcd.print("Temp");
lcd.setCursor(12, 1); //// clear
lcd.print(" ");
lcd.setCursor(13, 1);
lcd.print(temp);
}
///////////////////////////////////////////////////////////Function temp Hum
void dhum() {
if (hum < 70 ) {
current1 = millis();
Serial.println(hum);
if (current1 - pre1 >= timer1) {
pre1 = current1;
if (led1 == LOW) {
led1 = HIGH;
} else {
led1 = LOW;
}
digitalWrite(pump, led1);
if (led1 == HIGH) {
timer1 = 5000;
Serial.print("timer1 H =");
Serial.println(timer1);
} else {
(timer1 = 1000);
Serial.print("Timer1 L =");
Serial.println(timer1);
}
}
}
else {
digitalWrite(pump, LOW);
}
if (temp > 38) {
current2 = millis();
if (current2 - pre2 >= timer2) {
pre2 = current2;
if (led2 == LOW) {
led2 = HIGH;
} else {
led2 = LOW;
}
digitalWrite(fan, led2);
if (led2 == HIGH) {
timer2 = 5000;
Serial.print("timer2 H =");
Serial.println(timer2);
} else {
(timer1 = 1000);
Serial.print("Timer2 L =");
Serial.println(timer2);
}
}
} else if (temp <= 25) {
digitalWrite(fan, LOW);
}
if (temp < 25) {
digitalWrite(lamp, HIGH);
} else {
digitalWrite(lamp, LOW);
}
}