#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>


#define DS18B20PIN 16
#define COMP1 2
#define FAN1 33
#define FAN2 32
#define ARAMC1 14
#define ARAMC2 35
#define COMP2 4
#define PUMP 17
#define GAS 27
#define WAY1 25
#define WAY2 26
#define GOMB4 19
#define GOMB3 23
#define GOMB2 18
#define GOMB1 13

/*
ROM = 28 50 82 35 23 20  1 5E - 1. ELORE MENO
ROM = 28 c8 E7 7  D6  1 3C 3C -2. CSOHO1
ROM = 28 91 84 48 F6 AD 3C 96 - 3.
ROM = 28 BD 5 48 F6 21 3C 39  - 4.
ROM = 28 77 B3 48 F6 D2 3C 76  - 5.
ROM = 28 CF F 15 0 0 0 73      -6.
ROM = 28 E D3 48 F6 BC 3C EF   -7.
*/
DeviceAddress sensor1 = { 0x28, 0x50, 0x82, 0x35, 0x23, 0x20, 0x1, 0x5E };//eloremeno
DeviceAddress sensor2 = { 0x28, 0xC8, 0xE7, 0x7, 0xD6, 0x1, 0x3C, 0x3C }; // csoho 1
DeviceAddress sensor3= { 0x28, 0x91, 0x84, 0x48, 0xF6, 0xAD, 0x3C, 0x96 };//senzor 3 csoho 1
DeviceAddress sensor4= { 0x28, 0xBD, 0x5, 0x48, 0xF6, 0x21, 0x3C, 0x39 };//senzor 4 ROM = 28 BD 5 48 F6 21 3C 39  - 4. kulso

//DeviceAddress sensor6= { 0x28, 0xCF, 0xF, 0x15, 0x0, 0x0, 0x0, 0x73 }; //senzor 6
//DeviceAddress sensor7= { 0x28, 0xE, 0xD3, 0x48, 0xF6, 0xBC, 0x3C, 0xEF };//7-es


float csohomerseklet1=0,csohomerseklet2=0;
                        
float eloremeno=0,kinthomerseklet=0,szamolgato1=0,szamolgato2=0;

long gombidostart,homerskeletido,aramido,elindultido1,elindultido2,comp1startido,comp2startido,defroststartido1,defroststartido2,hp1stopido,hp2stopido,wait,ventstopido1=-10000,ventstopido2=-10000,teljesitmenyido=20;
int gomb,gomb2,gomb3,gomb4,temp,k=0,b=0;
int menu,jel,set,start,mod=1,hutesbe=0,hibah=0,kevesgazhiba1=0,kevesgazhiba2=0;
int veletlen=0,mukodj1=0, mukodj2=0, elindult1=0, elindult2=0,ventilator1=0, ventilator2=0,way1=0,way2=0,def1=0,def2=0,nagyonhiba1=0,nagyonhiba2=0,def1drb=0,def2drb=0,gasbe=0,pumpbe=0,gas=1;
int readValue1,readValue2,maxValue1 = 0,minValue1 = 4096,maxValue2 = 0,minValue2 = 4096; 
float result1=0,result2=0,aramC1=0,aramC2=0,tularam=8,comparam1=7.5,comparam2=7.5,fagyaram1=4,fagyaram2=4;          
byte addr[8];
long defc1=0,defc2=0,defc11=0,defc22=0;
int deftime=20,deftimesoft=20,stopido=10, hibaido=20, stabido=30, ventpihen=10,softdefrost=1,koef=5, delateljesitmeny=1;
long ido=0;
float settemp=25,hisztiplusz=0, hisztiminusz=0,minimumstart=0,softdefrosttemp=1; 


OneWire oneWire(DS18B20PIN);
DallasTemperature sensor(&oneWire);
LiquidCrystal_I2C lcd(0x27, 20, 4);

byte degree_symbol[8] = 
              {
                0b00111,
                0b00101,
                0b00111,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000
              };
void setup()
{
  EEPROM.begin(22);
  
   pinMode(COMP1, OUTPUT);
   pinMode(COMP2, OUTPUT);
   pinMode(FAN1, OUTPUT);
   pinMode(FAN2, OUTPUT);
   pinMode(PUMP, OUTPUT);
   pinMode(GAS, OUTPUT);
   pinMode(WAY1, OUTPUT);
   pinMode(WAY2, OUTPUT);
   pinMode(GOMB1, INPUT);
   pinMode(GOMB2, INPUT);
   pinMode(GOMB3, INPUT);
   pinMode(GOMB4, INPUT);
  
  gombidostart= millis();
  aramido = millis();
  ido = millis();
  
  Serial.begin(115200);
  sensor.begin();
  lcd.init();
  lcd.backlight();
  lcd.createChar(0, degree_symbol);
  lcd.clear();
  epromolvas();
  
  comparam1 = comparam1/20;
  comparam2 = comparam2/20;
  fagyaram1 = fagyaram1/20;
  fagyaram2 = fagyaram2/20;
  settemp=settemp/2;
  hisztiplusz=hisztiplusz/20;
  hisztiminusz=hisztiminusz/20;
  softdefrosttemp=softdefrosttemp/4;
  minimumstart=(minimumstart/2)-20;
  }

