#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <EEPROM-Storage.h>
 
const int solarTempSensor = A0; // solar temperature sensor
const int boilerTempSensor = A1; // boiler temperature sensor
const int stoveTempSensor = A2; // stove temperature sensor

int solarPump = 9; // solar pump pin
int stovePump = 10; // stove pump pin
int electricPump = 11; // electric pump pin

int solarTemp; // variable to store solar temperature
int boilerTemp; // variable to store boiler temperature
int stoveTemp; // variable to store stove temperature
const float BETA = 3950;
LiquidCrystal_I2C lcd(0x27, 16, 2);





const int upButton = 2;
const int downButton = 3;
const int leftButton = 4;
const int rightButton = 5;


int menuState = 0;
 


float celsiuss = 1 ;
float celsiusb = 1 ;
float celsiusst = 1 ;
float hissol = 10;
float hisstove = 10;
float TBmax = 90 ;
float TBSol = 90 ;
float TBStove = 40 ;
float TBEL = 50 ;
float DIFEL = 10;
bool EL = false;
  

bool SOLX = false;
bool TBX = false;
bool STOVEX = false;
bool SOL = false;
bool STOVE = false;


int address = 0;
int val = 0;

void setup() {
    pinMode(solarPump, OUTPUT);
  pinMode(stovePump, OUTPUT);
  pinMode(electricPump, OUTPUT);
    Serial.begin(9600);
    lcd.begin(20, 4);
  lcd.backlight();
 




pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(leftButton, INPUT_PULLUP);
  pinMode(rightButton, INPUT_PULLUP);

  
 



}

void t1() {
  
     lcd.setCursor(0, 0); 
  lcd.print("ERRPR");
  
}



