еще онин аквариумный контроллер

Нет ответов
vsg
Offline
Зарегистрирован: 09.02.2015

Всем доброго времени суток.

Не найдя контролера для себя, решил собрать то, что нужно мне. Собирал как говориться "с миру по нитке". Вроде все работает, но я понимаю, что код далеко не идеален. Если у кого есть время и желания помочь сделать код более красивым и оптимальным, то пошу помочь. С железом проблем нет, а вот в програмировании ...... не очень, но поменить уже написанное получается, ...... кажется.

[code]
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // addr, EN, RW, RS, D4, D5, D6, D7, BacklightPin, POLARITY

#define DS1307_I2C_ADDRESS 0x68
byte decToBcd(byte val) {
  return ((val / 10 * 16) + (val % 10));
}
byte bcdToDec(byte val) {
  return ((val / 16 * 10) + (val % 16));
}

long timeKey;
long keyPress;
byte Flag = 0;

byte rightButton, upButton, downButton, leftButton, enterButton, noneButton;

int Hours_Start_Sunrise[2];                    // начало рассвета по каналам час
int Minutes_Start_Sunrise[2];                  // начало рассвета по каналам минута
int Hours_End_Sunrise[2];                      // окончание рассвета по каналам час
int Minutes_End_Sunrise[2];                    // окончание рассвета по каналам минута
int Hours_Start_Sunset[2];                     // начало заката по каналам час
int Minutes_Start_Sunset[2];                   // начало заката по каналам минута
int Hours_End_Sunset[2];                       // окончание заката по каналам час
int Minutes_End_Sunset[2];                     // окончание заката по каналам минута

int MinuteDay_Start_Sunrise[2];                // день в минутах для начала рассвета
int MinuteDay_End_Sunrise[2];                  // день в минутах для окончания рассвета
int MinuteDay_Start_Sunset[2];                 // день в минутах для начала заката
int MinuteDay_End_Sunset[2];                   // день в минутах для окончания заката

int Hours_Start_CO2;                           // начало подачи СО2 час
int Minutes_Start_CO2;                         // начало подачи СО2 минута
int Hours_End_CO2;                             // окончание подачи СО2 час
int Minutes_End_CO2;                           // окончание подачи СО2 минута
int MinuteDay_Start_CO2;                       // время начала подачи CO2 в минутах
int MinuteDay_Stop_CO2;                        // время окончания подачи CO2 в минутах

int Hours_Start_FitoLamp;                      // начало работы Фито Лампы час
int Minutes_Start_FitoLamp;                    // начало работы Фито Лампы минута
int Hours_End_FitoLamp;                        // окончание работы Фито Лампы час
int Minutes_End_FitoLamp;                      // окончание работы Фито Лампы минута
int MinuteDay_Start_FitoLamp;                  // время включения Фито Лампы
int MinuteDay_Stop_FitoLamp;                   // время отключения Фито Лампы

int Current_MinuteDay;                         // текущая минута суток
int Current_Period[2];                         // текущая часть суток 1-рассвет, 2-день, 3-закат, 4-ночь для каждого ШИМ

int Meridian_PWM[2];                           // макс. значение ШИМ
int Midnight_PWM[2];                           // минимальное значение ШИМ
int Current_PWM[2] =  { 0, 0};                 // текущее значение ШИМ
int SumPWM;

int PWM_Pin[2] = { 10, 11};                    // назначение PWM выходов для hot, cold\

boolean lightOnOff = LOW;

int Fan = 9;                                   // вентилятор охлаждения радиатора, назначаем 9 выход
int Moon = 8;                                  // включение «луны», назначаем 8 выход
int CO2 = 7;                                   // реле СО2
int FitoLamp = 12;                             // Фито Лампа
int UV = 13;                                   // UV стерилизатор

