Помогите разобраться в скетче

dominion
Offline
Зарегистрирован: 30.05.2016

Есть скетч автор не я. Бортовой компьютер авто. Поготе пожалуйста, не могу понять как реализована работа кнопок. Как куда подключать. Какие пины??

 


#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>

#include <OneWire.h>
#include <EEPROM.h>
#include "RTClib.h"
RTC_DS1307 rtc;

// set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x27,16,2);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

OneWire  ds(14);                 //датчик 18в20 на выводе А0

#define BUFFER_SIZE 100         // Буфер для хранения данных последовательного порта
#define buffer_errors_size 20   // Буфер для ошибок ЭБУ
#define backlight 11            // Вывод управления подсветкой дисплея
#define buzz 12                 // Вывод пищалки
#define FAN_HEAT 17             // Вывод управления вентилятором отопителя салона
#define ON_LIGHT 16             // вывод упраления светом

//--------------------------назначение ячеек в памяти EEPROM------------------------
#define eepautolightstat 10
#define eepautolightoff 11
#define eepautolighton 12
#define eepcoolfanstat 13
#define eepcoolfanoff 14
#define eepcoolfanon 15
#define eepheatfanon 16
#define eepheatfan1 17
#define eepheatfan2 18
#define eepbacklight 19

//-------------------------команды для обмена данными и управления ЭБУ-----------------------
unsigned char stopCommunication[]                  = {0x81, 0x10, 0xF1, 0x82, 0x04};
unsigned char startCommunication[]                 = {0x81, 0x10, 0xF1, 0x81, 0x03};                      //старт диагностического сеанса
unsigned char readDataByLocalIdentifier_RLI_ASS[]  = {0x82, 0x10, 0xF1, 0x21, 0x01, 0xA5};                //запрос диагностических данных
unsigned char readDTCByStatus[]                    = {0x84, 0x10, 0xF1, 0x18, 0x00, 0x00, 0x00, 0x9D};    //запрос кодов ошибок
unsigned char startcoolfan[]                       = {0x84, 0x10, 0xF1, 0x30, 0x0A, 0x06, 0x01, 0xC6};
unsigned char stopcoolfan[]                        = {0x83, 0x10, 0xf1, 0x30, 0x0A, 0x00, 0xBE};
unsigned char clearerrors[]                        = {0x83, 0x10, 0xF1, 0x14, 0x00, 0x00, 0x98};
unsigned char statusfan[]                          = {0x83, 0x10, 0xF1, 0x30, 0x0A, 0x01, 0xBF};

byte ds_check = 1;
byte val_heat = 1;
byte Spd = 1;
byte To = 1;
byte Toon = 0;
byte Gaz = 1;
short obr = 1;
float u = 1;
float FuelRateKm = 1;
float Fuelhoure = 1;
byte bot_long = 0;
int light_sensor = 0;
int lt_time = 0;
float celsius = 0;

byte eepromi;
byte eeprom1;
byte eeprom2;
int light_sensor_summ = 0;
byte val_coolfan_stat = 0;

byte kol_er = 0;

int yeartime;
byte monthtime;
byte daytime;
byte hourtime;
byte minutetime;
byte secondtime;
byte clockinit = 0;
byte clockfirst = 1;

byte eepromltst = 0;
byte eepromlton = 1;
byte eepromltoff = 1;
byte eeprombllight = 0;
byte eepromfan = 0;
byte eepromf1 = 0;
byte eepromf2 = 0;
byte eepromcool = 0;
byte eepromcooloff = 0;
byte eepromcoolon = 0;


byte bottom = 0;
byte Spd1 = 0;
byte To1 = 0;
byte Gaz1 = 0;
short obr1 = 0;
float u1 = 0;
float FuelRateKml = 0;
float Fuelhoure1 = 0;

byte i = 0;
unsigned char buffer[BUFFER_SIZE];                                      //буфер для приема сообщений с тачки
unsigned char buffer_errors [buffer_errors_size];
byte bufSize = 0;

uint8_t clr[8] = {B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000};

uint8_t l_h[8] = {B10000, B10001, B11010, B00100, B01000, B10101, B00111, B00101};  // Знак расход в час
uint8_t l1_100[8] = {B00100, B00100, B00100, B00111, B00000, B10111, B10101, B10111}; // Знак расхода на 100 старший
uint8_t l2_100[8] = {B00010, B00100, B01000, B10000, B00000, B11100, B10100, B11100}; // Знак расхода на 100 младший
// uint8_t load_5[8] = {B11100,B11100,B11100,B11111,B11111,B11111,B11111,B11111};
uint8_t temp[8] = {B11000, B11000, B00000, B00111, B00100, B00100, B00111, B00000}; // Знак температуры

void setup() {

  eeprominit();
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  //  lcd.init();
  //  lcd.backlight();

  //pinMode(14, OUTPUT);
  Serial.begin(10400);
  Wire.begin();
  rtc.begin();

  lcd.createChar(1, clr);
  lcd.createChar(3, l1_100);
  lcd.createChar(4, l2_100);
  lcd.createChar(5, l_h);
  lcd.createChar(6, temp);      //Максимум до восьми пользовательских символов  (0 - 7 нумерация)




  if (! rtc.isrunning()) {
    lcd.setCursor(0, 0);
    lcd.print("RTC is NOT");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(2016, 2, 14, 18, 40, 0));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  pinMode(backlight, OUTPUT);
  pinMode(FAN_HEAT, OUTPUT);
  pinMode(ON_LIGHT, OUTPUT);
  digitalWrite(FAN_HEAT, HIGH); // Выключить печку
  digitalWrite(ON_LIGHT, LOW); // Выключить автосвет
  analogWrite(backlight, 150); //Начальный уровень подсветки дисплея

  clockfirst = 1;
  lcd.clear();
  initcon();

  // lcd.clear();
}

