//esp32 smartfarm
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"

LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;

int day;///////////
int month;
int year;

int hour;///////
int minute;
int second;
int dnum;  //more

int pump = 5;   /////relay
int fan  = 4;   /////relay
int lamp = 2;   /////relay

int set = 14;
int up = 12;
int down = 13;

int st = 0;

//int h = 0;      
//int m = 0;
//int s = 0;
//////////////start time
int h1 = 0;
int m1 = 0;
int s1 = 0;
/////////////stop time
int h2 = 0;
int m2 = 0;
int s2 = 0;

char daysOfTheWeek[7][6] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};//more


void setup() {
  
  pinMode( pump, OUTPUT);
  pinMode( fan, OUTPUT);
  pinMode( lamp, OUTPUT);

  pinMode( set, INPUT_PULLUP);
  pinMode( up, INPUT_PULLUP);
  pinMode( down, INPUT_PULLUP);

  Serial.begin(115200);
  rtc.begin();
  lcd.init();
  lcd.backlight();


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


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

void Date_Time(){         ////////fuction Date_Time
 DateTime now = rtc.now(); //
  day    = now.day();
  month  = now.month();
  year   = now.year();
  hour   = now.hour();
  minute = now.minute();
  second = now.second();
  dnum   = now.dayOfTheWeek(); /////more 2 day no s (daysOfTheWeek[now.dayOfTheWeek()]);

  //////////////////////////////date
  lcd.setCursor(0, 0);
  lcd.print(daysOfTheWeek[dnum]);//more 3
  lcd.setCursor(6, 0);

  if (day <= 9) {
    lcd.print("0");
  }
  lcd.print(day); 
  lcd.setCursor(8, 0);
  lcd.print("/");
  lcd.setCursor(9, 0);

  if (month <= 9) {
    lcd.print("0");
  }
  lcd.print(month);
  lcd.setCursor(11, 0);
  lcd.print("/");
  lcd.setCursor(12, 0);
  lcd.print(year);
  lcd.setCursor(0, 1);
  
   //////////////////////////////time
  lcd.print("Time");
  lcd.setCursor(6, 1);

  if (hour <= 9){       ////////////h
    lcd.print("0");
  }
  lcd.print(hour);
  lcd.setCursor(8, 1);
  lcd.print(":");
  lcd.setCursor(9, 1);

  if (minute <= 9) {    /////////////m
    lcd.print("0");
  }
  lcd.print(minute);
  lcd.setCursor(11, 1);
  lcd.print(":");
  lcd.setCursor(12, 1);

  if (second <= 9) {    //////////////s
    lcd.print("0");
  }
  lcd.print(second);
}/////////////////////////end loop

/*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
$abcdeabcde151015202530fghijfghij
GND5VSDASCLSQWRTCDS1307+
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module