void epromiras()
  {
  int settempep=settemp*2;
  int comparam11= comparam1*20;
  int comparam22= comparam2*20;
  int fagyaram11= fagyaram1*20;
  int fagyaram22= fagyaram2*20;
  int minimumstart1=(minimumstart+20)*2;
  int hisztiplusz1=hisztiplusz*20;
  int hisztiminusz1=hisztiminusz*20;
  int softdefrosttemp1=softdefrosttemp*4;
  
  EEPROM.write(0, settempep);//
  EEPROM.write(1, mod);//
  EEPROM.write(2, comparam11);//
  EEPROM.write(3, comparam22);//
  EEPROM.write(4, fagyaram11);//
  EEPROM.write(5, fagyaram22);//  
  EEPROM.write(6, deftime/10);// 
  EEPROM.write(7, stopido/10);// 
  EEPROM.write(8, hibaido/10);// 
  EEPROM.write(9, stabido/10);// 
  EEPROM.write(10, ventpihen/10);
  EEPROM.write(11, hisztiplusz1);
  EEPROM.write(12, hisztiminusz1);
  EEPROM.write(13, minimumstart1);
  EEPROM.write(14, softdefrost);
  EEPROM.write(16, koef);
  EEPROM.write(17, softdefrosttemp1);
  EEPROM.write(18, teljesitmenyido/10);
  EEPROM.write(19, delateljesitmeny);
  EEPROM.write(20, deftimesoft/10); 
  
  EEPROM.commit(); 
  }

 void epromolvas()
  {

  settemp = EEPROM.read(0);//
  mod = EEPROM.read(1);//
  comparam1 = EEPROM.read(2);//
  comparam2 = EEPROM.read(3);//
  fagyaram1 = EEPROM.read(4);//
  fagyaram2 = EEPROM.read(5);//  
  deftime = EEPROM.read(6)*10;// 
  stopido = EEPROM.read(7)*10;// 
  hibaido = EEPROM.read(8)*10;// 
  stabido = EEPROM.read(9)*10;// 
  ventpihen = EEPROM.read(10)*10;
  hisztiplusz = EEPROM.read(11);
  hisztiminusz = EEPROM.read(12);
  minimumstart = EEPROM.read(13);
  softdefrost = EEPROM.read(14);
  koef = EEPROM.read(16);
  softdefrosttemp = EEPROM.read(17);
  teljesitmenyido = EEPROM.read(18)*10;
  delateljesitmeny = EEPROM.read(19);
  deftimesoft = EEPROM.read(20)*10;
  
  wait=-hibaido*10000;
  } 

void gasstart()  
  {
  digitalWrite(GAS,HIGH);
  gasbe=1; 
  }//void vege bekapcsolas GAS

void pumpstart()  
  {
  digitalWrite(PUMP,HIGH);
  pumpbe=1; 
  }//void vege bekapcsolas PUMP

void gasstop()  
  {
  digitalWrite(GAS,LOW);
  gasbe=0; 
  }//void vege bekapcsolas VENT1

void pumpstop()  
  {
  digitalWrite(PUMP,LOW);
  pumpbe=0; 
  }//void vege bekapcsolas VENT1     

void vent1start()  
  {
  digitalWrite(FAN1,HIGH);
  ventilator1=1; 
  }//void vege bekapcsolas VENT1

void vent2start()  
  {
  digitalWrite(FAN2,HIGH); 
  ventilator2=1; 
  }//void vege bekapcsolas VENT2

void vent1stop()  
  {
  digitalWrite(FAN1,LOW);
  ventilator1=0;
  ventstopido1=ido; 
  }//void vege bekapcsolas VENT1
  
void vent2stop()  
  {
  digitalWrite(FAN2,LOW);
  ventilator2=0; 
  ventstopido2=ido; 
  }//void vege bekapcsolas VENT2
  
void comp1start()  
  {
  digitalWrite(COMP1,HIGH); 
  comp1startido=ido;
  }//void vege bekapcsolas COMP1

void comp2start()  
  {
  digitalWrite(COMP2,HIGH); 
  comp2startido=ido;
  }//void vege bekapcsolas COMP2
void comp1stop()  
  {
  digitalWrite(COMP1,LOW); 
  comp1startido=0;
  }//void vege bekapcsolas COMP1

void comp2stop()  
  {
  digitalWrite(COMP2,LOW); 
  comp2startido=0;
  }//void vege bekapcsolas COMP1


void way1start()  
  {
  digitalWrite(WAY1,HIGH);
  way1=1; 
  }//void vege bekapcsolas VENT1