void loop()
{

  //  Serial.write(startCommunication, 5);
  //  delay(30);

  Serial.write(readDataByLocalIdentifier_RLI_ASS, 6);     // Запрос на прием диагностических данных
  bott_150ms();


  //-----------------------------------------------------------Чтение данных из порта---------------------------------//
  readbuf();

  if (buffer[10] == 0x61) {             // Положительный ответ (buffer[10] == 0x61)           //
    Spd1 = buffer[29];                                      // Скорость авто                                      //
    Gaz1 = buffer[22];                                      // Положение дросельной заслонки                      //
    To1 = buffer[20] - 40;                                  // Температура ОЖ                                     //
    obr1 = buffer[23] * 40;                                 // Обороты двигателя                                  //
    u1 = 5.2 + buffer[30] * 0.05;                           // Напряжение бортовой сети                           //
    FuelRateKml = ((buffer[41] << 8) | buffer[40]) / 128.0; // Средний расход топлива на км                       //
    Fuelhoure1 = ((buffer[39] << 8) | buffer[38]) / 50.0;   // Средний расход топлива в час                       //

  }                                                                                                                //
  //------------------------------------------------------------------------------------------------------------------//

  //---------показания расхода топлива литров на 100км и скорости если скорость больше 0--------------------------

  if (Spd1 > 0)
  {
    if (Spd1 != Spd)
    {
      Spd = Spd1;
      lcd.setCursor(11, 1);
      lcd.print(char(1));
      if (Spd < 100) lcd.print (char(1));
      if (Spd < 10) lcd.print (char(1));
      lcd.print(Spd);
      lcd.print("S");
    }

    if ( FuelRateKm != FuelRateKml)
    {
      FuelRateKm = FuelRateKml;
      lcd.setCursor(4, 1);
      if (FuelRateKm < 100) lcd.print(char(1));
      if (FuelRateKm < 10) lcd.print(char(1));
      lcd.print(FuelRateKm, 1);
      lcd.print(char(3));
      lcd.print(char(4));
      lcd.print(char(1));
    }

  }

  //--------------------часы и расход топлива в час если скорость равно 0----------------
  if (Spd1 == 0)
  {

    DateTime now = rtc.now();
    lcd.setCursor(11, 1);
    if (now.hour() < 10) lcd.print ("0");
    lcd.print(now.hour(), DEC);
    lcd.print(":");
    if (now.minute() < 10) lcd.print ("0");
    lcd.print(now.minute(), DEC);

    /*
        int val = analogRead(A1);
        lcd.setCursor(11, 1);
        if (val < 1000) lcd.print(char(1));
        if (val < 100) lcd.print(char(1));
        lcd.print(val);
    */

    if (Fuelhoure != Fuelhoure1)
    {
      Fuelhoure = Fuelhoure1;
      lcd.setCursor(4, 1);
      if (Fuelhoure < 100) lcd.print(char(1));
      if (Fuelhoure < 10) lcd.print(char(1));
      lcd.print(Fuelhoure, 1);
      lcd.print(char(5));
      lcd.print(char(1));
    }
  }

  //------------температура салона или положение дросельной заслонки если она больше 0%-----------------------------------
  if (Gaz1 == 0)
  {
    dallas_read();
    lcd.setCursor(0, 1);
    if ((celsius > -10) && (celsius < 100)) lcd.print (char(1));
    if ((celsius < 10) && (celsius >= 0)) lcd.print (char(1));
    lcd.print(celsius, 0);
    lcd.print(char(6));       //Символ температуры
    lcd.print(char(1));
  } else if (Gaz1 != Gaz)
  {
    Gaz = Gaz1;
    lcd.setCursor(0, 1);
    lcd.print(char(1));
    if (Gaz < 10) lcd.print (char(1));
    lcd.print(Gaz);
    lcd.print("%");
  }

  //-----------------------------------показания оборотов-----------------------------------------------------------
  if (obr1 != obr)
  {
    obr = obr1;
    lcd.setCursor(6, 0);
    if (obr < 1000) lcd.print(char(1));
    if (obr < 100)
    {
      lcd.print(char(1));
      lcd.print(char(1));
      obr = 0;
    }
    lcd.print (obr);
  }

  //-------------------------------------показания температуры двигателя------------------------------------------------------
  if ((To1 != To))
  {
    To = To1;
    lcd.setCursor(0, 0);
    if ((To > -10) && (To < 100)) lcd.print (char(1));
    if ((To < 10) && (To >= 0)) lcd.print (char(1));
    lcd.print(To);
    lcd.print(char(6));       //Символ температуры
    lcd.print(char(1));
  }


  //----------------------------------------------показания напряжения------------------------------------------
  if (u1 != u)
  {
    u = u1;
    lcd.setCursor(11, 0);
    if (u < 10) lcd.print (char(1));
    lcd.print(u, 1);
    lcd.print("V");
  }






  //-----------------процедура включения вентилятора печки салона----------------------
  salon_fan_on();                                                                   //
  //-----------------------------------------------------------------------------------

  //-----------------процедура автосвета-----------------------------------------------
  auto_and_backlight();                                                             //
  //-----------------------------------------------------------------------------------

  //----- процедура принудительного включения вентилятора радиатора ------------------
  checkcoolfan();                                                                  //
  //----------------------------------------------------------------------------------


  //-------------------------------------опрос кнопок------------------------------------------
  bottoms();
  // if (bottom == 111) alarm_OIL(); //--Если лампочка масла горит длительное время-------------

  if (bottom == 21) time_5sec();  //------------краткое нажатие кнопки вверх-------------------

  if (bottom == 32) setupmenu(); //--------длительное нажатие кнопки ввниз---------------------

  if (bottom == 31) control_stat(); //-----------краткое нажатие кнопки ввниз------------------

  if (bottom == 22) check_errors(); //----------длительное нажатие кнопки вверх----------------



}

////////////////////////////////////////////////END LOOP///////////////////////////////////////

//---------------------показ часов продолжительностью 5 секунд-------------------------------
void time_5sec()
{
  bottom = 0;
  lcd.clear();
  for (int t_v = 0; t_v < 30; t_v++) //-----продолжительности показа часов при кратком нажатии вверх--------
  {
    clockRTC();
    bott_150ms();
  }

  initcon();
}

void light_sensor_read()
{
  for (int summ = 1; summ <= 10; summ ++)
  {

    light_sensor_summ = light_sensor_summ + (analogRead(A7));
    delay(1);
  }
  light_sensor = light_sensor_summ / 10;
  light_sensor_summ = 0;
}