// ------------------------- SETUP -------------------------
void setup ()
{
  Wire.begin();
  lcd.begin(20, 4);
  lcd.backlight();
  Serial.begin(9600);

  /*
    EEPROM.write(10, 100);   Hours_Start_Sunrise[0] =   EEPROM.read(10);
    EEPROM.write(11, 100);   Minutes_Start_Sunrise[0] = EEPROM.read(11);
    EEPROM.write(12, 100);   Hours_End_Sunrise[0] =     EEPROM.read(12);
    EEPROM.write(13, 100);   Minutes_End_Sunrise[0] =   EEPROM.read(13);
    EEPROM.write(14, 100);   Hours_Start_Sunset[0] =    EEPROM.read(14);
    EEPROM.write(15, 100);   Minutes_Start_Sunset[0] =  EEPROM.read(15);
    EEPROM.write(16, 100);   Hours_End_Sunset[0] =      EEPROM.read(16);
    EEPROM.write(17, 100);   Minutes_End_Sunset[0] =    EEPROM.read(17);

    EEPROM.write(20, 100);   Hours_Start_Sunrise[1] =   EEPROM.read(20);
    EEPROM.write(21, 100);   Minutes_Start_Sunrise[1] = EEPROM.read(21);
    EEPROM.write(22, 100);   Hours_End_Sunrise[1] =     EEPROM.read(22);
    EEPROM.write(23, 100);   Minutes_End_Sunrise[1] =   EEPROM.read(23);
    EEPROM.write(24, 100);   Hours_Start_Sunset[1] =    EEPROM.read(24);
    EEPROM.write(25, 100);   Minutes_Start_Sunset[1] =  EEPROM.read(25);
    EEPROM.write(26, 100);   Hours_End_Sunset[1] =      EEPROM.read(26);
    EEPROM.write(27, 100);   Minutes_End_Sunset[1] =    EEPROM.read(27);

                            Midnight_PWM[0] =          EEPROM.read(30);
                            Meridian_PWM[0] =          EEPROM.read(31);
                            Midnight_PWM[1] =          EEPROM.read(32);
                            Meridian_PWM[1] =          EEPROM.read(33);
  */
 /* 
  Hours_Start_Sunrise[0] =   EEPROM.read(10); if (Hours_Start_Sunrise[0] >   10) {Hours_Start_Sunrise[0] =   8; EEPROM.write(10, Hours_Start_Sunrise[0]);}
  Minutes_Start_Sunrise[0] = EEPROM.read(11); if (Minutes_Start_Sunrise[0] > 50) {Minutes_Start_Sunrise[0] = 0; EEPROM.write(11, Minutes_Start_Sunrise[0]);}
  Hours_End_Sunrise[0] =     EEPROM.read(12); if (Hours_End_Sunrise[0] >     12) {Hours_End_Sunrise[0] =     10; EEPROM.write(12, Hours_End_Sunrise[0]);}
  Minutes_End_Sunrise[0] =   EEPROM.read(13); if (Minutes_End_Sunrise[0] >   50) {Minutes_End_Sunrise[0] =   0;  EEPROM.write(13, Minutes_End_Sunrise[0]);}
  Hours_Start_Sunset[0] =    EEPROM.read(14); if (Hours_Start_Sunset[0] >    22) {Hours_Start_Sunset[0] =    19; EEPROM.write(14, Hours_Start_Sunset[0]);}
  Minutes_Start_Sunset[0] =  EEPROM.read(15); if (Minutes_Start_Sunset[0] >  50) {Minutes_Start_Sunset[0] =  0; EEPROM.write(15, Minutes_Start_Sunset[0]);}
  Hours_End_Sunset[0] =      EEPROM.read(16); if (Hours_End_Sunset[0] >      23) {Hours_End_Sunset[0] =      21; EEPROM.write(16, Hours_End_Sunset[0]);}
  Minutes_End_Sunset[0] =    EEPROM.read(17); if (Minutes_End_Sunset[0] >    50) {Minutes_End_Sunset[0] =    0;  EEPROM.write(17, Minutes_End_Sunset[0]);}

  Hours_Start_Sunrise[1] =   EEPROM.read(20); if (Hours_Start_Sunrise[1] >   10) {Hours_Start_Sunrise[1] =   8; EEPROM.write(20, Hours_Start_Sunrise[1]);}
  Minutes_Start_Sunrise[1] = EEPROM.read(21); if (Minutes_Start_Sunrise[1] > 50) {Minutes_Start_Sunrise[1] = 30; EEPROM.write(21, Minutes_Start_Sunrise[1]);}
  Hours_End_Sunrise[1] =     EEPROM.read(22); if (Hours_End_Sunrise[1] >     12) {Hours_End_Sunrise[1] =     11; EEPROM.write(22, Hours_End_Sunrise[1]);}
  Minutes_End_Sunrise[1] =   EEPROM.read(23); if (Minutes_End_Sunrise[1] >   50) {Minutes_End_Sunrise[1] =   0;  EEPROM.write(23, Minutes_End_Sunrise[1]);}
  Hours_Start_Sunset[1] =    EEPROM.read(24); if (Hours_Start_Sunset[1] >    22) {Hours_Start_Sunset[1] =    18; EEPROM.write(24, Hours_Start_Sunset[1]);}
  Minutes_Start_Sunset[1] =  EEPROM.read(25); if (Minutes_Start_Sunset[1] >  50) {Minutes_Start_Sunset[1] =  30; EEPROM.write(25, Minutes_Start_Sunset[1]);}
  Hours_End_Sunset[1] =      EEPROM.read(26); if (Hours_End_Sunset[1] >      23) {Hours_End_Sunset[1] =      20; EEPROM.write(26, Hours_End_Sunset[1]);}
  Minutes_End_Sunset[1] =    EEPROM.read(27); if (Minutes_End_Sunset[1] >    50) {Minutes_End_Sunset[1] =    30; EEPROM.write(27, Minutes_End_Sunset[1]);}

  Midnight_PWM[0] = EEPROM.read(30); if (Midnight_PWM[0] > 30) {Midnight_PWM[0] = 0; EEPROM.write(30, Midnight_PWM[0]);}
  Meridian_PWM[0] = EEPROM.read(31); if (Meridian_PWM[0] < 75) {Meridian_PWM[0] = 255; EEPROM.write(31, Meridian_PWM[0]);}
  Midnight_PWM[1] = EEPROM.read(32); if (Midnight_PWM[1] > 30) {Midnight_PWM[1] = 0; EEPROM.write(32, Midnight_PWM[1]);}
  Meridian_PWM[1] = EEPROM.read(33); if (Meridian_PWM[1] < 75) {Meridian_PWM[1] = 255; EEPROM.write(33, Meridian_PWM[1]);  }
 */

  Hours_Start_Sunrise[0] =   EEPROM.read(10);
  Minutes_Start_Sunrise[0] = EEPROM.read(11);
  Hours_End_Sunrise[0] =     EEPROM.read(12);
  Minutes_End_Sunrise[0] =   EEPROM.read(13);
  Hours_Start_Sunset[0] =    EEPROM.read(14);
  Minutes_Start_Sunset[0] =  EEPROM.read(15);
  Hours_End_Sunset[0] =      EEPROM.read(16);
  Minutes_End_Sunset[0] =    EEPROM.read(17);

  Hours_Start_Sunrise[1] =   EEPROM.read(20);
  Minutes_Start_Sunrise[1] = EEPROM.read(21);
  Hours_End_Sunrise[1] =     EEPROM.read(22);
  Minutes_End_Sunrise[1] =   EEPROM.read(23);
  Hours_Start_Sunset[1] =    EEPROM.read(24);
  Minutes_Start_Sunset[1] =  EEPROM.read(25);
  Hours_End_Sunset[1] =      EEPROM.read(26);
  Minutes_End_Sunset[1] =    EEPROM.read(27);

  Midnight_PWM[0] =          EEPROM.read(30);
  Meridian_PWM[0] =          EEPROM.read(31);
  Midnight_PWM[1] =          EEPROM.read(32);
  Meridian_PWM[1] =          EEPROM.read(33);


  /*
  EEPROM.write(50, 7);
  EEPROM.write(51, 0);
  EEPROM.write(52, 20);
  EEPROM.write(53, 0);
  */

  Hours_Start_CO2  =         EEPROM.read(50);
  Minutes_Start_CO2  =       EEPROM.read(51);
  Hours_End_CO2  =           EEPROM.read(52);
  Minutes_End_CO2 =          EEPROM.read(53);  

  /*
  EEPROM.write(60, 11);
  EEPROM.write(61, 0);
  EEPROM.write(62, 15);
  EEPROM.write(63, 0);
  */

  Hours_Start_FitoLamp  =    EEPROM.read(60);
  Minutes_Start_FitoLamp  =  EEPROM.read(61);
  Hours_End_FitoLamp  =      EEPROM.read(62);
  Minutes_End_FitoLamp =     EEPROM.read(63);  


  for (int i = 0; i <= 1; i++)  {
    pinMode(PWM_Pin[i], OUTPUT);                                                           // Установка пин для ШИМ
  }
  
  pinMode(Fan, OUTPUT);                                                                     // инициализируем пин охлаждения LED
  pinMode(Moon, OUTPUT);
  digitalWrite(Moon, HIGH);                                                                 // инициализируем пин Луны
  pinMode(CO2, OUTPUT);
  digitalWrite(CO2, HIGH);                                                                  // инициализируем пин клапана CO2
  pinMode(FitoLamp, OUTPUT);
  digitalWrite(FitoLamp, HIGH);                                                             // инициализируем пин клапана CO2
  pinMode(UV, OUTPUT);
  digitalWrite(UV, HIGH);                                                                   // инициализируем пин клапана CO2
  
  digitalWrite(A0, HIGH);
  


  for (int i = 0; i <= 1; i++)  {
    analogWrite(PWM_Pin[i], Midnight_PWM[i]);                                              // Установка минимального значения ШИМ
    Current_PWM[i] = Midnight_PWM[i];                                                      // Установка текущих значений ШИМ в минимум
  }

  for (int i = 0; i <= 1; i++)  {
    MinuteDay_Start_Sunrise[i] = 60 * Hours_Start_Sunrise[i] + Minutes_Start_Sunrise[i] ;  // Расчет минуты суток для начала рассвета
    MinuteDay_End_Sunrise[i] = 60 * Hours_End_Sunrise[i] + Minutes_End_Sunrise[i];         // Расчет минуты суток для окончания рассвета
    MinuteDay_Start_Sunset[i] = 60 * Hours_Start_Sunset[i] + Minutes_Start_Sunset[i];      // Расчет минуты суток для начала заката
    MinuteDay_End_Sunset[i] = 60 * Hours_End_Sunset[i] + Minutes_End_Sunset[i];            // Расчет минуты суток для окончания заката
  }
    MinuteDay_Start_CO2 = 60 * Hours_Start_CO2 + Minutes_Start_CO2;                        // Расчет минуты суток для начала подачи СО2
    MinuteDay_Stop_CO2 = 60 * Hours_End_CO2 + Minutes_End_CO2;                             // Расчет минуты суток для окончания подачи СО2

    MinuteDay_Start_FitoLamp = 60 * Hours_Start_FitoLamp + Minutes_Start_FitoLamp;         // Расчет минуты суток для включения Фито Лампы
    MinuteDay_Stop_FitoLamp = 60 * Hours_End_FitoLamp + Minutes_End_FitoLamp;              // Расчет минуты суток для отключения Фито Лампы
}