void way2start()  
  {
  digitalWrite(WAY2,HIGH); 
   way2=1; 
  }//void vege bekapcsolas VENT2

void way1stop()  
  {
  digitalWrite(WAY1,LOW);
  way1=0; 
  }//void vege bekapcsolas VENT1
void way2stop()  
  {
  digitalWrite(WAY2,LOW);
   way2=0; 
  }//void vege bekapcsolas VENT2
  
 
void hpstart1()  
  {
  way1start();
  vent1start();
  comp1start();
  elindult1=1;
  elindultido1=ido;
  mukodj1=1;
  }//void vege bekapcsolas HP1 
  
void hpstart2()  
  { 
  way2start();
  vent2start();
  comp2start();  
  elindult2=1; 
  elindultido2=ido; 
  mukodj2=1;
  }//void vege bekapcsolas HP2 
  
void hpstop1()  
  {
  vent1stop();
  comp1stop();  
  elindult1=0;
  elindultido1=0;
  hp1stopido=ido;
  wait=ido;
  }//void vege kikapcsolas HP1
  
void hpstop2()  
  {
  vent2stop();
  comp2stop();  
  elindult2=0;
  elindultido2=0;
  hp2stopido=ido;
  wait=ido;
  }//void vege kikapcsolas HP2 

void stopi()
  {
  hpstop1();
  hpstop2();
  mukodj1=0;
  mukodj2=0;
  def1=0;
  def2=0;

  nagyonhiba1=0;
  nagyonhiba2=0;
  kevesgazhiba1=0;
  kevesgazhiba2=0;
  hutesbe=0;
  gasstop();
  }

void homerseklet()
  {
  float memoria=eloremeno;
  float memoria2=kinthomerseklet;
  sensor.requestTemperatures(); 
  eloremeno=sensor.getTempC(sensor1);
  csohomerseklet1=sensor.getTempC(sensor2);
  csohomerseklet2=sensor.getTempC(sensor3);
  kinthomerseklet=sensor.getTempC(sensor4);
  k++;
  b++;
  if(eloremeno==-127.00 and k<5) eloremeno=memoria;  else k=0;
  if(kinthomerseklet==-127.00 and b<5)kinthomerseklet=memoria;else b=0;
  
  aramido=ido;
  }

void arammero()
  {
  readValue1 = analogRead(ARAMC1);// see if you have a new maxValue
  readValue2 = analogRead(ARAMC2);// see if you have a new maxValue
 
  if((ido-aramido) < 1000)
    {
    if (readValue1 > maxValue1) 
       {
       maxValue1 = readValue1;/*record the maximum sensor value*/
       }
  if (readValue1 < minValue1) 
       {
       minValue1 = readValue1;
       }
    } 
  if((ido-aramido) < 1000)
    {
    if (readValue2 > maxValue2) 
       {
       maxValue2 = readValue2;/*record the maximum sensor value*/
       }
  if (readValue2 < minValue2) 
       {
       minValue2 = readValue2;
       }

    }//if vege


  if((ido-aramido) > 1000)
    {
     //Serial.println(); Serial.print("millis()-aramido=");Serial.print(millis()-aramido);Serial.print(">1000"); 
    result1 = ((maxValue1 - minValue1) * 3.235)/4096.0;
    result2 = ((maxValue2 - minValue2) * 3.235)/4096.0;
 
    
    aramido=ido;
    maxValue1 = 0;             // store max value here
    minValue1 = 4096;
    maxValue2 = 0;             // store max value here
    minValue2 = 4096;    
      
    aramC1 = ((((result1/2.0) *0.707) * 1000)/66);
    aramC2 = ((((result2/2.0) *0.707) * 1000)/66);
    //if(aramC1>3)aramC1=aramC1; else aramC1=aramC1-0.13;
    //if(aramC2>3)aramC2=aramC2+0.4; else aramC2=aramC2-0.13;
    aramC1=aramC1-0.13;
    aramC2=aramC2-0.13;
    if(aramC1<=0)aramC1=0;
    if(aramC2<=0)aramC2=0;
     
    }//if vege aram meres szamolasa
    
     //Serial.println(); Serial.print("++++aramC1="); 
     
    // Serial.print(aramC1);
    // Serial.println("aramC2="); Serial.print(aramC2);
  }//aram mero fugveny vege


