//  rtc22 set time start stop  delay milli
//  temp เกิน 38 พัดลมทำงาน 2นาที หยุด 1นาที จนได้ 25
//  ถ้าต่ำกว่า 25 หลอดไฟติด
//  ต่ำกว่า ปั้มทำงาน 2นาที หยุด 1นาทีจนถึง 80 หยุดทำงาน

#include "RTClib.h"                  //rtc  ต้องมี
#include <LiquidCrystal_I2C.h>       //lcd  ต้องมี
#include "DHT.h"                     //dht22

RTC_DS1307 rtc;                      // rtc ต้องมี
LiquidCrystal_I2C lcd(0x27, 16, 2 ); // lcd ต้องมี
DHT  dht(8, DHT22);                  //dht22
unsigned long pre1 =0;
unsigned long pre2 =0;
unsigned long pre3 =0;
unsigned long pre4 =0;

unsigned long timer1 =1000;
unsigned long timer2 =1000;
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 h = 0;      
int m = 0;
int s = 0;

int h1 = 0;
int m1 = 0;
int s1 = 0;

int h2 = 0;
int m2 = 0;
int s2 = 0;
int st = 0;
////dht22
int tmax = 38;  //temp >38 fan on 2 m stop 1 m to 25, <25 lamp on
int tmin = 25;
int hmax = 80;  //<70 pump on 2 m stop  1m check hum 80pump off
int hmin = 70; 

int pump = 13;   /////relay
int fan  = 12;   /////relay
int lamp = 11;   /////relay

int hum;
int temp;

///////////////////////////////