// ------------------------- LOOP -------------------------
void loop ()
{
  key();
  if (enterButton == 1) menu();
  if (downButton == 1) lightOnOff = HIGH;
  if (upButton == 1) lightOnOff = LOW;
  mainScreen();
  MainCode();
  fanAndMoon();
  Serial.print (rightButton); Serial.print (upButton); Serial.print (downButton); Serial.print (leftButton); Serial.print (enterButton); Serial.println (noneButton);
}


void setDateDs1307(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{  
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(second));    
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));     
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{ 
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  *second     = bcdToDec(Wire.read() & 0x7f);
  *minute     = bcdToDec(Wire.read());
  *hour       = bcdToDec(Wire.read() & 0x3f); 
  *dayOfWeek  = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month      = bcdToDec(Wire.read());
  *year       = bcdToDec(Wire.read());
}



void key() {
  int val = analogRead(0);
  if (val >= 0 && val < 50) {
    rightButton = 1;
    if (millis() - keyPress > 3000) {
      rightButton = 0;
      keyPress = millis();
    }
  }
  else {
    rightButton = 0;
  }

  if (val > 130 && val < 170) {
    upButton = 1;
    if (millis() - keyPress > 3000) {
      upButton = 0;
      keyPress = millis();
    }
  }
  else {
    upButton = 0;
  }

  if (val > 310 && val < 350) {
    downButton = 1;
    if (millis() - keyPress > 3000) {
      downButton = 0;
      keyPress = millis();
    }
  }
  else {
    downButton = 0;
  }

  if (val > 490 && val < 520) {
    leftButton = 1;
    if (millis() - keyPress > 3000) {
      leftButton = 0;
      keyPress = millis();
    }
  }
  else {
    leftButton = 0;
  }

  if (val > 720 && val < 760) {
    enterButton = 1;
    if (millis() - keyPress > 3000) {
      enterButton = 0;
      keyPress = millis();
    }
  }
  else {
    enterButton = 0;
  }

  if (rightButton != 1 && upButton != 1 && downButton != 1 && leftButton != 1 && enterButton != 1) {
    noneButton = 1;
  } else {
    noneButton = 0;
  }
}



void MainCode()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

  Current_MinuteDay = (hour*60 + minute);

  
// --------------------------------------------------------------- "CO2" -------------------------------------------------------------- 
  if (Current_MinuteDay >= MinuteDay_Start_CO2 && Current_MinuteDay <  MinuteDay_Stop_CO2) {digitalWrite (CO2, LOW);} else {digitalWrite(CO2, HIGH);} 

// --------------------------------------------------------------- "UV" --------------------------------------------------------------- 
  if (dayOfWeek == 6) {digitalWrite (UV, LOW);} else {digitalWrite(UV, HIGH);}

// --------------------------------------------------------------- "FitoLamp" ---------------------------------------------------------   
  if (Current_MinuteDay >= MinuteDay_Start_FitoLamp && Current_MinuteDay <  MinuteDay_Stop_FitoLamp) {digitalWrite (FitoLamp, LOW);} else {digitalWrite(FitoLamp, HIGH);}