void futes()
  {  
  Serial.println(); Serial.print("mukodj1=0="); Serial.print(mukodj1); Serial.print("mukodj2=0="); Serial.print(mukodj2);  Serial.print("  def1=0"); Serial.print(def1); Serial.print("  def2=0"); Serial.print(def2); Serial.print("millis()-wait="); Serial.print(ido-wait); Serial.print("millis()-wait="); Serial.print(ido-wait); Serial.print(">>>hibaido*1000"); Serial.print(hibaido*1000);     
  if(mukodj1==0 and mukodj2==0 and def1==0 and def2==0 and (ido-wait>hibaido*1000)) veletlen=random(1,5); 
  if(veletlen==1 and nagyonhiba1==0)mukodj1=1;
  if(veletlen==2 and nagyonhiba2==0)mukodj2=1;
  veletlen=0;

 // Serial.println(""); Serial.print("def1=");Serial.print(def1);Serial.print("  mukodj1=");Serial.print(mukodj1);Serial.print("  def2=");Serial.print(def2); Serial.print("  mukodj2=");Serial.print(mukodj2);
  if(mukodj1==1 and elindult1==0 and def1==0 and nagyonhiba1==0)hpstart1();  
  if(mukodj2==1 and elindult2==0 and def2==0 and nagyonhiba2==0)hpstart2();  

  if(aramC1>comparam1 and ventilator1==1 and elindult1==1  and ido-comp1startido>5000)vent1stop();
  if(aramC1<comparam1 and ventilator1==0 and elindult1==1 and (ido-ventstopido1>ventpihen*1000))vent1start();
  
  if(aramC2>comparam2 and ventilator2==1 and elindult2==1  and ido-comp2startido>5000)vent2stop();
  if(aramC2<comparam2 and ventilator2==0 and elindult2==1 and (ido-ventstopido2>ventpihen*1000))vent2start();

  
  if(aramC1<fagyaram1 and elindult1==1 and softdefrost==1 and kinthomerseklet>softdefrosttemp and (ido-comp1startido>stabido*1000) and def1==0)
    {
    defc11=defc11+1; Serial.println(""); Serial.print("defc11=");Serial.print(defc11);Serial.print("  DEFROST GYANU1");
    if(defc11>1000)
      { 
      comp1stop();
      def1=1;
      if(elindult2==0 and def2==0  and nagyonhiba2==0)hpstart2();
       defroststartido1=ido;  
      //def1drb++;
      }
    }else defc11=0;
  
  if(aramC2<fagyaram2 and elindult2==1 and softdefrost==1 and kinthomerseklet>softdefrosttemp and (ido-comp2startido>stabido*1000) and def2==0)
    {
    defc22=defc22+1; Serial.println(""); Serial.print("defc22=");Serial.print(defc22);Serial.print("  DEFROST GYANU2");
    if(defc22>1000)
      { 
      comp2stop();
      def2=1;
      if(elindult1==0 and def1==0  and nagyonhiba1==0)hpstart1();
      defroststartido2=ido;  
      def2drb++;
      }
    }else defc22=0;
    
  if(aramC1<fagyaram1 and elindult1==1 and (softdefrost==0 or kinthomerseklet<softdefrosttemp)  and (ido-comp1startido>stabido*1000) and def1==0)
    {
    defc1=defc1+1; Serial.println(""); Serial.print("defc1=");Serial.print(defc1);Serial.print("  DEFROST GYANU nagy1");
    if(defc1>1000)
      {
      hpstop1();
      def1=1;   
      if(elindult2==0 and def2==0  and nagyonhiba2==0)hpstart2();
      defroststartido1=ido;  
      //def1drb++;
      }
      }else defc1=0;
  
  if(aramC2<fagyaram2 and elindult2==1 and (softdefrost==0 or kinthomerseklet<softdefrosttemp) and (ido-comp2startido>stabido*1000) and def2==0)
    {
    defc2=defc2+1;
    if(defc2>1000)
      { 
      hpstop2();
      def2=1;
      if(elindult1==0 and def1==0  and nagyonhiba1==0)hpstart1();
      defroststartido2=ido;  
      def2drb++;
      }
    }else defc2=0;
  
  
    if(def1==1 and way1==0 and comp1startido==0  and nagyonhiba1==0)
      {
      comp1start();  
      defroststartido1=ido;  
      }
    if(def2==1 and way2==0 and comp2startido==0  and nagyonhiba2==0)
      {
      comp2start();  
      defroststartido2=ido;  
      }  

  if(def1==1 and (softdefrost==0 or kinthomerseklet<softdefrosttemp) and (ido-defroststartido1)>deftime*1000) 
    {
    def1=0;
    hpstop1();
    mukodj1=0;
    Serial.println(""); Serial.print("MEGTORTENT 1-*****************************************************************");
    }
    
  if(def2==1 and (softdefrost==0 or kinthomerseklet<softdefrosttemp) and (ido-defroststartido2)>deftime*1000) 
    {
    def2=0;
    hpstop2();
    mukodj2=0;
    Serial.println(""); Serial.print("22222222222222222  MEGTORTENT 2-*****************************************************************");
    }   
    
  if(def1==1 and (softdefrost==1 or kinthomerseklet>softdefrosttemp) and  (ido-defroststartido1)>deftimesoft*1000) 
    {
    def1=0;
    hpstop1();
    mukodj1=0;
     Serial.println(""); Serial.print("333333333333333333333333333  MEGTORTENT 3333333333333333333333333-*****************************************************************");
    }
    
  if(def2==1 and (softdefrost==1 or kinthomerseklet>softdefrosttemp) and (ido-defroststartido2)>deftimesoft*1000) 
    {
    def2=0;
    hpstop2();
    mukodj2=0;
         Serial.println(""); Serial.print("444444444444444444444444444444  MEGTORTENT 444444444444444444444444444444444-*****************************************************************");

    }

 // Serial.println(""); Serial.print(" ---- settemp-eloremeno=");Serial.print(settemp-eloremeno);Serial.print(" >>>> delateljesitmeny   ");Serial.print(delateljesitmeny); Serial.print("------koef>");Serial.print(koef);

  if(settemp-eloremeno>koef and nagyonhiba2==0 and elindult1==1 and elindult2==0 and (ido-elindultido1)>teljesitmenyido*1000 and def1==0 and def2==0 and (ido-wait>hibaido*1000))
    {
      hpstart2();    
      
    }
   if(settemp-eloremeno>koef and nagyonhiba1==0 and elindult2==1 and elindult1==0 and (ido-elindultido2)>teljesitmenyido*1000 and def1==0 and def2==0 and (ido-wait>hibaido*1000))
    {
    hpstart1();   
    }

    if(settemp-eloremeno<delateljesitmeny and elindult1==1 and elindult2==1 and def1==0 and def2==0)
      {
      int veletlenke2=random(1,2);
 
      if(veletlenke2==1)
        {
        hpstop1();
        mukodj1=0;
        }
        else 
          {
          hpstop2();
          mukodj2=0;
          }
      veletlenke2=0;
      }


   if(aramC1>comparam1+0.5 and (ido-comp1startido)>5000)
    {
    if(def1==0)nagyonhiba1=1;  
    mukodj1=0;  
    hpstop1(); 
    def1=0;
   aramC1=0;
    }  

  if(aramC2>comparam2+0.5 and (ido-comp2startido)>5000)
    {
    if(def2==0)nagyonhiba2=1;  
    mukodj2=0;
    hpstop2();
    def2=0;
    aramC2=0;
    }
     
  }//futes void vege

  