void auto_and_backlight()
{
  //-------------показания датчика освещенности и включение света и приглушение подсветки с интервалом 10 сек---------------------
  eepromltst = EEPROM.read(eepautolightstat);

  //light_sensor = analogRead(A7); //----чтение показания датчика освещенности---------
  eepromlton = EEPROM.read(eepautolighton);

  eepromltoff = EEPROM.read(eepautolightoff);

  eeprombllight = EEPROM.read(eepbacklight);

  /*
  lcd.setCursor(0, 1);
  lcd.print(eepromltoff);
  lcd.setCursor(6, 1);
  lcd.print(light_sensor);
  lcd.setCursor(11, 1);
  lcd.print(eepromlton);
  lcd.setCursor(0, 0);
  lcd.print(eepromltst);
  */
  light_sensor_read();

  if (light_sensor > (eepromlton * 4))
  {
    lt_time = lt_time + 1;
    if (lt_time == 25)     //-------------задержка времени включения
    {
      //  lt_time = 0;
      analogWrite (backlight, eeprombllight); //-----притушить подсветку дисплея согласно переменной
      beep();
      if (eepromltst == 1 && obr1 > 600) digitalWrite(ON_LIGHT, HIGH);              //-----включить свет
    }
  }



  if (light_sensor < (eepromltoff * 4))
  {
    lt_time = lt_time + 1;
    if (lt_time == 25)       //-------------задержка времени выключения
    {
      // lt_time = 0;
      beep();
      analogWrite (backlight, 150);   //----максимальная яркость дисплея
      if (eepromltst == 1) digitalWrite(ON_LIGHT, LOW);          //----выключить свет
    }
  }

  if ((light_sensor > (eepromltoff * 4)) && (light_sensor < (eepromlton * 4))) lt_time = 0;
  if (lt_time > 30000) lt_time = 0;
}

void salon_fan_on()
{
  //-------- Если температура салона или температура двигателя выше заданных включить печку, ----
  //--------------если это разрешено иначе включить печку постоянно---------------------
  eepromfan = EEPROM.read(eepheatfanon);
  if (eepromfan == 1)
  {
    eepromf1 = EEPROM.read(eepheatfan1);
    eepromf2 = EEPROM.read(eepheatfan2);
    val_heat = val_heat + 1;

    if (val_heat == 20) val_heat = 1;

    if (((celsius >= eepromf1 && celsius < 80) || To >= eepromf2) && val_heat == 1)
    {
      val_heat = 0;
      digitalWrite(FAN_HEAT, LOW);
      beep();
    }

  }
  if (eepromfan == 0) digitalWrite (FAN_HEAT, LOW);
}


void alarm_OIL()
{
  bottom = 0;
  if ( obr1 > 500)
  {
    int alarm_n = 10; //---продолжительность сигнализации------------
    do
    {

      lcd.clear();
      lcd.setCursor(2, 0);
      lcd.print("LOW Pressure");
      lcd.setCursor(6, 1);
      lcd.print("OIL");
      analogWrite(backlight, 0);
      long_beep();
      analogWrite(backlight, 255);
      delay(500);
      alarm_n = alarm_n - 1;
    } while (alarm_n != 0);
    initcon();
  }

}

void long_beep()
{
  digitalWrite(buzz, HIGH);
  delay(500);
  digitalWrite(buzz, LOW);
}

void control_stat()
{
  bottom = 0;
  lcd.clear();
  // int val_light = digitalRead(ON_LIGHT); //----считывание выхода света--------------

  lcd.setCursor(0, 0);
  lcd.print("Salon");
  lcd.setCursor(6, 0);
  if (val_heat == 0) lcd.print("ON");
  if (val_heat != 0) lcd.print("OFF");
  lcd.setCursor(0, 1);
  lcd.print("Cool");
  lcd.setCursor(5, 1);
  if (val_coolfan_stat == 1) lcd.print("ON");
  if (val_coolfan_stat == 0) lcd.print("OFF");
  lcd.setCursor(11, 0);
  lcd.print("Light");
  // lcd.setCursor(12, 1);
  light_sensor_read();
  lcd.setCursor(11, 1);
  if (light_sensor < 1000) lcd.print(char(1));
  if (light_sensor < 100) lcd.print(char(1));
  lcd.print(light_sensor);
  //  if (val_light == 1) lcd.print("ON");
  //  if (val_light == 0) lcd.print("OFF");
  delay(5000); //-----продолжительность показа состояния выходов-------
  initcon();

}

//--------------------включение/выключение вентилятора радиатора если разрешено----------------
void checkcoolfan()
{

  eepromcool = EEPROM.read(eepcoolfanstat);
  if ((eepromcool == 1) && (To1 != Toon))
  {
    eepromcooloff = EEPROM.read(eepcoolfanoff);
    eepromcoolon = EEPROM.read(eepcoolfanon);
    Toon = To1;
    if (Toon >= eepromcoolon && val_coolfan_stat == 0)
    {
      val_coolfan_stat = 1;
      Serial.write(startcoolfan, 8);
      beep();
      lcd.setCursor(4, 0);
      lcd.print("*");
      // delay(25);
    }
    if (Toon <= eepromcooloff && val_coolfan_stat == 1)
    {
      val_coolfan_stat = 0;
      Serial.write(stopcoolfan, 7);
      beep();
      lcd.setCursor(4, 0);
      lcd.print(char(1));
      // delay(25);
    }
  }
}

//-----------------------снятие показаний датчика температуры------------------------------
void dallas_read()
{

  byte i;
  byte type_s = 0;
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    ds.reset_search();
    bott_150ms();
    bott_150ms();
    return;
  }

  ds.reset();
  ds.select(addr);       // Выбираем адрес
  //ds.write (0xCC);
  ds.write(0x44);     // Производим замер
  delay(50);

  do
  {
    ds_check = digitalRead(14);
    bottoms();
    delay(50);
  } while (ds_check == 0);



  //  bott_500ms();
  //  bott_500ms();
  ds.reset();
  ds.select(addr);
  //ds.write (0xCC);
  ds.write(0xBE);         // Считываем оперативную память датчика

  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();  // Заполняем массив считанными данными
  }

  // Данные о температуре содержатся в первых двух байтах, переведем их в одно значение и преобразуем в шестнадцатиразрядное число
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3;
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  }
  else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;
    else if (cfg == 0x20) raw = raw & ~3;
    else if (cfg == 0x40) raw = raw & ~1;
  }
  celsius =  (float)raw / 16.0;
}

void clockRTC()
{
  DateTime now = rtc.now();
  lcd.setCursor(3, 0);
  if (now.day() < 10) lcd.print ("0");
  lcd.print(now.day(), DEC);
  lcd.print(".");
  if (now.month() < 10) lcd.print ("0");
  lcd.print(now.month(), DEC);
  lcd.print(".");
  lcd.print(now.year(), DEC);
  lcd.setCursor(4, 1);
  if (now.hour() < 10) lcd.print ("0");
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  if (now.minute() < 10) lcd.print ("0");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  if (now.second() < 10) lcd.print ("0");
  lcd.print(now.second(), DEC);

  if (bottom == 32) setupmenu();
}