// --------------------------------------------------------------- "Освещение" --------------------------------------------------------   
  for (int i=0; i<=1; i++)  {
    //    Current_Period[i]=4;
    if (Current_MinuteDay>=MinuteDay_Start_Sunrise[i] && Current_MinuteDay<=MinuteDay_End_Sunrise[i] && lightOnOff == LOW) {Current_Period[i]=1;}
      else if ((Current_MinuteDay>MinuteDay_End_Sunrise[i] && Current_MinuteDay<MinuteDay_Start_Sunset[i]) || lightOnOff == HIGH){Current_Period[i]=2;}
      else if (Current_MinuteDay>=MinuteDay_Start_Sunset[i] && Current_MinuteDay<=MinuteDay_End_Sunset[i] && lightOnOff == LOW) {Current_Period[i]=3;}
      else Current_Period[i]=4;

    // --------------------------------------------------------------- "Рассвет" ------------------------------------------------------- 
    if (Current_Period[i]==1) {Current_PWM[i]=(float)(((float)Current_MinuteDay-MinuteDay_Start_Sunrise[i])/((float)MinuteDay_End_Sunrise[i]-MinuteDay_Start_Sunrise[i]))*((float)Meridian_PWM[i]-Midnight_PWM[i]);
      if (Current_PWM[i]<Midnight_PWM[i]) {Current_PWM[i]=Midnight_PWM[i];}
      analogWrite(PWM_Pin[i], Current_PWM[i]);
    }
    // --------------------------------------------------------------- "Полдень" ------------------------------------------------------- 
    if (Current_Period[i]==2){
      if (Current_PWM[i]!=Meridian_PWM[i]) {Current_PWM[i]=Meridian_PWM[i]; analogWrite(PWM_Pin[i], Current_PWM[i]);}
    }
    // --------------------------------------------------------------- "Закат" --------------------------------------------------------- 
    if (Current_Period[i]==3) {
      Current_PWM[i]=(float)(((float)MinuteDay_End_Sunset[i]-Current_MinuteDay)/((float)MinuteDay_End_Sunset[i]-MinuteDay_Start_Sunset[i]))*((float)Meridian_PWM[i]-Midnight_PWM[i]); analogWrite(PWM_Pin[i], Current_PWM[i]);
    }
    // --------------------------------------------------------------- "Полночь" -------------------------------------------------------- 
    if (Current_Period[i]==4) {if (Current_PWM[i]!=Midnight_PWM[i]) {Current_PWM[i]=Midnight_PWM[i];
      if (Current_PWM[i]<Midnight_PWM[i]) {Current_PWM[i]=Midnight_PWM[i];}
        analogWrite(PWM_Pin[i], Current_PWM[i]);
      }
    }
  }
}

void fanAndMoon(){
  SumPWM=0;
  for (int i=0; i<=1; i++) {SumPWM=SumPWM+Current_PWM[i];}
  
  analogWrite (Fan, map(SumPWM, 0, 510, 0, 255));

  if (SumPWM <= 10) {digitalWrite(Moon, LOW);}
  else {digitalWrite(Moon, HIGH);}
}

void mainScreen() {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  int ch1 = map(Current_PWM[0], 0, 255, 0, 100);
  int ch2 = map(Current_PWM[1], 0, 255, 0, 100);
  int fan = map(SumPWM, 0, 510, 0, 100);

  // -------------------------------------- Вывод времени --------------------------------------
  lcd.setCursor(0, 0);
  if (hour < 10) lcd.print(" ");
  lcd.print(hour);
  lcd.print(":");
  if (minute < 10) lcd.print("0");
  lcd.print(minute);
  lcd.print(":");
  if (second < 10) lcd.print("0");
  lcd.print(second);
  lcd.print(" ");
  if (dayOfMonth < 10) lcd.print(" ");
  lcd.print(dayOfMonth);
  lcd.print("/");
  if (month < 10) lcd.print("0");
  lcd.print(month);
  lcd.print("/");
  lcd.print("20");
  lcd.print(year);
  // -------------------------------------- Вывод мощности 1 канала --------------------------------------
  lcd.setCursor(0, 1);
  lcd.print("CH1=");
  if (ch1 < 100 && ch1 >= 10) lcd.print(" ");
  if (ch1 < 10) lcd.print("  ");
  lcd.print(ch1);
  lcd.print("%");
  lcd.setCursor(10, 1);
  // -------------------------------------- Вывод мощности 2 канала --------------------------------------
  lcd.print("CH2=");
  if (ch2 < 100 && ch2 >= 10) lcd.print(" ");
  if (ch2 < 10) lcd.print("  ");
  lcd.print(ch2);
  lcd.print("%");
  lcd.setCursor(0, 2);
  // -------------------------------------- Вывод мощности вентиляторов --------------------------------------
  lcd.print("Fan=");
  if (fan < 100 && fan >= 10) lcd.print(" ");
  if (fan < 10) lcd.print("  ");
  lcd.print(fan);
  lcd.print("%");
  // -------------------------------------- Вывод состояния СО2 --------------------------------------
  lcd.setCursor(10, 2);
  lcd.print("CO2=");
  lcd.print(digitalRead(CO2) == HIGH ? "Off " : "On " );
  // -------------------------------------- Вывод состояния освещения --------------------------------------
  lcd.setCursor(0, 3);
  lcd.print("L=");
  lcd.print(lightOnOff == HIGH ? "1" : "0" );
  // -------------------------------------- Вывод состояния Фито Лампы --------------------------------------
  lcd.setCursor(4, 3);
  lcd.print("F=");
  lcd.print(digitalRead(FitoLamp) == HIGH ? "0" : "1" );
  // -------------------------------------- Вывод состояния UV --------------------------------------
  lcd.setCursor(8, 3);
  lcd.print("UV=");
  lcd.print(digitalRead(UV) == HIGH ? "0" : "1" );
  // -------------------------------------- Вывод состояния UV --------------------------------------
  lcd.setCursor(13, 3);
  lcd.print("M=");
  lcd.print(digitalRead(Moon) == HIGH ? "0" : "1" );
}