void way()
  {
  if(elindult1==0 and elindultido1==0 and way1==1 and ido-hp1stopido>stopido*1000) way1stop();
  if(elindult2==0 and elindultido2==0 and way2==1 and ido-hp2stopido>stopido*1000) way2stop();
  if(elindult2==0 and elindultido2==0 and pumpbe==1 and ido-hp2stopido>stopido*1000)pumpstop();
  }

void kevesgaz()
  {
  if(aramC1<(fagyaram1-1) and (ido-comp1startido>stabido*500) and elindult1==1 and def1==0)
    {
    kevesgazhiba1=1;
    nagyonhiba1=1;
    hpstop1();  
    mukodj1=0;
    }
  if(aramC2<(fagyaram2-1)and (ido-comp2startido>stabido*500) and elindult2==1 and def2==0)
    {
    kevesgazhiba2=1;
    nagyonhiba2=1;
    hpstop2();  
    mukodj2=0;
    }    
  }//kevesgaz
  
void hutes()
  {
   if(nagyonhiba1==0 and hutesbe==0)
    {
    comp1start();
    vent1start();
    hutesbe=1;
    }
  if(aramC1>comparam1+0.5)
    { 
    hpstop1(); 
    nagyonhiba1=1;
    }    
  }
  

void loop()
  {
  ido = millis();
    
  if(ido-homerskeletido>4000) 
  {
  homerskeletido=ido;
  homerseklet();
  menuke();
  }else
  arammero();
 
  way();
  gomb=digitalRead(GOMB1);
  gomb2=digitalRead(GOMB2);
  gomb3=digitalRead(GOMB3);
  gomb4=digitalRead(GOMB4); 
    
    if(start==1)
      {
      if(kinthomerseklet>minimumstart)
        {
        hibah=0;
        if(settemp>eloremeno and mod==1)
          {
          if(gasbe==1)gasstop(); 
          if(pumpbe==0)pumpstart(); 
          futes();
          }
        
        if(settemp+hisztiminusz<eloremeno and mod==0)
          {
          hutes();      
          if(pumpbe==0)pumpstart();   
          }
        }
      if(kinthomerseklet>minimumstart and gas==0)hibah=1;
          
      if(kinthomerseklet<minimumstart and mod==1 and gasbe==0)
        {
        stopi();
        if(gas==1)gasstart();   
        }
      }// if vege futes inditasa
      
    if(settemp<eloremeno and mod==1)
          {
          stopi();      
          }//hoszivatyu kikapcsolasa futes
    
    if(settemp-hisztiplusz>eloremeno and mod==0)
          {
          stopi();      
          }//hoszivatyu kikapcsolasa hutes

      if(start==0 and mod==1 and (elindult1==1 or elindult2==1 or nagyonhiba1==1 or nagyonhiba2==1)) stopi();

      
      if(start==0 and hutesbe==1) stopi();
      if(start==0 and gasbe==1)stopi();
  
//kevesgaz();
  if((gomb==HIGH or gomb2==HIGH or gomb3==HIGH or gomb4==HIGH) and (ido - gombidostart >= 500))
    {
    if(gomb==HIGH)
      {
      if(set==1)settemp=settemp+0.5;
      if(jel==0 and set==0)menu++;
     
      if(jel==1 and set==0 and menu==3) hisztiplusz=hisztiplusz+0.25;
      if(jel==2 and set==0 and menu==3) hisztiminusz=hisztiminusz+0.25;
      if(jel==3 and set==0 and def1==0 and def2==0 and menu==3) softdefrost=1;
      if(jel==4 and set==0 and menu==3 and start==0) mod=1;

      if(jel==1 and set==0 and menu==4 and start==0) deftime=deftime+10;
      if(jel==2 and set==0 and menu==4 and start==0) stabido=stabido+10;
      if(jel==3 and set==0 and menu==4 and start==0) hibaido=hibaido+10;
      if(jel==4 and set==0 and menu==4 and start==0) stopido=stopido+10;

      if(jel==1 and set==0 and menu==5 and start==0) comparam1=comparam1+0.25;
      if(jel==2 and set==0 and menu==5 and start==0) comparam2=comparam2+0.25;
      if(jel==3 and set==0 and menu==5 and start==0) fagyaram1=fagyaram1+0.25;
      if(jel==4 and set==0 and menu==5 and start==0) fagyaram2=fagyaram2+0.25;
      

      if(jel==1 and set==0 and menu==6) koef=koef+1;
      if(jel==2 and set==0 and menu==6 and start==0) ventpihen=ventpihen+10;
      if(jel==3 and set==0 and menu==6 and start==0) gas=1;
      if(jel==4 and set==0 and menu==6 and start==0) minimumstart=minimumstart+0.5;
      
       if(jel==2 and set==0 and menu==7) softdefrosttemp=softdefrosttemp+0.25;
       if(jel==1 and set==0 and menu==7) teljesitmenyido=teljesitmenyido+10;
       if(jel==3 and set==0 and menu==7) delateljesitmeny=delateljesitmeny+1;
          if(jel==4 and set==0 and menu==7) deftimesoft=deftimesoft+10;
       
      }
      
  if(gomb2==HIGH)
    {
    if(set==1)settemp=settemp-0.5;
    if(jel==0  and set==0)menu--;
    
    if(jel==1 and set==0 and hisztiplusz>0 and menu==3) hisztiplusz=hisztiplusz-0.25;
    if(jel==2 and set==0 and hisztiminusz>0 and menu==3) hisztiminusz=hisztiminusz-0.25;
    if(jel==3 and set==0 and def1==0 and def2==0 and menu==3) softdefrost=0;
    if(jel==4 and set==0 and menu==3  and start==0) mod=0;

      if(jel==1 and set==0 and menu==4 and start==0 and deftime>10) deftime=deftime-10;
      if(jel==2 and set==0 and menu==4 and start==0 and stabido>10) stabido=stabido-10;
      if(jel==3 and set==0 and menu==4 and start==0 and hibaido>10) hibaido=hibaido-10;
      if(jel==4 and set==0 and menu==4 and start==0 and stopido>10) stopido=stopido-10;

      if(jel==1 and set==0 and menu==5 and start==0 and comparam1>4) comparam1=comparam1-0.25;
      if(jel==2 and set==0 and menu==5 and start==0 and comparam2>4) comparam2=comparam2-0.25;
      if(jel==3 and set==0 and menu==5 and start==0 and fagyaram1>1) fagyaram1=fagyaram1-0.25;
      if(jel==4 and set==0 and menu==5 and start==0 and fagyaram2>1) fagyaram2=fagyaram2-0.25;
      
      if(jel==1 and set==0 and menu==6 and koef>0) koef=koef-1;
      if(jel==2 and set==0 and menu==6 and start==0 and ventpihen>10) ventpihen=ventpihen-10;
      if(jel==3 and set==0 and menu==6 and start==0) gas=0;
      if(jel==4 and set==0 and menu==6 and start==0) minimumstart=minimumstart-0.5;

      if(jel==2 and set==0 and menu==7) softdefrosttemp=softdefrosttemp-0.25;
       if(jel==1 and set==0 and menu==7) teljesitmenyido=teljesitmenyido-10;
       if(jel==3 and set==0 and menu==7 and delateljesitmeny>0) delateljesitmeny=delateljesitmeny-1;
        if(jel==4 and set==0 and menu==7) deftimesoft=deftimesoft-10;
    }
    
  if(gomb3==HIGH)
    {
    if(set==0 and menu==0)
      set=1;else set=0;
   
    if(set==0 and menu==0)
      { 
      epromiras();
      }
    
    if(menu>=3)jel++;
    if(jel>4)
      {      
      jel=0;
      epromiras();
      }
    }
  if(gomb4==HIGH)
    {
    if(start==0)
       {
       start=1;
       }
    else start=0;
    //Serial.println("START=");Serial.print(start);
    }        

  gombidostart= ido;
  if(menu>=9)menu=0; 
  if(menu<0)menu=8; 
  lcd.clear();
  menuke();
  }//gomb nyomas if vege

//if vege homerseklet ki olvasas
  
}//void loop vege
void menuke()
  {
  if(menu==0)alap();
  if(menu==1)menu1();
  if(menu==2)menu2();
  if(menu==3)menu3();
  if(menu==4)menu4();
  if(menu==5)menu5();
  if(menu==6)menu6();
  if(menu==7)menu7();
  if(menu==8)menu8();
  }

