#include <Wire.h> // for I2C communication
#include <LiquidCrystal_I2C.h> // for LCD display
  int menu= 0; // variable to store the menu option
  float temperature; // variable to store the temperature value
  float humidity; // variable to store the humidity value

   
  float  R_humiditiy   = 50;
  float  R_temperature = 37;
  float Max_temperature =58;
  float Max_humiditiy   =120;
  float Min_temperature =10;
  float Min_humiditiy   =20;
  float kp =17;
  float ki =1.7;
  float kd =0.17;

  unsigned long prevTime = millis();
  

#define RELAY_PIN 33 // pin of the relay that controls the motor
#define BUTTON1_PIN 5 // pin of the first button
#define BUTTON2_PIN 19 // pin of the second button
#define BUTTON3_PIN 4 // pin of the third button
#define BUTTON4_PIN 18 // pin of the fourth button

 LiquidCrystal_I2C lcd(0x27, 16, 2); // create an LCD object with address 0x27 and size 16*2
 float setting[]={0,0,0,0,0,0,0,0,0};
 void select_menue(char add_mines , int menue , float val_apDate , float max_val , float min_val , float reslsion);

    void setup(){
     
      setup_lcd();
      void ad(int menue , float val_apDate , float max_val , float reslsion);
   }
   void loop(){
     for ( int i=0; i>1000;i++ ) {
     
       Serial.print(i );
     }
     loop_lcd(); 
     for ( int i=0; i>1000;i++ ) {
     
       Serial.print(i );
     }
    
   }


  void setup_lcd(){
    Serial.begin(9600); // start serial communication for debugging
  Wire.begin(); // start I2C communication
  lcd.init(); // initialize LCD display
  lcd.backlight(); // turn on LCD backlight
 // sht20.initSHT20(); // initialize SHT20 sensor
  
  pinMode(RELAY_PIN, OUTPUT); // set relay pin as output
  pinMode(BUTTON1_PIN, INPUT_PULLUP); // set button1 pin as input with internal pullup resistor
  pinMode(BUTTON2_PIN, INPUT_PULLUP); // set button2 pin as input with internal pullup resistor
  pinMode(BUTTON3_PIN, INPUT_PULLUP); // set button3 pin as input with internal pullup resistor
  pinMode(BUTTON4_PIN, INPUT_PULLUP); // set button4 pin as input with internal pullup resistor

    setting[0] = R_humiditiy;
    setting[1] = R_temperature ;
    setting[2] = Max_temperature ;
    setting[3] = Max_humiditiy   ;
    setting[4] = Min_temperature;
    setting[5] = Min_humiditiy   ;
    setting[6] = kp ;
    setting[7] = ki ;
    setting[8] = kd ;

  }

   void loop_lcd(){


     unsigned long currentTime = millis(); 
      if (currentTime - prevTime > 1) {


      if (digitalRead(BUTTON1_PIN) == LOW) { // if button1 is pressed
      menu = (menu + 1) % 11; // change menu option (0: main screen, 1: set temperature, 2: about)
      delay(20); // debounce delay
          }
  
      if (digitalRead(BUTTON2_PIN) == LOW) { // if button2 is pressed

        
       if (menu == 1) {select_menue('+', 0, R_temperature, 38 , 0.1); }
       else if (menu == 2) {select_menue('+', 1, R_humiditiy, 70  , 3);}
       else if (menu == 3) {select_menue('+', 2, Max_temperature, 42  , 0.1); }
       else if (menu == 4) {select_menue('+', 3, Max_humiditiy,   120  , 3  );}
       else if (menu == 5) {select_menue('+', 4, Min_temperature, 20   , 0.1);}
       else if (menu == 6) {select_menue('+', 5 , Min_humiditiy,  60   , 1  );}
       else if (menu == 7) {select_menue('+', 6 , kp,  111   , 0.7  );}
       else if (menu == 8) {select_menue('+', 7 , ki,  77    , 0.1  );}
       else if (menu == 9) {select_menue('+', 8 , kd,  77    , 0.1  );}

    delay(20); // debounce delay
  }
  
   if (digitalRead(BUTTON3_PIN) == LOW) { // if button3 is pressed

       if (menu == 1) {select_menue('-',  R_temperature,  0 , 30  , 0.1);}
       else if (menu == 2) {select_menue('-', 1, R_humiditiy, 40 , 3);}
       else if (menu == 3) {select_menue('-', 2, Max_temperature, 37   , 0.1);}
       else if (menu == 4) {select_menue('-', 3, Max_humiditiy,  50  , 3  );}
       else if (menu == 5) {select_menue('-', 4, Min_temperature ,15  , 0.1);}
       else if (menu == 6) {select_menue('-', 5 , Min_humiditiy,   40   , 1  );}
       else if (menu == 7) {select_menue('-', 6 , kp,  111      , 0.7  );}
       else if (menu == 8) {select_menue('-', 7 , ki,  77     , 0.1  );}
       else if (menu == 9) {select_menue('-', 8 , kd,  77     , 0.1  );}


    delay(20); // debounce delay
   }
   
   
   if (digitalRead(BUTTON4_PIN) == LOW) { // if button4 is pressed
     menu = 0;
   }
   
   switch (menu) { // display different screens depending on menu option
   case 0: // main screen
  lcd.clear(); // clear LCD display
  lcd.setCursor(0, 0); // set cursor position to first row, first column
  lcd.print("Temp: "); // print "Temp: "
  lcd.print("tempe"); // print temperature value
  lcd.print(" C"); // print " C"
  Serial.println("Temp:temperature c ");
  lcd.setCursor(0, 1); // set cursor position to second row, first column
  lcd.print("Hum: "); // print "Hum: "
  lcd.print("humi"); // print humidity value
  lcd.print(" %"); // print " %"
  Serial.println("Hum:%");
  lcd.setCursor(10, 1); // set cursor position to second row, tenth column
  /*lcd.print("Set: "); // print "Set: "
  lcd.print(setpoint); // print setpoint value
  lcd.print(" C");*/ // print " C"
  Serial.println("Set: " );
  Serial.print(R_temperature );
  Serial.print(" C" );
  delay(200);
   break; // exit switch statement

   case 1: // set temperature screen
    write_on_Lcd(0, 0, "Set Temp : ", R_temperature, "C", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 2: // set temperature screen
    write_on_Lcd(0, 0, "Set hum: ", R_humiditiy, "%", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 3: // set temperature screen
    write_on_Lcd(0, 0, "Set MaxTem: ", Max_temperature, "C", 0, 1,"B2:+ B3:- B1:Back"); break; // exit switch statement
   case 4: // set temperature screen
    write_on_Lcd(0, 0, "Set Maxhum: ", Max_humiditiy, "%", 0, 1,"B2:+ B3:- B1:Back"); break; // exit switch statement
   case 5: // set temperature screen
    write_on_Lcd(0, 0, "Set MinTem: ", Min_temperature, "C", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 6: // set temperature screen
    write_on_Lcd(0, 0, "Set MinTem: ", Min_humiditiy, "%", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 7: // set temperature screen
   write_on_Lcd(0, 0, "Set kp: ", kp, "", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 8: // set temperature screen
    write_on_Lcd(0, 0, "Set ki: ", ki, "", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement
   case 9: // set temperature screen
     write_on_Lcd(0, 0, "Set kd: ", kd, "", 0, 1,"B2:+ B3:- B1:Back");break; // exit switch statement

  case 10: // about screen
  lcd.clear(); // clear LCD display
  lcd.setCursor(0, 0); // set cursor position to first row, first column
  lcd.print("Smart Incubator"); // print project name
  lcd.setCursor(0, 1); // set cursor position to second row, first column
  lcd.print("By User B");
  Serial.println("Smart Incubator" ); // print your name or nickname
  delay(200);
   break; // exit switch statement
  }
prevTime = currentTime; 

} 
   }

   void select_menue(char add_mines , int menue , float val_apDate , float max_val  , float reslsion){

     if(add_mines == '+'){
        if(val_apDate < max_val ){
        setting[menue] =val_apDate + reslsion ;
        }
        else{ setting[menue]= setting[menue];    }
     }

     else if(add_mines == '-'){
        if(val_apDate < max_val ){
        setting[menue] =val_apDate - reslsion ;
        }
        else{ setting[menue]= setting[menue];    }
     }
   

     R_humiditiy   = setting[0];
     R_temperature = setting[1];
     Max_temperature =setting[2];
     Max_humiditiy   =setting[3];
     Min_temperature =setting[4];
     Min_humiditiy   =setting[5];
     kp =setting[6];
     ki =setting[7];
     kd =setting[8];

   

}

void write_on_Lcd(int x, int y,String s ,float f , String s1, int x2, int y2, String s2 ){
    lcd.clear();
    lcd.setCursor(x,y); 
    lcd.print(s); 
    lcd.print(f); 
    lcd.print(s1); 
    lcd.setCursor(x2, y2);
    lcd.print(s2); 

    Serial.println("Set: " );
    Serial.print( f );
    Serial.print(s1 );
    Serial.println(s2);
    delay(200);
}