void menu(){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("       Setup        ");
  char menuTxt[8][20] = {"Set Clock >>      ", "Set Chanel 1 >>   ", "Set Chanel 2 >>   ", "Set Min & Max >>  ", "Set CO2 Time >>   ", "Set FitoLamp >>   ", "Reset to default>>", "Exit >>           "};
  byte pos = 0;
  timeKey = millis();

  while(1){  
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {lcd.clear(); return;}
    lcd.setCursor(0, 1);
    lcd.print(pos+1);
    lcd.print(".");
    lcd.print(menuTxt[pos]);

    if (downButton == 1 && pos != 0 && Flag == 0) pos--;
    if (upButton == 1 && pos < 7 && Flag == 0) pos++;

    
    if (rightButton == 1 && pos == 0 && Flag == 0) {lcd.clear(); setClock();}
    if (rightButton == 1 && pos == 1 && Flag == 0) {lcd.clear(); setCh1();}
    if (rightButton == 1 && pos == 2 && Flag == 0) {lcd.clear(); setCh2();}
    if (rightButton == 1 && pos == 3 && Flag == 0) {lcd.clear(); setMinMax();}
    if (rightButton == 1 && pos == 4 && Flag == 0) {lcd.clear(); setCO2();}
    if (rightButton == 1 && pos == 5 && Flag == 0) {lcd.clear(); setFitoLamp();}
    if (rightButton == 1 && pos == 6 && Flag == 0) {lcd.clear(); resetToDefault();}
    if (rightButton == 1 && pos == 7 && Flag == 0) {lcd.clear(); return;}
    
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
}
//------------------------------------------------------------------------------------------------ Set Clock ------------------------------------------------------------------------------------------------

void setClock(){
  timeKey = millis();
  byte pos = 0;
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.cursor();
  lcd.clear();
  lcd.blink();

  while(enterButton != 1){ // крутим   цикл   
    key(); // читаем состояние кнопок
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 60000) {lcd.clear(); return;}
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);     // выводим инфу
    if (hour < 10) lcd.print("0");
    lcd.print(hour);
    lcd.print(":");
    if (minute < 10) lcd.print("0"); 
    lcd.print(minute);  
    lcd.print(" ");     
    if (dayOfMonth < 10) lcd.print("0");
    lcd.print(dayOfMonth);
    lcd.print("/");
    if (month < 10) lcd.print("0");
    lcd.print(month);
    lcd.print("/");
    lcd.print(year);
    lcd.setCursor(pos, 0);

    if (pos < 12 && rightButton == 1 && Flag == 0) pos += 3;
    else if ( pos > 1 && leftButton == 1 && Flag == 0) pos -= 3;

    else if (pos == 0 && downButton == 1  && Flag == 0)   {hour = (hour == 23) ? 0:(hour+1);}    
    else if (pos == 0 && upButton == 1    && Flag == 0)   {hour = (hour == 0) ? 23:(hour-1);}
    else if (pos == 3 && downButton == 1  && Flag == 0)   {minute = (minute == 59) ? 0:(minute+1);}
    else if (pos == 3 && upButton == 1    && Flag == 0)   {minute = (minute == 0) ? 59:(minute-1);}
    else if (pos == 6 && downButton == 1  && Flag == 0)   {dayOfMonth = (dayOfMonth == 31) ? 1:(dayOfMonth+1);}
    else if (pos == 6 && upButton == 1    && Flag == 0)   {dayOfMonth = (dayOfMonth == 1) ? 31:(dayOfMonth-1);}
    else if (pos == 9 && downButton == 1  && Flag == 0)   {month = (month == 12) ? 1:(month+1);}
    else if (pos == 9 && upButton == 1    && Flag == 0)   {month = (month == 1) ? 12:(month-1);}
    else if (pos == 12 && downButton == 1 && Flag == 0)   {year = (year == 19) ? 14:(year+1);}   
    else if (pos == 12 && upButton == 1   && Flag == 0)   {year = (year == 14) ? 19:(year-1);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  second =0;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); 
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}

//------------------------------------------------------------------------------------------------ Set Chanel 1 ------------------------------------------------------------------------------------------------
void setCh1(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
  lcd.blink();

  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);
    if (Hours_Start_Sunrise[0] < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_Sunrise[0]); 
    if (Minutes_Start_Sunrise[0] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_Start_Sunrise[0]); 
    lcd.print (" ");
    if (Hours_End_Sunrise[0] < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_Sunrise[0]);   
    if (Minutes_End_Sunrise[0] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_Sunrise[0]);   
    lcd.print ("  ");
    if (Hours_Start_Sunset[0] < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_Sunset[0]);  
    if (Minutes_Start_Sunset[0] < 10)  {lcd.print ("0");} 
    lcd.print(Minutes_Start_Sunset[0]);  
    lcd.print (" ");
    if (Hours_End_Sunset[0] < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_Sunset[0]);    
    if (Minutes_End_Sunset[0] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_Sunset[0]);

    int massivePos[8] = {0, 2, 5, 7, 11, 13, 16, 18};
    pos = massivePos[i];
    lcd.setCursor(pos, 0);

    if (i<8 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 0 && downButton == 1  && Flag == 0)  {Hours_Start_Sunrise[0] =   (Hours_Start_Sunrise[0] == 23)   ? 0: (Hours_Start_Sunrise[0]+1);}    
    else if (pos == 0 && upButton == 1    && Flag == 0)  {Hours_Start_Sunrise[0] =   (Hours_Start_Sunrise[0] == 0)    ? 23:(Hours_Start_Sunrise[0]-1);}
    else if (pos == 2 && downButton == 1  && Flag == 0)  {Minutes_Start_Sunrise[0] = (Minutes_Start_Sunrise[0] == 50) ? 0: (Minutes_Start_Sunrise[0]+10);}
    else if (pos == 2 && upButton == 1    && Flag == 0)  {Minutes_Start_Sunrise[0] = (Minutes_Start_Sunrise[0] == 0)  ? 50:(Minutes_Start_Sunrise[0]-10);}
    else if (pos == 5 && downButton == 1  && Flag == 0)  {Hours_End_Sunrise[0] =     (Hours_End_Sunrise[0] == 23)     ? 0: (Hours_End_Sunrise[0]+1);}
    else if (pos == 5 && upButton == 1    && Flag == 0)  {Hours_End_Sunrise[0] =     (Hours_End_Sunrise[0] == 0)      ? 23:(Hours_End_Sunrise[0]-1);}
    else if (pos == 7 && downButton == 1  && Flag == 0)  {Minutes_End_Sunrise[0] =   (Minutes_End_Sunrise[0] == 50)   ? 0: (Minutes_End_Sunrise[0]+10);}
    else if (pos == 7 && upButton == 1    && Flag == 0)  {Minutes_End_Sunrise[0] =   (Minutes_End_Sunrise[0] == 0)    ? 50:(Minutes_End_Sunrise[0]-10);}
    else if (pos == 11 && downButton == 1 && Flag == 0)  {Hours_Start_Sunset[0] =    (Hours_Start_Sunset[0] == 23)    ? 0: (Hours_Start_Sunset[0]+1);}   
    else if (pos == 11 && upButton == 1   && Flag == 0)  {Hours_Start_Sunset[0] =    (Hours_Start_Sunset[0] == 0)     ? 23:(Hours_Start_Sunset[0]-1);}
    else if (pos == 13 && downButton == 1 && Flag == 0)  {Minutes_Start_Sunset[0] =  (Minutes_Start_Sunset[0] == 50)  ? 0: (Minutes_Start_Sunset[0]+10);}
    else if (pos == 13 && upButton == 1   && Flag == 0)  {Minutes_Start_Sunset[0] =  (Minutes_Start_Sunset[0] == 0)   ? 50:(Minutes_Start_Sunset[0]-10);}
    else if (pos == 16 && downButton == 1 && Flag == 0)  {Hours_End_Sunset[0] =      (Hours_End_Sunset[0] == 23)      ? 0: (Hours_End_Sunset[0]+1);}   
    else if (pos == 16 && upButton == 1   && Flag == 0)  {Hours_End_Sunset[0] =      (Hours_End_Sunset[0] == 0)       ? 23:(Hours_End_Sunset[0]-1);}
    else if (pos == 18 && downButton == 1 && Flag == 0)  {Minutes_End_Sunset[0] =    (Minutes_End_Sunset[0] == 50)    ? 0: (Minutes_End_Sunset[0]+10);}   
    else if (pos == 18 && upButton == 1   && Flag == 0)  {Minutes_End_Sunset[0] =    (Minutes_End_Sunset[0] == 0)     ? 50:(Minutes_End_Sunset[0]-10);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  lcd.clear();
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  EEPROM.write(10, Hours_Start_Sunrise[0]);
  EEPROM.write(11, Minutes_Start_Sunrise[0]);
  EEPROM.write(12, Hours_End_Sunrise[0]);
  EEPROM.write(13, Minutes_End_Sunrise[0]);  
  EEPROM.write(14, Hours_Start_Sunset[0]);
  EEPROM.write(15, Minutes_Start_Sunset[0]);
  EEPROM.write(16, Hours_End_Sunset[0]);
  EEPROM.write(17, Minutes_End_Sunset[0]);   
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}