//--------------------процедура установки связи с ЭБУ-------------------------------------
void initcon()
{


  do
  {
    Serial.write(stopCommunication, 5);
    bott_150ms();
    Serial.write(startCommunication, 5);
    bott_150ms();
    readbuf();
    if (clockfirst == 1)
    {
      /*
      dallas_read();
      lcd.setCursor(12, 1);
      if ((celsius > -10) && (celsius < 100)) lcd.print (char(1));
      if ((celsius < 10) && (celsius >= 0)) lcd.print (char(1));
      lcd.print(celsius, 0);
      lcd.print(char(6));       //Символ температуры
      */
      clockRTC();
    }
  } while (buffer[18] != 0xC1);

  beep();
  if (clockfirst == 1)
  {
    delay(500);
    clockfirst = 2;
  }
  Spd = 1;
  To = 1;
  Gaz = 1;
  obr = 1;
  u = 1;
  FuelRateKm = 1;
  Fuelhoure = 1;
  lt_time = 0;

  //  eepromltst = EEPROM.read(eepautolightstat);
  //  eepromlton = EEPROM.read(eepautolighton) * 4;
  //  eepromltoff = EEPROM.read(eepautolightoff) * 4;
  //  eeprombllight = EEPROM.read(eepbacklight);
  //  eepromfan = EEPROM.read(eepheatfanon);
  //  eepromf1 = EEPROM.read(eepheatfan1);
  //  eepromf2 = EEPROM.read(eepheatfan2);
  //  eepromcool = EEPROM.read(eepcoolfanstat);
  //  eepromcooloff = EEPROM.read(eepcoolfanoff);
  //  eepromcoolon = EEPROM.read(eepcoolfanon);

  lcd.clear();

}

//----------------процедура чтения данных из буфера последовательного порта------------------
void readbuf()
{
  bufSize = 0;
  if (Serial.available() > 0)
  {
    bufSize = Serial.available();                                                                               //
    for (i = 0; i < bufSize; i++)
      buffer[i] = Serial.read();
  }
}

// -------------------------процедура проверки на наличие ошибок------------------------------
void check_errors()
{
  bottom = 0;

  initcon();
  Serial.write(readDTCByStatus, 8);     // Запрос на прием диагностических данных
  delay(200);
  readbuf();
  if (buffer[11] == 0x58);
  {
    kol_er = buffer[12];
    lcd.setCursor(0, 0);
    lcd.print("Err:");
    if (kol_er != 0)
    {
      bottom = 0;
      clockinit = 20;

      int p = 10;
      int h = 0;
      for (i = 0; i < kol_er; i++)
      {
        p = p + 3;
        buffer_errors[h] = buffer[p];
        buffer_errors[h + 1] = buffer[p + 1];
        h = h + 2;
      }

      lcd.setCursor(4, 0);
      lcd.print(kol_er);
      lcd.setCursor(6, 0);
      lcd.print("P");
      if (buffer_errors[0] < 16) lcd.print("0");
      lcd.print(buffer_errors[0], HEX);
      lcd.print(buffer_errors[1], HEX);

      if (kol_er > 1 )
      {
        lcd.setCursor(0, 1);
        lcd.print("P");
        if (buffer_errors[2] < 16) lcd.print("0");
        lcd.print(buffer_errors[2], HEX);
        lcd.print(buffer_errors[3], HEX);
      }

      if (kol_er > 2)
      {
        lcd.setCursor(6, 1);
        lcd.print("P");
        if (buffer_errors[4] < 16) lcd.print("0");
        lcd.print(buffer_errors[4], HEX);
        lcd.print(buffer_errors[5], HEX);
      }
      do
      { lcd.setCursor(12, 0);
        lcd.print("Igno");
        lcd.setCursor(12, 1);
        lcd.print(char(1));
        lcd.print(char(1));
        lcd.print(char(1));
        lcd.print(char(1));
        bott_500ms();

        lcd.setCursor(12, 1);
        if (kol_er > 3) lcd.print ("Next");
        else lcd.print(" Del");
        lcd.setCursor(12, 0);
        lcd.print(char(1));
        lcd.print(char(1));
        lcd.print(char(1));
        lcd.print(char(1));
        bott_500ms();

        if ((bottom == 32) && (kol_er > 3)) next_errors();
        if ((bottom == 32) && (kol_er <= 3)) clear_errors();
        //    if (bottom == 31) clockinit = clockinit + 1;
        //if (bottom == 21) clockinit = clockinit - 1;
        if (bottom == 22) clockinit = 55;

      } while (clockinit == 20);
      bottom = 0;
      clockinit = 0;
    }
    else
    {
      lcd.setCursor(5, 0);
      lcd.print(kol_er);
      delay(2000);
    }
  }
  initcon();
}

//---------------------процедура удаления ошибок--------------------------------
void clear_errors()
{
  bottom = 0;
  clockinit = 55;
  initcon();
  Serial.write(clearerrors, 7);
  lcd.clear();

  lcd.setCursor(1, 0);
  lcd.print("Errors DELETE");
  delay(1000);

}