void loop() {


  
  solarTemp = analogRead(solarTempSensor); // read solar temperature
  boilerTemp = analogRead(boilerTempSensor); // read boiler temperature
  stoveTemp = analogRead(stoveTempSensor); // read stove temperature
  celsiuss = 1 / (log(1 / (1023. / solarTemp - 1)) / BETA + 1.0 / 298.15) - 273.15;
  celsiusb = 1 / (log(1 / (1023. / boilerTemp - 1)) / BETA + 1.0 / 298.15) - 273.15;
  celsiusst = 1 / (log(1 / (1023. / stoveTemp - 1)) / BETA + 1.0 / 298.15) - 273.15;

     if (celsiusb >= TBmax && menuState == 0) {     //**************************
     lcd.setCursor(0, 0); 
     lcd.print("Pregrejan Bojler!!! ");         //Bojler max Temp
     lcd.setCursor(0, 1);
     lcd.print(celsiusb);
     lcd.print(" C  !!!");
     lcd.print("       ");   
     lcd.setCursor(0, 2);
     lcd.print("                   "); 
     TBX = true;}
     else if (celsiusb < TBmax){
     TBX = false;} //********************************                                           
     

   if (celsiuss > celsiusb + hissol & TBX == false & celsiusb <= TBSol & SOL == true) {   //*********************
   digitalWrite(solarPump, HIGH);                          
   SOLX = true;}                                         //  Solar  
   else if (celsiuss <= celsiusb | TBX == true){
   digitalWrite(solarPump, LOW);                 
   SOLX = false;}                                        //****************************************
                          
  
   if (TBX == true | SOLX == true | celsiusst <= celsiusb) {                     //****************************************
   digitalWrite(stovePump, LOW); 
   STOVEX = false;}
   else if (celsiusst > celsiusb + hisstove & celsiusb <= TBStove & STOVE == true) {      //   Kotao
   digitalWrite(stovePump, HIGH);      
   STOVEX = true;}                                      
                                         //****************************************


    if (TBX == true | SOLX == true | STOVEX == true | EL == false | celsiusb >= TBEL | celsiusb <= TBEL-DIFEL){  
      digitalWrite(electricPump, LOW);}                  //   EL. Grejac    
      else {
      digitalWrite(electricPump, HIGH);}           
      //****************************************

  

  int upState = digitalRead(upButton);                   //****************************************
  int downState = digitalRead(downButton);
  int leftState = digitalRead(leftButton);               //  Varijable za prekidace
  int rightState = digitalRead(rightButton);             //****************************************


  if (upState == LOW) {                                  //****************************************
    delay(200);  }
  if (downState == LOW) {    
    delay(200);  }
  if (leftState == LOW) {
    menuState = menuState - 1;                           //  Funkcije prekidaca
    lcd.clear();
    delay(500);  }
  if (rightState == LOW) {
    menuState = menuState + 1;
    lcd.clear();
    delay(500);  }                                       //****************************************

  
  switch (menuState) {
         
    case 1:
    
    if (TBmax > 90){
      TBmax = 90;}
    if (TBmax < 0){
      TBmax = 0;}  
    if (upState == LOW) {
    TBmax  = TBmax + 1; }
    if (downState == LOW) {
    TBmax  = TBmax - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("TBMAX ");
    lcd.print(TBmax);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno 90 C ");
          
      break;

    case 2:
    if (TBSol > TBmax - 5){
      TBSol = TBmax - 5;}
    if (TBSol < 0){
      TBSol = 0;}  
    if (upState == LOW) {
    TBSol  = TBSol + 1; }
    if (downState == LOW) {
    TBSol  = TBSol - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("TBSol ");
    lcd.print(TBSol);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno ");
    lcd.print(TBmax -5);
    lcd.print(" C ");
      break;

    case 3:
    if (TBStove > TBmax - 5){
      TBStove = TBmax - 5;}
    if (TBStove < 0){
      TBStove = 0;}   
    if (upState == LOW) {
    TBStove  = TBStove + 1; }
    if (downState == LOW) {
    TBStove  = TBStove - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("TBStove ");
    lcd.print(TBStove);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno ");
    lcd.print(TBmax -5);
    lcd.print(" C ");
      break;

    case 4:
    if (TBEL > TBmax - 5){
      TBEL = TBmax - 5;}
    if (TBEL < 0){
      TBEL = 0;}   
    if (upState == LOW) {
    TBEL  = TBEL + 1; }
    if (downState == LOW) {
    TBEL  = TBEL - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("TBEL ");
    lcd.print(TBEL);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno ");
    lcd.print(TBmax -5);
    lcd.print(" C "); 
      break;

    case 5:
    if (hissol > 10){
      hissol = 10;}
    if (hissol < 0){
      hissol = 0;}   
    if (upState == LOW) {
    hissol  = hissol + 1; }
    if (downState == LOW) {
    hissol  = hissol - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("hissol ");
    lcd.print(hissol);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno 10 C "); 
      break;

    case 6:
    if (hisstove > 10){
      hisstove = 10;}
    if (hisstove < 0){
      hisstove = 0;}   
    if (upState == LOW) {
    hisstove  = hisstove + 1; }
    if (downState == LOW) {
    hisstove  = hisstove - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("hisstove ");
    lcd.print(hisstove);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno 10 C "); 
      break;

    case 7:
    if (DIFEL > 10){
      DIFEL = 10;}
    if (DIFEL < 0){
      DIFEL = 0;}   
    if (upState == LOW) {
    DIFEL  = DIFEL + 1; }
    if (downState == LOW) {
    DIFEL  = DIFEL - 1; }
    lcd.setCursor(0, 0); 
    lcd.print("DIFEL ");
    lcd.print(DIFEL);
    lcd.print(" C");
    lcd.setCursor(0, 3); 
    lcd.print("Maximalno 10 C "); 
      break;
        
    case 8:
    if (SOL == false){
    lcd.setCursor(0, 0); 
    lcd.print("Kolektori Iskljuceni");}
    else { lcd.setCursor(0, 0); 
    lcd.print("Kolektori Ukljuceni ");}
    if (upState == LOW) {
    SOL  = true; 
    lcd.setCursor(0, 0); 
    lcd.clear();
    lcd.print("Kolektori Uskljuceni ");}
    if (downState == LOW) {
    SOL = false;
    lcd.setCursor(0, 0); 
    lcd.setCursor(0, 0); 
    lcd.print("Kolektori Iskljuceni");} 
      break;
          
    case 9:
    if (STOVE == false){
    lcd.setCursor(0, 0); 
    lcd.print("Kotao Iskljucen");}
    else { lcd.setCursor(0, 0); 
    lcd.print("Kotao Ukljucen ");}
    if (upState == LOW) {
    STOVE  = true; 
    lcd.setCursor(0, 0); 
    lcd.clear();
    lcd.print("Kotao Ukljucen ");}
    if (downState == LOW) {
    STOVE = false;
    lcd.setCursor(0, 0); 
    lcd.setCursor(0, 0); 
    lcd.print("Kotao Iskljucen");} 
      break;
          
    case 10:
    if (EL == false){
    lcd.setCursor(0, 0); 
    lcd.print("EL. Grejac Iskljucen");}
    else { lcd.setCursor(0, 0); 
    lcd.print("EL. Grejac Ukljucen");}
    if (upState == LOW) {
    EL  = true; 
    lcd.setCursor(0, 0); 
    lcd.clear();
    lcd.print("EL. Grejac Ukljucen");}
    if (downState == LOW) {
    EL = false;
    lcd.setCursor(0, 0); 
    lcd.setCursor(0, 0); 
    lcd.print("EL. Grejac Iskljucen");}
      break;
   
      default:                                   //****************************************      
     if (celsiusb < TBmax) {
     lcd.setCursor(0, 0);                        
     lcd.print("Solar Temp ");
     lcd.print(celsiuss);
     lcd.print(" C ");
     lcd.setCursor(0, 1);                        ///     Default status menija
     lcd.print("Kotao Temp ");
     lcd.print(celsiusst);
     lcd.print(" C ");
     lcd.setCursor(0, 2);
     lcd.print("Bojler Temp ");
     lcd.print(celsiusb);
     lcd.print(" C ");}                           ///**************************************** 

     int SState = digitalRead(9);                ///**************************************** 
     if (SOL == false){
     lcd.setCursor(0, 3);
     lcd.print("S XX ");}

     else if (SState == HIGH) {
     lcd.setCursor(0, 3);
     lcd.print("S ON ");}                        ///  Status Pumpe SOL 
     else {
     lcd.setCursor(0, 3);
     lcd.print("S OFF");  }                      ///**************************************** 

     int KState = digitalRead(10);               ///**************************************** 
     if (STOVE == false){
     lcd.setCursor(6, 3);
     lcd.print("K XX ");}

     else if (KState == HIGH) {
     lcd.setCursor(6, 3);
     lcd.print("K ON ");}                        ///  Status Pumpe Kotla
     else {
     lcd.setCursor(6, 3);
     lcd.print("K OFF");  }                      ///**************************************** 
  
     int EState = digitalRead(11);               ///**************************************** 
     if (EL == false){
     lcd.setCursor(12, 3);
     lcd.print("E XX ");}
    
     else if (EState == HIGH) {                  ///  Status EL. Grejaca
     lcd.setCursor(12, 3);
     lcd.print("E ON ");}
     else {
     lcd.setCursor(12, 3);
     lcd.print("E OFF");
     break;  }                                   ///**************************************** 
   
     
    }


    if (menuState <= -1 | menuState > 10){
    menuState = 0;  }

    Serial.print(" ");
            Serial.print(menuState);
          

  delay(0);
}

Pumpa Solar.
Pumpa Kotao.
EL. Grejac.