//------------------------------------------------------------------------------------------------ Set Chanel 2 ------------------------------------------------------------------------------------------------
void setCh2(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
  lcd.blink();

  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);
    if (Hours_Start_Sunrise[1] < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_Sunrise[1]); 
    if (Minutes_Start_Sunrise[1] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_Start_Sunrise[1]); 
    lcd.print (" ");
    if (Hours_End_Sunrise[1] < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_Sunrise[1]);   
    if (Minutes_End_Sunrise[1] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_Sunrise[1]);   
    lcd.print ("  ");
    if (Hours_Start_Sunset[1] < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_Sunset[1]);  
    if (Minutes_Start_Sunset[1] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_Start_Sunset[1]);  
    lcd.print (" ");
    if (Hours_End_Sunset[1] < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_Sunset[1]);    
    if (Minutes_End_Sunset[1] < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_Sunset[1]);

    int massivePos[8] = {0, 2, 5, 7, 11, 13, 16, 18};
    pos = massivePos[i];
    lcd.setCursor(pos, 0);

    if (i<8 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 0 && downButton == 1 && Flag == 0)  {Hours_Start_Sunrise[1] =   (Hours_Start_Sunrise[1] == 23)   ? 0: (Hours_Start_Sunrise[1]+1);}    
    else if (pos == 0 && upButton == 1 && Flag == 0)  {Hours_Start_Sunrise[1] =   (Hours_Start_Sunrise[1] == 0)    ? 23:(Hours_Start_Sunrise[1]-1);}
    else if (pos == 2 && downButton == 1 && Flag == 0)  {Minutes_Start_Sunrise[1] = (Minutes_Start_Sunrise[1] == 50) ? 0: (Minutes_Start_Sunrise[1]+10);}
    else if (pos == 2 && upButton == 1 && Flag == 0)  {Minutes_Start_Sunrise[1] = (Minutes_Start_Sunrise[1] == 0)  ? 50:(Minutes_Start_Sunrise[1]-10);}
    else if (pos == 5 && downButton == 1 && Flag == 0)  {Hours_End_Sunrise[1] =     (Hours_End_Sunrise[1] == 23)     ? 0: (Hours_End_Sunrise[1]+1);}
    else if (pos == 5 && upButton == 1 && Flag == 0)  {Hours_End_Sunrise[1] =     (Hours_End_Sunrise[1] == 0)      ? 23:(Hours_End_Sunrise[1]-1);}
    else if (pos == 7 && downButton == 1 && Flag == 0)  {Minutes_End_Sunrise[1] =   (Minutes_End_Sunrise[1] == 50)   ? 0: (Minutes_End_Sunrise[1]+10);}
    else if (pos == 7 && upButton == 1 && Flag == 0)  {Minutes_End_Sunrise[1] =   (Minutes_End_Sunrise[1] == 0)    ? 50:(Minutes_End_Sunrise[1]-10);}
    else if (pos == 11 && downButton == 1 && Flag == 0) {Hours_Start_Sunset[1] =    (Hours_Start_Sunset[1] == 23)    ? 0: (Hours_Start_Sunset[1]+1);}   
    else if (pos == 11 && upButton == 1 && Flag == 0) {Hours_Start_Sunset[1] =    (Hours_Start_Sunset[1] == 0)     ? 23:(Hours_Start_Sunset[1]-1);}
    else if (pos == 13 && downButton == 1 && Flag == 0) {Minutes_Start_Sunset[1] =  (Minutes_Start_Sunset[1] == 50)  ? 0: (Minutes_Start_Sunset[1]+10);}
    else if (pos == 13 && upButton == 1 && Flag == 0) {Minutes_Start_Sunset[1] =  (Minutes_Start_Sunset[1] == 0)   ? 50:(Minutes_Start_Sunset[1]-10);}
    else if (pos == 16 && downButton == 1 && Flag == 0) {Hours_End_Sunset[1] =      (Hours_End_Sunset[1] == 23)      ? 0: (Hours_End_Sunset[1]+1);}   
    else if (pos == 16 && upButton == 1 && Flag == 0) {Hours_End_Sunset[1] =      (Hours_End_Sunset[1] == 0)       ? 23:(Hours_End_Sunset[1]-1);}
    else if (pos == 18 && downButton == 1 && Flag == 0) {Minutes_End_Sunset[1] =    (Minutes_End_Sunset[1] == 50)    ? 0: (Minutes_End_Sunset[1]+10);}   
    else if (pos == 18 && upButton == 1 && Flag == 0) {Minutes_End_Sunset[1] =    (Minutes_End_Sunset[1] == 0)     ? 50:(Minutes_End_Sunset[1]-10);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  lcd.clear();
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  EEPROM.write(20, Hours_Start_Sunrise[1]);
  EEPROM.write(21, Minutes_Start_Sunrise[1]);
  EEPROM.write(22, Hours_End_Sunrise[1]);
  EEPROM.write(23, Minutes_End_Sunrise[1]);  
  EEPROM.write(24, Hours_Start_Sunset[1]);
  EEPROM.write(25, Minutes_Start_Sunset[1]);
  EEPROM.write(26, Hours_End_Sunset[1]);
  EEPROM.write(27, Minutes_End_Sunset[1]);   
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}