void next_errors()
{
  bottom = 0;
  lcd.clear();

  if (kol_er > 3 )
  {
    lcd.setCursor(0, 0);
    lcd.print("P");
    if (buffer_errors[6] < 16) lcd.print("0");
    lcd.print(buffer_errors[6], HEX);
    lcd.print(buffer_errors[7], HEX);
  }
  if (kol_er > 4 )
  {
    lcd.setCursor(6, 0);
    lcd.print("P");
    if (buffer_errors[8] < 16) lcd.print("0");
    lcd.print(buffer_errors[8], HEX);
    lcd.print(buffer_errors[9], HEX);
  }
  if (kol_er > 5 )
  {
    lcd.setCursor(0, 1);
    lcd.print("P");
    if (buffer_errors[10] < 16) lcd.print("0");
    lcd.print(buffer_errors[10], HEX);
    lcd.print(buffer_errors[11], HEX);
  }
  if (kol_er > 6 )
  {
    lcd.setCursor(6, 1);
    lcd.print("P");
    if (buffer_errors[12] < 16) lcd.print("0");
    lcd.print(buffer_errors[12], HEX);
    lcd.print(buffer_errors[13], HEX);
  }
  do
  { lcd.setCursor(12, 0);
    lcd.print("Igno");
    lcd.setCursor(12, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(12, 1);
    lcd.print(" Del");
    lcd.setCursor(12, 0);
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    if (bottom == 32) clear_errors();
    //    if (bottom == 31) clockinit = clockinit + 1;
    //if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;

  } while (clockinit == 20);
  bottom = 0;
  //clockinit = 0;
}


//---------------процедура короткого сигнала----------------------------------------
void beep()
{
  digitalWrite(buzz, HIGH);
  delay(50);
  digitalWrite(buzz, LOW);
}

//-----------------процедура опроса кнопок и тревожного входа низкого давления масла----------
void bottoms()
{
  int val = analogRead(A1);

  if (val < 250)
  {
    bottom = 1;
    if (bot_long == 1) beep();
    if (bot_long == 20) long_beep();
    bot_long = bot_long + 1;
  }

  if (val > 410 && val < 540)       // для машины
    // if (val > 320 && val < 420)       // для программатора
  {
    bottom = 2;
    if (bot_long == 1) beep();
    if (bot_long == 20) long_beep();
    bot_long = bot_long + 1;
  }

  if (val > 580 && val < 700)       // для машины
    // if (val > 430 && val < 530)       // для программатора
  {
    bottom = 3;
    if (bot_long == 1) beep();
    if (bot_long == 20) long_beep();
    bot_long = bot_long + 1;
  }

  if ((bottom == 1) && (bot_long > 25))
  {
    bot_long = 0;
    bottom == 111;
  }

  if ((bottom == 1) && (val > 840))
  {
    if (bot_long < 20)
    {
      bot_long = 0;
      bottom = 11;
    }
    if (bot_long >= 20)
    {
      bot_long = 0;
      bottom = 12;
    }
  }

  if ((bottom == 2) && (val > 840))
  {
    if (bot_long < 20)
    {
      bot_long = 0;
      bottom = 21;
    }
    if (bot_long >= 20)
    {
      bot_long = 0;
      bottom = 22;
    }
  }

  if ((bottom == 3) && (val > 840))
  {
    if (bot_long < 20)
    {
      bot_long = 0;
      bottom = 31;
    }
    if (bot_long >= 20)
    {
      bot_long = 0;
      bottom = 32;
    }
  }


}

//----------------------выбор меню установки----------------------------------
void setupmenu()
{
  clockinit = 1;
  do
  {

    //bottoms();
    if (clockinit == 1) setclock();

    if (clockinit == 2) setdate();

    if (clockinit == 3) autolight_on();

    if (clockinit == 4) autolight_level();

    if (clockinit == 5) coolfan_on();

    if (clockinit == 6) coolfan_engine();

    if (clockinit == 7) heatfan_on();

    if (clockinit == 8) heatfan_level();

    if (clockinit == 9) backlight_level();

    //   if (clockinit == 10) beep_on();

    if (bottom == 22) clockinit = 55;

    if ((clockinit < 1) && (clockinit != 55)) clockinit = 9;

    if ((clockinit > 9) && (clockinit != 55)) clockinit = 1;


    bott_150ms();

  } while (clockinit < 50);
  initcon();
}
//-------------------меню установки часов-----------------------------------------
void setclock()
{
  read_datetime();
  bottom = 0;
  lcd.clear();

  do
  {
    lcd.setCursor(0, 0);
    lcd.print ("Set Clock");
    lcd.setCursor (2, 1);
    if (hourtime < 10) lcd.print (char(1));
    lcd.print(hourtime, DEC);
    lcd.print(":");
    if (minutetime < 10) lcd.print ("0");
    lcd.print(minutetime, DEC);

    bott_150ms();
    if (bottom == 32) setclockon();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 1);
  bottom = 0;
  //  lcd.clear();
  //  DateTime now = rtc.now();

}

//--------------------установка часов времени-----------------------------------------
void setclockon()
{
  bottom = 0;
  clockinit = 1;
  do
  {

    lcd.setCursor(2, 1);
    lcd.print(char(1));
    lcd.print(char(1));

    bott_500ms();

    lcd.setCursor(2, 1);
    if (hourtime < 10) lcd.print("0");
    lcd.print(hourtime);

    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      hourtime = hourtime + 1;
      if (hourtime > 24) hourtime = 0;
    }
    if (bottom == 21)
    {
      bottom = 0;
      hourtime = hourtime - 1;
      if (hourtime < 0) hourtime = 23;
    }
    if (bottom == 32) setminuteon();;
    if (bottom == 22) clockinit = 55;
  } while (clockinit < 50);
  bottom = 0;
  rtc.adjust(DateTime(yeartime, monthtime, daytime, hourtime, minutetime, 0));
  clockinit = 1;
}

//---------------------установка минут времени-------------------------------------
void setminuteon()
{
  bottom = 0;
  clockinit = 1;
  do
  {

    lcd.setCursor(5, 1);
    lcd.print(char(1));
    lcd.print(char(1));

    bott_500ms();

    lcd.setCursor(5, 1);
    if (minutetime < 10) lcd.print("0");
    lcd.print(minutetime);

    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      minutetime = minutetime + 1;
      if (minutetime > 59) minutetime = 0;
    }
    if (bottom == 21)
    {
      bottom = 0;
      minutetime = minutetime - 1;
      if (minutetime < 0) minutetime = 59;
    }

    if ((bottom == 32) || (bottom == 22)) clockinit = 55;
  } while (clockinit < 50);
  bottom = 0;



}

//--------------------меню установки числа, месяца, года------------------------------
void setdate()
{
  read_datetime();
  bottom = 0;
  lcd.clear();
  do
  {

    lcd.setCursor(0, 0);
    lcd.print ("Set Date");
    lcd.setCursor (2, 1);
    if (daytime < 10) lcd.print ("0");
    lcd.print(daytime, DEC);
    lcd.print(".");
    if (monthtime < 10) lcd.print ("0");
    lcd.print(monthtime, DEC);
    lcd.print(".");
    lcd.print(yeartime, DEC);

    bott_150ms();
    if (bottom == 32) setdateon();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 2);
  bottom = 0;
  //    lcd.clear();

}