void menu1()
  {
    float stack2=0;
  lcd.setCursor(0,0);
  lcd.print("Csoho1:");
  lcd.print(csohomerseklet1);
  lcd.write(0);
  lcd.print("C");

  lcd.setCursor(0,1);
  lcd.print("Aram COMP1: ");
  stack2=aramC1*220;
  lcd.print(aramC1);
  lcd.print("A");
  lcd.setCursor(0,2);
  lcd.print("P-HP1[W]:");
  lcd.print(stack2,0);
  
  lcd.setCursor(0,3);
  lcd.print("State:");
  lcd.setCursor(8,3);
  lcd.print("          ");
  lcd.setCursor(8,3);
  if(elindult1==0 and def1==0 and nagyonhiba1==0 and comp1startido==0)lcd.print("Stand by");
  if(elindult1==1 and def1==0)lcd.print("Futes");
   if(nagyonhiba1==1 and kevesgazhiba1==0)lcd.print("TularamHIBA");
  if(nagyonhiba1==0 and comp1startido>10 and mod==0 and start==1)lcd.print("Hutes");
       if(nagyonhiba1==1 and kevesgazhiba1==1)lcd.print("Low freon");
  if(def1==1)
      {
    lcd.print("Defrost-");
    //lcd.print((deftime*1000-(millis()-defroststartido1))/1000);
     if(softdefrost==0)  lcd.print((deftime*1000-(ido-defroststartido1))/1000);else lcd.print((deftimesoft*1000-(ido-defroststartido1))/1000);
    lcd.print("s");
    }
  }
  