void setup() {
  dht.begin();                   // dht22
  rtc.begin();                   //ต้องมีrct
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(pump, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(lamp, OUTPUT);
  lcd.init();                   //ต้องมี lcd
  lcd.backlight();              //ต้องมี lcd
}///////////////////////////

void loop() {
  int sw1 = digitalRead(4);
  delay(100);
 if (sw1 == 0) {
    st++;
 if (st >= 7) {
      st = 0;
      lcd.clear();
    
    }
  }
 ///////////////////
  if ( st == 0){
  time();
  ht();
  } else{
    start ();
    stop();
  }
 
  
 
}////////////end loop

/////////////////////function ///////  start
void start() {
  if (st == 1) {
  int sw2 = digitalRead(3);
    delay(50);
  if (sw2 == 0) {
      h1++;
      if ( h1 >= 24) {
        h1 = 0;
      }
    }
   ///////// 
  int sw3 = digitalRead(5);
    delay(50);
  if (sw3 == 0) {
      h1--;
      if ( h1 <= 0) {
        h1 = 23;
      }
    }
    lcd.setCursor(6, 0);
    lcd.print("  ");
    delay(200);  ///////////500
  }

  if (st == 2) {
    int sw2 = digitalRead(3);
    delay(50);
  if (sw2 == 0) {
      m1++;
      if ( m1 >= 60) {
        m1 = 0;
      }
    }
  ///////////////////////////  
    int sw3 = digitalRead(5);
    delay(50);
  if (sw3 == 0) {
      m1--;
      if ( m1 <= 0) {
        m1 = 59;
      }
    }
    lcd.setCursor(9, 0);
    lcd.print("  ");
    delay(200);/////////////500
  }
  if (st == 3) {
    int sw2 = digitalRead(3);
    delay(50);
  if (sw2 == 0) {
      s1++;
  if ( s1 >= 60) {
        s1 = 0;
      }
    }
    int sw3 = digitalRead(5);
    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(12, 0);
  if (s1 <= 9) {
    lcd.print("0");
  }
  lcd.print(s1);
  
}///////////////end loop

//////////////////////////function stop
void stop() {
  if (st == 4) {
    int sw2 = digitalRead(3);
    delay(50);
    if (sw2 == 0) {
      h2++;
      if ( h2 >= 24) {
        h2 = 0;
      }
    }
    int sw3 = digitalRead(5);
    delay(50);
    if (sw3 == 0) {
      h2--;
      if ( h2 <= 0) {
        h2 = 23;
      }
    }
    lcd.setCursor(6, 1);
    lcd.print("  ");
    delay(200);/////////////////////500
  }
  if (st == 5) {
    int sw2 = digitalRead(3);
    delay(50);
    if (sw2 == 0) {
      m2++;
      if ( m2 >= 60) {
        m2 = 0;
      }
    }
    int sw3 = digitalRead(5);
    delay(50);
    if (sw3 == 0) {
      m2--;
      if ( m2 <= 0) {
        m2 = 59;
      }
    }
    lcd.setCursor(9, 1);
    lcd.print("  ");
    delay(200);///////////500
  }
  if (st == 6) {
    int sw2 = digitalRead(3);
    delay(50);
    if (sw2 == 0) {
      s2++;
      if ( s2 >= 60) {
        s2 = 0;
      }
    }
    int sw3 = digitalRead(5);
    delay(50);
    if (sw3 == 0) {
      s2--;
      if ( s2 <= 0) {
        s2 = 59;
      }
    }
    lcd.setCursor(12, 1);
    lcd.print("  ");
    delay(200); //////////////500
  }
  ////////////////////////
  lcd.setCursor(0, 1);         //copy
  lcd.print("Stop");
   lcd.setCursor(4, 1);    ////////clear
   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); ////////clear
  lcd.print("  ");
}/////////////////////////end loop


void time() {/////////////////////////////functiontime
  DateTime now = rtc.now();   /////// rtc
 h = now.hour();
 m = now.minute();
 s = now.second();
 hum = dht.readHumidity();          ////hum
 temp = dht.readTemperature();      ////temp

 lcd.setCursor(0, 0);
  lcd.print("Time");
  lcd.setCursor(6, 0);
  if (h <= 9) {
    lcd.print("0");
  }
  lcd.print(h);
  lcd.setCursor(8, 0);
  lcd.print(":");
  ////////////////////////minute
  lcd.setCursor(9, 0);
  if (m <= 9) {
    lcd.print("0");
  }
  lcd.print(m);
  lcd.setCursor(11, 0);
  lcd.print(":");
  lcd.setCursor(12, 0);
  if (s <= 9) {
    lcd.print("0");
  }
  lcd.print(s);
}////////////////////////end loop

void ht(){       /////////////////////function dht22
  hum  = dht.readHumidity();   // อ่าน hum
  temp = dht.readTemperature();// อ่าน temp
  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);

  
  if(hum<70){  /////////////////////// function hum

   current1 = millis();          // from milli

    if (current1 - pre1 >= timer1) { ///////////// led1
      Serial.print( "timer on"); // more
      Serial.println(pre1);
      pre1 = current1;

    if (led1 == LOW) {      ////// ติดดับเท่ากัน
      led1 = HIGH;
    } else {
      led1 = LOW;
    }
    digitalWrite(13, led1);
    if (led1 == HIGH){       ////// ติดดับไม่เท่ากัน
      timer1 = timer1 + 120000;
    } else {
      (timer1 + 60000);
    }
 }
   }
 if (temp > 38) { //////////////////temp

   current2 = millis();              // from milli

  if (current2 - pre2 >= timer2) { ////////////// led2
      Serial.print( "timer off"); 
      Serial.println(pre2);
      pre2 = current2;

    if (led2 == LOW) {
      led2 = HIGH;
    } else {
      led2 = LOW;
    }
    digitalWrite(12, led2);
    if (led2 == HIGH){       
      timer2 = timer2 + 1000;
    } else {
      (timer2 + 1000);
    }
    
  }
  } 
  else if( temp <= 25){
    digitalWrite(fan, LOW);
  }
 if (temp <25){
  digitalWrite(lamp, HIGH);
 }
 else {
    digitalWrite(lamp, LOW);
    
 }

 

}///////////////////////end loop


  
GND5VSDASCLSQWRTCDS1307+
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module