//----------------установка даты дня---------------------------------------------
void setdateon()
{
  bottom = 0;
  do
  {

    lcd.setCursor(2, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();
    lcd.setCursor(2, 1);
    if (daytime < 10) lcd.print("0");
    lcd.print(daytime);
    bott_500ms();
    if (bottom == 31)
    {
      bottom = 0;
      daytime = daytime + 1;
      if (daytime > 31) daytime = 1;
    }
    if (bottom == 21)
    {
      bottom = 0;
      daytime = daytime - 1;
      if (daytime < 1)daytime = 31;
    }
    if (bottom == 32) setmonthon();
    if (bottom == 22) clockinit = 55;
  } while (clockinit < 50);
  bottom = 0;
  DateTime now = rtc.now();

  hourtime = now.hour();
  minutetime = now.minute();
  secondtime = now.second();
  rtc.adjust(DateTime(yeartime, monthtime, daytime, hourtime, minutetime, 0));
  clockinit = 2;
}


//-----------------------установка месяца------------------------------
void setmonthon()
{
  bottom = 0;
  do
  {
    lcd.setCursor(5, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();
    lcd.setCursor(5, 1);
    if (monthtime < 10) lcd.print ("0");
    lcd.print(monthtime);
    bott_500ms();
    if (bottom == 31)
    {
      bottom = 0;
      monthtime = monthtime + 1;
      if (monthtime > 12) monthtime = 1;
    }
    if (bottom == 21)
    {
      bottom = 0;
      monthtime = monthtime - 1;
      if (monthtime < 1) monthtime = 12;
    }
    if (bottom == 32) setyearon();

    if (bottom == 22) clockinit = 55;
  } while (clockinit < 50);
  bottom = 0;
  //clockinit = 2;

}
//----------------------установка года------------------
void setyearon()
{
  bottom = 0;
  do
  {
    lcd.setCursor(8, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();
    lcd.setCursor(8, 1);
    lcd.print(yeartime);
    bott_500ms();
    if (bottom == 31)
    {
      bottom = 0;
      yeartime = yeartime + 1;
    }
    if (bottom == 21)
    {
      bottom = 0;
      yeartime = yeartime - 1;
      if (yeartime < 2000) yeartime = 2000;
    }
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit < 50);
  bottom = 0;

}
//-------------------------меню установки функции включения автосвет-------------------
void autolight_on()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print ("Autolight");
  lcd.setCursor (13, 0);
  lcd.print("ON");
  lcd.setCursor(13, 1);
  lcd.print("OFF");
  eepromi = EEPROM.read(eepautolightstat);
  do
  {
    if (eepromi == 1)
    {
      lcd.setCursor(12, 1);
      lcd.print(char(1));
      lcd.setCursor(12, 0);
      lcd.print(">");
    }
    if (eepromi == 0)
    {
      lcd.setCursor(12, 0);
      lcd.print(char(1));
      lcd.setCursor(12, 1);
      lcd.print(">");
    }



    bott_500ms();
    if (bottom == 32) set_autolight_stat();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 3);
  bottom = 0;
  if (eepromi == 0) digitalWrite(ON_LIGHT, LOW);
  EEPROM.write(eepautolightstat, eepromi);
}
//------------------------установка включения функции автосвет--------------------
void set_autolight_stat()
{
  bottom = 0;
  do
  {
    lcd.setCursor(12, 0);
    lcd.print(char(1));
    lcd.setCursor(12, 1);
    lcd.print(char(1));
    bott_500ms();
    if (eepromi == 1)
    {

      lcd.setCursor(12, 0);
      lcd.print(">");
    }
    if (eepromi == 0)
    {

      lcd.setCursor(12, 1);
      lcd.print(">");
    }
    bott_500ms();
    if (bottom == 31) eepromi = 0;
    if (bottom == 21) eepromi = 1;
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 3);
  bottom = 0;
  clockinit = 3;
}

//----------------------------меню установки порога автосвет--------------------------------
void autolight_level()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print ("Autolight LEVEL");

  eeprom1 = EEPROM.read(eepautolightoff);
  eeprom2 = EEPROM.read(eepautolighton);
  lcd.setCursor(0, 1);
  if ((eeprom1 * 4) < 1000) lcd.print (char(1));
  if ((eeprom1 * 4) < 100) lcd.print (char(1));
  lcd.print(eeprom1 * 4);
  lcd.setCursor(12, 1);
  if ((eeprom2 * 4) < 1000) lcd.print(char(1));
  if ((eeprom2 * 4) < 100) lcd.print(char(1));
  lcd.print(eeprom2 * 4);

  do
  {
    //light_sensor = analogRead(A7);
    light_sensor_read();
    lcd.setCursor(6, 1);
    if (light_sensor < 1000) lcd.print(char(1));
    if (light_sensor < 100) lcd.print(char(1));
    lcd.print(light_sensor);
    bott_500ms();
    if (bottom == 32) set_autolight_off();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 4);
  bottom = 0;
  EEPROM.write(eepautolightoff, eeprom1);
  EEPROM.write(eepautolighton, eeprom2);

}

//--------------Установка порога выключения света----------------
void set_autolight_off()
{
  bottom = 0;
  do
  {
    //light_sensor = analogRead(A7);
    light_sensor_read();
    lcd.setCursor(6, 1);
    if (light_sensor < 1000) lcd.print(char(1));
    if (light_sensor < 100) lcd.print(char(1));
    lcd.print(light_sensor);
    //  lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print(char(1));
    lcd.setCursor(1, 1);
    lcd.print(char(1));
    lcd.setCursor(2, 1);
    lcd.print(char(1));
    lcd.setCursor(3, 1);
    lcd.print(char(1));
    bott_500ms();
    lcd.setCursor(0, 1);
    if ((eeprom1 * 4) < 1000) lcd.print (char(1));
    if ((eeprom1 * 4) < 100) lcd.print (char(1));

    lcd.print(eeprom1 * 4);
    bott_500ms();
    if (bottom == 31)
    {
      bottom = 0;
      eeprom1 = eeprom1 + 5;
      if (eeprom1 >= 250) eeprom1 = 250;
    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom1 = eeprom1 - 5;
      if (eeprom1 <= 5) eeprom1 = 5;
    }
    if (bottom == 32) set_autolight_on();
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 4);
  bottom = 0;
  clockinit = 4;
}

void set_autolight_on()
{
  bottom = 0;
  if (eeprom1 >= eeprom2) eeprom2 = eeprom1 + 10;
  do
  {
    //light_sensor = analogRead(A7);
    light_sensor_read();
    lcd.setCursor(6, 1);
    if (light_sensor < 1000) lcd.print(char(1));

    lcd.print(light_sensor);
    //  lcd.clear();
    lcd.setCursor(12, 1);
    lcd.print(char(1));
    lcd.setCursor(13, 1);
    lcd.print(char(1));
    lcd.setCursor(14, 1);
    lcd.print(char(1));
    lcd.setCursor(15, 1);
    lcd.print(char(1));
    bott_500ms();
    lcd.setCursor(12, 1);
    if ((eeprom2 * 4) < 1000) lcd.print(char(1));
    if ((eeprom2 * 4) < 100) lcd.print(char(1));

    lcd.print(eeprom2 * 4);
    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      eeprom2 = eeprom2 + 5;
      if (eeprom2 >= 250) eeprom2 = 250;
    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom2 = eeprom2 - 5;
      if (eeprom2 <= 5) eeprom2 = 5;
      if (eeprom1 >= eeprom2) eeprom2 = eeprom1 + 5;
    }

    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 4);
  bottom = 0;
}