//------------------------------------------------------------------------------------------------ Set Min & Max ------------------------------------------------------------------------------------------------
void setMinMax(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
  lcd.blink();
  int min0 = map(Midnight_PWM[0], 0, 255, 0, 100);
  int max0 = map(Meridian_PWM[0], 0, 255, 0, 100);
  int min1 = map(Midnight_PWM[1], 0, 255, 0, 100);
  int max1 = map(Meridian_PWM[1], 0, 255, 0, 100);
      
  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);
    lcd.print("  CH 1        CH 2  ");
    lcd.setCursor(0, 1);
    lcd.print("Min  Max    Min  Max");
    lcd.setCursor(0, 2);

    if (min0 < 100 && min0 >=10) lcd.print(" ");
    if (min0 < 10) lcd.print("  ");
    lcd.print (min0);
    lcd.setCursor(5, 2);
    if (max0 < 100 && max0 >=10) lcd.print(" ");
    if (max0 < 10) lcd.print("  ");
    lcd.print (max0);
    lcd.setCursor(12, 2);    
    if (min1 < 100 && min1 >=10) lcd.print(" ");
    if (min1 < 10) lcd.print("  ");
    lcd.print (min1);
    lcd.setCursor(17, 2);
    if (max1 < 100 && max1 >=10) lcd.print(" ");
    if (max1 < 10) lcd.print("  ");    
    lcd.print (max1);
    
    int massivePos[4] = {0, 5, 12, 17};
    pos = massivePos[i];
    lcd.setCursor(pos, 2);

    if (i<4 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 0 && downButton == 1 && Flag == 0)  {min0 = (min0 == 20)  ? 0:  (min0+5);}    
    else if (pos == 0 && upButton == 1 && Flag == 0)  {min0 = (min0 == 0)   ? 20: (min0-5);}
    else if (pos == 5 && downButton == 1 && Flag == 0)  {max0 = (max0 == 100) ? 40: (max0+5);}
    else if (pos == 5 && upButton == 1 && Flag == 0)  {max0 = (max0 == 40)  ? 100:(max0-5);}
    else if (pos == 12 && downButton == 1 && Flag == 0) {min1 = (min1 == 20)  ? 0:  (min1+5);}
    else if (pos == 12 && upButton == 1 && Flag == 0) {min1 = (min1 == 0)   ? 20: (min1-5);}
    else if (pos == 17 && downButton == 1 && Flag == 0) {max1 = (max1 == 100) ? 40: (max1+5);}
    else if (pos == 17 && upButton == 1 && Flag == 0) {max1 = (max1 == 40)  ? 100:(max1-5);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  lcd.clear();
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  Midnight_PWM[0] = map(min0, 0, 100, 0, 255);
  Meridian_PWM[0] = map(max0, 0, 100, 0, 255);
  Midnight_PWM[1] = map(min1, 0, 100, 0, 255);
  Meridian_PWM[1] = map(max1, 0, 100, 0, 255);
  EEPROM.write(30, Midnight_PWM[0]);
  EEPROM.write(31, Meridian_PWM[0]);
  EEPROM.write(32, Midnight_PWM[1]);
  EEPROM.write(33, Meridian_PWM[1]);  
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}


//------------------------------------------------------------------------------------------------ Set CO2 Time ------------------------------------------------------------------------------------------------
void setCO2(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
  lcd.blink();

  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);
    if (Hours_Start_CO2 < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_CO2); 
    if (Minutes_Start_CO2 < 10) {lcd.print ("0");} 
    lcd.print(Minutes_Start_CO2); 
    lcd.print (" ");
    if (Hours_End_CO2 < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_CO2);   
    if (Minutes_End_CO2 < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_CO2);   

    int massivePos[4] = {0, 2, 5, 7};
    pos = massivePos[i];
    lcd.setCursor(pos, 0);

    if (i<4 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 0 && downButton == 1 && Flag == 0)  {Hours_Start_CO2 =   (Hours_Start_CO2 == 23)   ? 0: (Hours_Start_CO2 + 1);}    
    else if (pos == 0 && upButton == 1 && Flag == 0)  {Hours_Start_CO2 =   (Hours_Start_CO2 == 0)    ? 23:(Hours_Start_CO2 - 1);}
    else if (pos == 2 && downButton == 1 && Flag == 0)  {Minutes_Start_CO2 = (Minutes_Start_CO2 == 50) ? 0: (Minutes_Start_CO2 +10);}
    else if (pos == 2 && upButton == 1 && Flag == 0)  {Minutes_Start_CO2 = (Minutes_Start_CO2 == 0)  ? 50:(Minutes_Start_CO2 -10);}
    else if (pos == 5 && downButton == 1 && Flag == 0)  {Hours_End_CO2 =     (Hours_End_CO2 == 23)     ? 0: (Hours_End_CO2 +1);}
    else if (pos == 5 && upButton == 1 && Flag == 0)  {Hours_End_CO2 =     (Hours_End_CO2 == 0)      ? 23:(Hours_End_CO2 -1);}
    else if (pos == 7 && downButton == 1 && Flag == 0)  {Minutes_End_CO2 =   (Minutes_End_CO2 == 50)   ? 0: (Minutes_End_CO2 +10);}
    else if (pos == 7 && upButton == 1 && Flag == 0)  {Minutes_End_CO2 =   (Minutes_End_CO2 == 0)    ? 50:(Minutes_End_CO2 -10);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  lcd.clear();
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  EEPROM.write(50, Hours_Start_CO2);
  EEPROM.write(51, Minutes_Start_CO2);
  EEPROM.write(52, Hours_End_CO2);
  EEPROM.write(53, Minutes_End_CO2);  
   
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}