void menu2()
  {
    float stack=0;
  lcd.setCursor(0,0);
  lcd.print("Csoho2:");
  lcd.print(csohomerseklet2);
  lcd.write(0);
  lcd.print("C");

  lcd.setCursor(0,1);
  lcd.print("Aram COMP2: ");
  stack=aramC2*220;
  lcd.print(aramC2);
  lcd.print("A");
  lcd.setCursor(0,2);
  lcd.print("P-HP2[W]:");
    lcd.setCursor(9,2);
    lcd.print("      ");
        lcd.setCursor(9,2);
  lcd.print(stack,0);
  
  lcd.setCursor(0,3);
  lcd.print("State:");
  lcd.setCursor(8,3);
  lcd.print("            ");
  lcd.setCursor(8,3);
  if(elindult2==0 and def2==0 and nagyonhiba2==0)lcd.print("Stand by");
  if(elindult2==1 and def2==0)lcd.print("Mukodik");
     if(nagyonhiba2==1 and kevesgazhiba2==0)lcd.print("TularamHIBA");
     if(nagyonhiba2==1 and kevesgazhiba2==1)lcd.print("Low freon");
  if(def2==1)
    {
    lcd.print("Defrost-");

  if(softdefrost==0)  lcd.print((deftime*1000-(ido-defroststartido2))/1000);else lcd.print((deftimesoft*1000-(ido-defroststartido2))/1000);
        lcd.print("s");
    }
  
  }

void menu3()
  {
  lcd.setCursor(0,0);
  if(jel==1)lcd.print(">");
  lcd.print("Hiszterezis+: ");
  lcd.print(hisztiplusz);

  lcd.setCursor(0,1);
  if(jel==2)lcd.print(">");
  lcd.print("Hiszterezis-: ");
  lcd.print(hisztiminusz);


    lcd.setCursor(0,2);
    if(jel==3)lcd.print(">");  
  lcd.print("Defrost: ");
   if(softdefrost==1)lcd.print("BE");else lcd.print("KI");
   
   lcd.setCursor(0,3);
   if(jel==4)lcd.print(">");  
   lcd.print("Mod: ");
   if(mod==1)lcd.print("Futes");else lcd.print("Hutes");
   

  
  
  } 
void menu4()
  {
  lcd.setCursor(0,0);
  if(jel==1)lcd.print(">");
  lcd.print("DefrostIdo[s]: ");
  lcd.print(deftime);

  lcd.setCursor(0,1);
  if(jel==2)lcd.print(">");
  lcd.print("Stabi ido[s]: ");
  lcd.print(stabido);


    lcd.setCursor(0,2);
    if(jel==3)lcd.print(">");  
  lcd.print("Hibaido[s]: ");
  lcd.print(hibaido);
  
   lcd.setCursor(0,3);
   if(jel==4)lcd.print(">");
   lcd.print("Stop ido[s]: ");
     lcd.print(stopido);
  } 