//-----------меню установки вентилятора радиатора-----------------------
void coolfan_on()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiator");
  lcd.setCursor(2, 1);
  lcd.print("FAN");
  lcd.setCursor(13, 0);
  lcd.print("ON");
  //  lcd.setCursor(15,0)
  //  lcd.print(char(6));
  lcd.setCursor(13, 1);
  lcd.print("OFF");
  //  lcd.setCursor(15,1);
  // lcd.print(char(6));
  eepromi = EEPROM.read(eepcoolfanstat);
  do
  {
    if (eepromi == 1)
    {
      lcd.setCursor(12, 1);
      lcd.print(char(1));
      lcd.setCursor(12, 0);
      lcd.print(">");
    }
    if (eepromi == 0)
    {
      lcd.setCursor(12, 0);
      lcd.print(char(1));
      lcd.setCursor(12, 1);
      lcd.print(">");
    }



    bott_500ms();
    if (bottom == 32) set_coolfan_stat();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 5);
  bottom = 0;

  EEPROM.write(eepcoolfanstat, eepromi);
}

//-------------------Установка принудительного включения вентилятора радиатора ON OFF---------
void set_coolfan_stat()
{
  bottom = 0;
  do
  {
    lcd.setCursor(12, 0);
    lcd.print(char(1));
    lcd.setCursor(12, 1);
    lcd.print(char(1));
    bott_500ms();
    if (eepromi == 1)
    {

      lcd.setCursor(12, 0);
      lcd.print(">");
    }
    if (eepromi == 0)
    {

      lcd.setCursor(12, 1);
      lcd.print(">");
    }
    bott_500ms();


    if (bottom == 31) eepromi = 0;
    if (bottom == 21) eepromi = 1;
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 5);
  bottom = 0;
  clockinit = 5;
}
//--------------------меню установки порогов включения и выключения вентилятора радиатора--------
void coolfan_engine()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Radiator");
  lcd.setCursor(2, 1);
  lcd.print("FAN");
  lcd.setCursor(9, 0);
  lcd.print("OFF");
  lcd.setCursor(15, 0);
  lcd.print(char(6));
  lcd.setCursor(10, 1);
  lcd.print("ON");
  lcd.setCursor(15, 1);
  lcd.print(char(6));
  eeprom1 = EEPROM.read(eepcoolfanoff);

  eeprom2 = EEPROM.read(eepcoolfanon);
  lcd.setCursor(13, 0);
  lcd.print(eeprom1);

  lcd.setCursor(12, 1);
  if (eeprom2 < 100) lcd.print(char(1));
  lcd.print(eeprom2);
  do {

    bott_150ms();
    if (bottom == 32) coolfan_engine_off();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 6);
  bottom = 0;
  EEPROM.write(eepcoolfanoff, eeprom1);
  EEPROM.write(eepcoolfanon, eeprom2);
}
//-----------------------------------------------------------------------------
void coolfan_engine_off()
{
  bottom = 0;
  do
  {
    lcd.setCursor(13, 0);
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(13, 0);
    lcd.print(eeprom1);
    bott_500ms();

    if (bottom == 32) coolfan_engine_on();
    if (bottom == 31)
    {
      bottom = 0;
      eeprom1 = eeprom1 + 1;
      if (eeprom1 > 99) eeprom1 = 99;

    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom1 = eeprom1 - 1;
      if (eeprom1 < 70) eeprom1 = 70;
    }
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 6);
  bottom = 0;
  clockinit = 6;
}
//------------------------------------------------------------------------------
void coolfan_engine_on()
{
  bottom = 0;
  if ((eeprom1 + 1) >= eeprom2) eeprom2 = eeprom1 + 1;
  do
  {

    lcd.setCursor(12, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(12, 1);
    if (eeprom2 < 100) lcd.print(char(1));
    lcd.print(eeprom2);
    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      eeprom2 = eeprom2 + 1;
      if (eeprom2 > 105) eeprom2 = 105;

    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom2 = eeprom2 - 1;

      if (eeprom2 < 75) eeprom2 = 75;
      if ((eeprom1 + 1) >= eeprom2) eeprom2 = eeprom1 + 1;
    }
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 6);
  bottom = 0;
}

void heatfan_on()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Salon");
  lcd.setCursor(1, 1);
  lcd.print("FAN");
  lcd.setCursor (13, 0);
  lcd.print("ON");
  lcd.setCursor(13, 1);
  lcd.print("OFF");
  eepromi = EEPROM.read(eepheatfanon);
  do
  {
    if (eepromi == 1)
    {
      lcd.setCursor(12, 1);
      lcd.print(char(1));
      lcd.setCursor(12, 0);
      lcd.print(">");
    }

    if (eepromi == 0)
    {
      lcd.setCursor(12, 0);
      lcd.print(char(1));
      lcd.setCursor(12, 1);
      lcd.print(">");
    }



    bott_150ms();
    if (bottom == 32) set_heatfan_on();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 7);
  bottom = 0;

  EEPROM.write(eepheatfanon, eepromi);
}

void set_heatfan_on()
{
  bottom = 0;
  do
  {
    lcd.setCursor(12, 0);
    lcd.print(char(1));
    lcd.setCursor(12, 1);
    lcd.print(char(1));
    bott_500ms();
    if (eepromi == 1)
    {

      lcd.setCursor(12, 0);
      lcd.print(">");
    }
    if (eepromi == 0)
    {

      lcd.setCursor(12, 1);
      lcd.print(">");
    }
    bott_500ms();

    if (bottom == 31) eepromi = 0;
    if (bottom == 21) eepromi = 1;
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 7);
  bottom = 0;
  clockinit = 7;
}