//------------------------------------------------------------------------------------------------ Set FitoLamp Time ------------------------------------------------------------------------------------------------
void setFitoLamp(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
  lcd.blink();

  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print("     ESC to save    ");
    lcd.setCursor(0, 0);
    if (Hours_Start_FitoLamp < 10) {lcd.print (" ");} 
    lcd.print(Hours_Start_FitoLamp); 
    if (Minutes_Start_FitoLamp < 10) {lcd.print ("0");} 
    lcd.print(Minutes_Start_FitoLamp); 
    lcd.print (" ");
    if (Hours_End_FitoLamp < 10) {lcd.print (" ");} 
    lcd.print(Hours_End_FitoLamp);   
    if (Minutes_End_FitoLamp < 10) {lcd.print ("0");} 
    lcd.print(Minutes_End_FitoLamp);   

    int massivePos[4] = {0, 2, 5, 7};
    pos = massivePos[i];
    lcd.setCursor(pos, 0);

    if (i<4 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 0 && downButton == 1 && Flag == 0)  {Hours_Start_FitoLamp =   (Hours_Start_FitoLamp == 23)   ? 0: (Hours_Start_FitoLamp + 1);}    
    else if (pos == 0 && upButton == 1 && Flag == 0)  {Hours_Start_FitoLamp =   (Hours_Start_FitoLamp == 0)    ? 23:(Hours_Start_FitoLamp - 1);}
    else if (pos == 2 && downButton == 1 && Flag == 0)  {Minutes_Start_FitoLamp = (Minutes_Start_FitoLamp == 50) ? 0: (Minutes_Start_FitoLamp +10);}
    else if (pos == 2 && upButton == 1 && Flag == 0)  {Minutes_Start_FitoLamp = (Minutes_Start_FitoLamp == 0)  ? 50:(Minutes_Start_FitoLamp -10);}
    else if (pos == 5 && downButton == 1 && Flag == 0)  {Hours_End_FitoLamp =     (Hours_End_FitoLamp == 23)     ? 0: (Hours_End_FitoLamp +1);}
    else if (pos == 5 && upButton == 1 && Flag == 0)  {Hours_End_FitoLamp =     (Hours_End_FitoLamp == 0)      ? 23:(Hours_End_FitoLamp -1);}
    else if (pos == 7 && downButton == 1 && Flag == 0)  {Minutes_End_FitoLamp =   (Minutes_End_FitoLamp == 50)   ? 0: (Minutes_End_FitoLamp +10);}
    else if (pos == 7 && upButton == 1 && Flag == 0)  {Minutes_End_FitoLamp =   (Minutes_End_FitoLamp == 0)    ? 50:(Minutes_End_FitoLamp -10);}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}
  }
  lcd.clear();
  lcd.noCursor();
  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  EEPROM.write(60, Hours_Start_FitoLamp);
  EEPROM.write(61, Minutes_Start_FitoLamp);
  EEPROM.write(62, Hours_End_FitoLamp);
  EEPROM.write(63, Minutes_End_FitoLamp);  
   
  lcd.print("       Saved        ");
  delay(1500);
  lcd.clear();
}


//------------------------------------------------------------------------------------------------ Reset to default ------------------------------------------------------------------------------------------------
void resetToDefault(){
  int i = 0;
  int pos;
  timeKey = millis();
  lcd.cursor();
  lcd.clear();
//  lcd.blink();

  while(enterButton != 1){
    key();
    if (noneButton != 1) timeKey = millis();
    if ((millis() - timeKey) > 15000) {
      lcd.clear(); 
      timeKey = millis(); 
      return;
    }
    lcd.setCursor(0, 3);
    lcd.print(" Select Y for reset ");
    lcd.setCursor(0, 0);
    lcd.print (" N  N  Y  N  N  N "); 

    int massivePos[6] = {1, 4, 7, 10, 13, 16};
    pos = massivePos[i];
    lcd.setCursor(pos, 0);

    if (i<6 && rightButton == 1 && Flag == 0) i++;
    if (i>0 && leftButton == 1 && Flag == 0) i--;

    else if (pos == 7 && downButton == 1 && Flag == 0)  {resetDefault();return;}    
    else if (pos == 7 && upButton == 1 && Flag == 0)  {resetDefault(); return;}
    else if (pos != 7 && downButton == 1 && Flag == 0)  {lcd.clear(); return;}    
    else if (pos != 7 && upButton == 1 && Flag == 0)  {lcd.clear(); return;}
    if (noneButton != 1) {Flag = 1;} else {Flag = 0;}

  }
  lcd.clear();
  lcd.noCursor();
//  lcd.noBlink(); 
  lcd.setCursor(0, 1);
  lcd.print("       Exit         ");
  delay(1500);
  lcd.clear();
}


void resetDefault() {
EEPROM.write(10, 8);
EEPROM.write(11, 0);
EEPROM.write(12, 10);
EEPROM.write(13, 0);
EEPROM.write(14, 19);
EEPROM.write(15, 0);
EEPROM.write(16, 21);
EEPROM.write(17, 0);

EEPROM.write(20, 8);
EEPROM.write(21, 30);
EEPROM.write(22, 10);
EEPROM.write(23, 30);
EEPROM.write(24, 18);
EEPROM.write(25, 30);
EEPROM.write(26, 20);
EEPROM.write(27, 30);

EEPROM.write(30, 0);
EEPROM.write(31, 255);
EEPROM.write(32, 0);
EEPROM.write(33, 255);

EEPROM.write(50, 7);
EEPROM.write(51, 0);
EEPROM.write(52, 20);
EEPROM.write(53, 0);

EEPROM.write(60, 11);
EEPROM.write(61, 0);
EEPROM.write(62, 15);
EEPROM.write(63, 0);

Hours_Start_Sunrise[0] =   EEPROM.read(10);
Minutes_Start_Sunrise[0] = EEPROM.read(11);
Hours_End_Sunrise[0] =     EEPROM.read(12);
Minutes_End_Sunrise[0] =   EEPROM.read(13);
Hours_Start_Sunset[0] =    EEPROM.read(14);
Minutes_Start_Sunset[0] =  EEPROM.read(15);
Hours_End_Sunset[0] =      EEPROM.read(16);
Minutes_End_Sunset[0] =    EEPROM.read(17);

Hours_Start_Sunrise[1] =   EEPROM.read(20);
Minutes_Start_Sunrise[1] = EEPROM.read(21);
Hours_End_Sunrise[1] =     EEPROM.read(22);
Minutes_End_Sunrise[1] =   EEPROM.read(23);
Hours_Start_Sunset[1] =    EEPROM.read(24);
Minutes_Start_Sunset[1] =  EEPROM.read(25);
Hours_End_Sunset[1] =      EEPROM.read(26);
Minutes_End_Sunset[1] =    EEPROM.read(27);

Midnight_PWM[0] =          EEPROM.read(30);
Meridian_PWM[0] =          EEPROM.read(31);
Midnight_PWM[1] =          EEPROM.read(32);
Meridian_PWM[1] =          EEPROM.read(33);

Hours_Start_CO2  =         EEPROM.read(50);
Minutes_Start_CO2  =       EEPROM.read(51);
Hours_End_CO2  =           EEPROM.read(52);
Minutes_End_CO2 =          EEPROM.read(53);  

Hours_Start_FitoLamp  =    EEPROM.read(60);
Minutes_Start_FitoLamp  =  EEPROM.read(61);
Hours_End_FitoLamp  =      EEPROM.read(62);
Minutes_End_FitoLamp =     EEPROM.read(63);  

lcd.clear();
lcd.setCursor(0, 1);
lcd.print("     Reseting       ");
delay(1500);
lcd.clear();
return;
}

[/code]