void menu5()
  {
  lcd.setCursor(0,0);
  if(jel==1)lcd.print(">");
  lcd.print("Komp MAX Aram1: ");
  lcd.print(comparam1);

  lcd.setCursor(0,1);
  if(jel==2)lcd.print(">");
  lcd.print("Komp MAX Aram2: ");
  lcd.print(comparam2);


    lcd.setCursor(0,2);
    if(jel==3)lcd.print(">");  
  lcd.print("Fagyas Aram1:");
  lcd.print(fagyaram1);
  
   lcd.setCursor(0,3);
   if(jel==4)lcd.print(">");
   lcd.print("Fagyas Aram2:");
   lcd.print(fagyaram2);
  } 

void menu6()
  {
  lcd.setCursor(0,0);
  if(jel==1)lcd.print(">");
   lcd.print("P-homerseklet: ");
   lcd.print(koef);

  lcd.setCursor(0,1);
  if(jel==2)lcd.print(">");
  lcd.print("Vent Stop Ido: ");
  lcd.print(ventpihen);
  
    lcd.setCursor(0,2);
    if(jel==3)lcd.print(">");
      lcd.print("Gaz: ");
   if(gas==1)lcd.print("BE");else lcd.print("KI");  
  
   lcd.setCursor(0,3);
   if(jel==4)lcd.print(">");
   lcd.print("MinStartTemp:");
   lcd.print(minimumstart);
  }

void menu7()
  {
    lcd.setCursor(0,0);
  if(jel==1)lcd.print(">");
   lcd.print("P-tenyIDo: ");
   lcd.print(teljesitmenyido);

  lcd.setCursor(0,1);
  if(jel==2)lcd.print(">");
  lcd.print("SoftDefTemp");
  lcd.print(softdefrosttemp);
  
    lcd.setCursor(0,2);
    if(jel==3)lcd.print(">");
      lcd.print("DeltaP: ");
 lcd.print(delateljesitmeny);
  
   lcd.setCursor(0,3);
   if(jel==4)lcd.print(">");
   lcd.print("SoftDefIdo:");
   lcd.print(deftimesoft);
  }
     
  
void menu8()
  {
  lcd.setCursor(0,0);
  lcd.print("KOMP1: ");if(comp1startido>10)lcd.print("BE");else lcd.print("KI") ;
  lcd.print("  KOMP2: ");if(comp2startido>10)lcd.print("BE");else lcd.print("KI"); 
  
  lcd.setCursor(0,1);
  lcd.print("VENT1: ");if(ventilator1==1)lcd.print("BE");else lcd.print("KI") ;
  lcd.print("  VENT2: ");if(ventilator2==1)lcd.print("BE");else lcd.print("KI"); 

  lcd.setCursor(0,2);
  lcd.print("4WAY1: ");if(way1==1)lcd.print("BE");else lcd.print("KI") ;
  lcd.print("  4WAY2: ");if(way2==1)lcd.print("BE");else lcd.print("KI") ;
  
  lcd.setCursor(0,3);
  lcd.print("GAZ:  ");if(gasbe==1)lcd.print("BE");else lcd.print("KI") ;
  lcd.print("  PUMPA: ");if(pumpbe==1)lcd.print("BE");else lcd.print("KI"); 
  }
  
void alap()
  {
  lcd.setCursor(0,0);
  lcd.print("Temp:");
  lcd.print(eloremeno);
  lcd.print("(");
  lcd.print(settemp);
  lcd.print(")");
  lcd.write(0);
  lcd.print("C");

  lcd.setCursor(0,1);
  lcd.print("Kulso:");
  lcd.print(kinthomerseklet);
  lcd.write(0);
  lcd.print("C");


  lcd.setCursor(0,2);
  lcd.print("                 ");
   lcd.setCursor(0,2);
   if(elindult1==1 and elindult2==0)lcd.print("Futes: HP1");
   if(elindult2==1 and elindult1==0)lcd.print("Futes: HP2");
   if(elindult1==1 and elindult2==1)lcd.print("Futes: HP1+HP2");
   if(gasbe==1)lcd.print("Futes: GAZ");
   if(hutesbe==1)lcd.print("Hutes");
   if(hibah==1)lcd.print("Hideg/Gaz-KI");                                 
    
    lcd.setCursor(0,3);
    lcd.print("HP1:");
    lcd.setCursor(4,3);
    lcd.print("    ");
    lcd.setCursor(4,3);   
    lcd.print(aramC1*220,0);
    lcd.print("W");

    
    lcd.setCursor(9,3);
      lcd.print(" HP2:");
        lcd.setCursor(14,3);
      lcd.print("    ");
    lcd.setCursor(14,3);
       lcd.print(aramC2*220,0);
     lcd.print("W");
    
   }