void heatfan_level()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Salon");
  lcd.setCursor(1, 1);
  lcd.print("FAN");
  lcd.setCursor(9, 0);
  lcd.print("Car");
  lcd.setCursor(15, 0);
  lcd.print(char(6));
  lcd.setCursor(6, 1);
  lcd.print("Engine");
  lcd.setCursor(15, 1);
  lcd.print(char(6));
  eeprom1 = EEPROM.read(eepheatfan1);

  eeprom2 = EEPROM.read(eepheatfan2);
  lcd.setCursor(13, 0);
  if (eeprom1 < 10) lcd.print(char(1));
  lcd.print(eeprom1);
  lcd.setCursor(13, 1);
  lcd.print(eeprom2);
  do {


    bott_150ms();
    if (bottom == 32) heatfan_level_01();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 8);
  bottom = 0;
  EEPROM.write(eepheatfan1, eeprom1);
  EEPROM.write(eepheatfan2, eeprom2);
}

void heatfan_level_01()
{
  bottom = 0;
  do
  {
    lcd.setCursor(13, 0);
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(13, 0);
    if (eeprom1 < 10) lcd.print(char(1));
    lcd.print(eeprom1);
    bott_500ms();

    if (bottom == 32) heatfan_level_02();
    if (bottom == 31)
    {
      bottom = 0;
      eeprom1 = eeprom1 + 1;
      if (eeprom1 > 35) eeprom1 = 35;

    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom1 = eeprom1 - 1;
      if (eeprom1 < 0) eeprom1 = 0;
    }
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 8);
  bottom = 0;
  clockinit = 8;
}

void heatfan_level_02()
{
  bottom = 0;
  do
  {
    lcd.setCursor(13, 1);
    lcd.print(char(1));
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(13, 1);
    lcd.print(eeprom2);
    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      eeprom2 = eeprom2 + 1;
      if (eeprom2 > 90) eeprom2 = 90;

    }
    if (bottom == 21)
    {
      bottom = 0;
      eeprom2 = eeprom2 - 1;

      if (eeprom2 < 10) eeprom2 = 10;

    }
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
  } while (clockinit == 8);
  bottom = 0;
}

void backlight_level()
{
  bottom = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print ("Backlight LCD");
  lcd.setCursor(9, 1);
  lcd.print("%");

  eepromi = EEPROM.read(eepbacklight);
  lcd.setCursor(5, 1);
  if (eepromi < 100) lcd.print(char(1));
  if (eepromi < 10) lcd.print(char(1));
  lcd.print(eepromi);
  do
  {

    bott_150ms();
    if (bottom == 32) set_backlight_level();
    if (bottom == 31) clockinit = clockinit + 1;
    if (bottom == 21) clockinit = clockinit - 1;
    if (bottom == 22) clockinit = 55;
  } while (clockinit == 9);
  bottom = 0;

  EEPROM.write(eepbacklight, eepromi);
}

void set_backlight_level()
{
  bottom = 0;
  do
  {
    //  lcd.clear();
    lcd.setCursor(5, 1);
    lcd.print(char(1));
    lcd.setCursor(6, 1);
    lcd.print(char(1));
    lcd.setCursor(7, 1);
    lcd.print(char(1));
    bott_500ms();

    lcd.setCursor(5, 1);
    if (eepromi < 100) lcd.print(char(1));
    if (eepromi < 10) lcd.print(char(1));
    lcd.print(eepromi);
    bott_500ms();

    if (bottom == 31)
    {
      bottom = 0;
      eepromi = eepromi + 5;
      if (eepromi > 100) eepromi = 100;
    }
    if (bottom == 21)
    {
      bottom = 0;
      eepromi = eepromi - 5;
      if (eepromi <= 5) eepromi = 5;
    }
    if ((bottom == 22) || (bottom == 32)) clockinit = 55;
    analogWrite (backlight, eepromi);
  } while (clockinit == 9);
  bottom = 0;
  clockinit = 9;
}

//-------------одноразовая запись исходных данных по умолчанию в EEPROM если он пуст------------
void eeprominit()
{
  eepromi = EEPROM.read(eepautolightstat);
  if ((eepromi == 0xFF)) EEPROM.write(eepautolightstat, 1);

  eepromi = EEPROM.read(eepautolightoff);
  if (eepromi == 0xFF) EEPROM.write(eepautolightoff, 150);

  eepromi = EEPROM.read(eepautolighton);
  if (eepromi == 0xFF) EEPROM.write(eepautolighton, 200);

  eepromi = EEPROM.read(eepcoolfanstat);
  if ((eepromi == 0xFF)) EEPROM.write(eepcoolfanstat, 1);

  eepromi = EEPROM.read(eepcoolfanoff);
  if (eepromi == 0xFF) EEPROM.write(eepcoolfanoff, 93);

  eepromi = EEPROM.read(eepcoolfanon);
  if (eepromi == 0xFF) EEPROM.write(eepcoolfanon, 97);

  eepromi = EEPROM.read(eepheatfanon);
  if ((eepromi == 0xFF)) EEPROM.write(eepheatfanon, 1);

  eepromi = EEPROM.read(eepheatfan1);
  if (eepromi == 0xFF) EEPROM.write(eepheatfan1, 25);

  eepromi = EEPROM.read(eepheatfan2);
  if (eepromi == 0xFF) EEPROM.write(eepheatfan2, 70);

  eepromi = EEPROM.read(eepbacklight);
  if (eepromi == 0xFF) EEPROM.write(eepbacklight, 10);
}

void read_datetime()
{
  DateTime now = rtc.now();
  yeartime = now.year();
  monthtime = now.month();
  daytime = now.day();
  hourtime = now.hour();
  minutetime = now.minute();
  secondtime = now.second();
}
void bott_150ms()
{
  bottoms();
  delay(50);
  bottoms();
  delay(50);
  bottoms();
  delay(50);
}

void bott_500ms()
{
  for ( int ms = 0; ms < 8; ms++)
  {
    bottoms();
    delay(50);
  }
}

 

ЕвгенийП
ЕвгенийП аватар
Offline
Зарегистрирован: 25.05.2015

1952 строки. Вы реально думаете, что кто-то будет разбираться? Не, ну может быть, конечно, найдётся человек, которому решительно нечем заняться в выходной, и он захочет Вам помочь, но не знаю.

Мне кажется, Вам лучше либо обратиться к автору скетча, либо заказать эту работу (по разбирательству) в разделе "Ищу исполнителя".

JasKo
Offline
Зарегистрирован: 21.11.2015

строка 875 три кнопки через резисторы посажены на вход A1.

Ну а дальше сам, сам...

dominion
Offline
Зарегистрирован: 30.05.2016

Спасибо большое!!!