// Wiring:
//A0 - Volts (battery)
//A1 - Amps (battery)

//D4---LEFT
//D5---RIGHT
//D7---SELECT
 
//D3----PWM out RAZRYAD
//D2----RAZRAD

//D12---LCD_RST (1)
//D11---LCD_CE (2)
//D10---LCD_DC (3)
//D9----LCD_DIN (4)
//D8----LCD_CLK (5)
//3,3v--LCD_VCC (6)
//D6----LCD_LIGHT (7) PWM OUT LED
//------LCD_GND (8)

//// DS1302:  SDA pin   -> Arduino Digital 4
//            SCL pin   -> Arduino Digital 5
/// RTS1307   A4
//            A5

#define LCD_C     LOW
#define LCD_D     HIGH

#define PIN_RESET 12
#define PIN_SCE   11
#define PIN_DC    10
#define PIN_SDIN  9
#define PIN_SCLK  8
#define ONE_WIRE_BUS 2//       .
#define DS1307_I2C_ADDRESS 0x68
#define DS1307_RAM ramBuffer

#include <Wire.h>
#include "header.h";
#include <PWM.h>
#include <EEPROM2.h>
#include <DS1307.h>
#include <OneWire.h>//    OneWire.
#include <DallasTemperature.h>//   .
#include <LCD5110_Graph.h>
LCD5110 myGLCD(8,9,10,12,11);
extern uint8_t SmallFont[];

int32_t frequency = 1000; //frequency (in Hz)
DS1307 rtc(A4, A5);
Time t;
Time t2;

float batteryVolts = 0;
float lastBatteryVolts = 0;
float lastBatteryVolts2 = 0;
float batteryAmps = 0;
float lastBatteryAmps = 0;
float lastBatteryAmps2 = 0;
float batteryWatts = 0;
float lastBatteryWatts = 0;
float maxwatts = 0;
float maxpwmwatts = 0;
float batteryAmpsChZ = 0;
float batteryAmpsChR = 0;
float start = 0;

float Umenu = 0;
float tmp1 = 0;
float tmp2 = 0;
float tmp3 = 0;
float cicl = 0;
float Upodd = 0.05;
float Umax = 0.05;
float Umin = 0.05;
float Izar = 0.05;
float Iraz = 0.05;
float Umain = 0.05;
float sumwats = 0;
float floatbyte = 0;
float sensorValue = 0;
float men = 0;
float men2 = 0;
float tmen = 0;

float T1; // DallasTemperature1
float T2; //DallasTemperature
float T3 = 0; //  DallasTemperature
float T4 = 0; //

char tmp[25];
boolean highWatts = false;
int chartX = 0;
int chartY = 0;
boolean mpptOn = false;
const boolean initialise = true;
int barwatts = 0;

int stat = 0;
int a = 0;
int b = 0;
int c = 1;
int n = 1;
int m = 1;
int time = 0;
int sek = 0;
int sek2 = 0;
int has = 0;
int mi = 0;
int mi2 = 0;
int den = 1;
int mec = 1;
int god = 2010;
int adress = 0;
int b_type;
int b_type_n;
int b_type_c;
byte byte1 =0;
byte byte2 =0;
byte byte3 =0;
byte byte4 =0;

unsigned long currentTime;
unsigned long loopTime;
const int pin_LEFT = 4;       // pin 4 
const int pin_RIGHT = 5;       // pin 5 
const int pin_SELECT = 7;       // pin 7

OneWire oneWire(ONE_WIRE_BUS);//   OneWire   OneWire   .
DallasTemperature sensors(&oneWire);// ,   oneWire     Dallas Temperature.
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
void eepromWriteFloat(int address, float floatbyte)
{
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=floatbyte;

   rtc.poke(address  , u.b[0]);
   rtc.poke(address+1, u.b[1]);
   rtc.poke(address+2, u.b[2]);
   rtc.poke(address+3, u.b[3]);
}

float eepromReadFloat(int address)
  {
   union u_tag 
   {
     byte b[4];
     float floatbyte;
   } u;   
   u.b[0] = rtc.peek(address);
   u.b[1] = rtc.peek(address+1);
   u.b[2] = rtc.peek(address+2);
   u.b[3] = rtc.peek(address+3);
   return u.floatbyte;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
void LcdInitialise(void)
{
  pinMode(PIN_SCE, OUTPUT);
  pinMode(PIN_RESET, OUTPUT);
  pinMode(PIN_DC, OUTPUT);
  pinMode(PIN_SDIN, OUTPUT);
  pinMode(PIN_SCLK, OUTPUT);
  digitalWrite(PIN_RESET, LOW);
  digitalWrite(PIN_RESET, HIGH);
  LcdWrite(LCD_C, 0x21 );  // LCD Extended Commands.
  LcdWrite(LCD_C, 0xBC );  // Set LCD Vop (Contrast). 
  LcdWrite(LCD_C, 0x04 );  // Set Temp coefficent. //0x04
  LcdWrite(LCD_C, 0x14 );  // LCD bias mode 1:48. //0x13
  LcdWrite(LCD_C, 0x0C );  // LCD in normal mode.
  LcdWrite(LCD_C, 0x20 );
  LcdWrite(LCD_C, 0x0C );
}

void LcdClear()
{
  for (int i=0; i<504; i++) LcdWrite(LCD_D, 0x00);
}

void LcdWrite(byte dc, byte data)
{
  digitalWrite(PIN_DC, dc);
  digitalWrite(PIN_SCE, LOW);
  shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  digitalWrite(PIN_SCE, HIGH);
}

void LcdString(char *characters, int x, int y)
{
  LcdWrite(LCD_C, 0x80 | x);  // Column.
  LcdWrite(LCD_C, 0x40 | y);  // Row.
  while (*characters) LcdCharacter(*characters++);
}

void LcdCharacter(char character)
  {
  for (int index = 0; index < 5; index++) LcdWrite(LCD_D, ASCII[character - 0x20][index]);
  LcdWrite(LCD_D, 0x00);
  }

void LcdXY(int x, int y)
{
  LcdWrite(LCD_C, 0x80 | x);  // Column.
  LcdWrite(LCD_C, 0x40 | y);  // Row.
}

  void LcdPlot (int x, int y)
{
  LcdXY(x,5-(y/8)); //set display address for plot
  int bin=128;
  for (int q=0; q<y%8; q++) bin=bin/2; //calculate pixel position in byte
  LcdWrite(LCD_D, bin); //plot pixel
  float slope=float(47-y)/float(83-x);
  
  for (int j=x+2; j<84; j++) 
    {
    float dy=slope*float(j-x);
    int k=y+round(dy);
    LcdXY(j,5-(k/8)); //set display address for plot
    int bin=128;
    for (int q=0; q<k%8; q++) bin=bin/2; //calculate pixel position in byte
    LcdWrite(LCD_D, bin); //plot pixel
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
//////////////////////////////////////////////////////
{
    {
          {
          
          sensors.begin(); //  1  .
          }
    // Set the clock to run-mode
   rtc.halt(false);
   rtc.setSQWRate(SQW_RATE_1);
   rtc.enableSQW(true);
    }
    {
    unsigned counter; // 2      , 
 //   EEPROM:
   EEPROM_read(36, counter);
 if (counter = 0)
   {
     counter = 1;
     EEPROM_write(0, Umax);
     EEPROM_write(4, Umin);
     EEPROM_write(8, Upodd);
     EEPROM_write(12, Izar);
     EEPROM_write(16, Iraz);
     EEPROM_write(20, sumwats);
     EEPROM_write(24, batteryAmpsChZ);
     EEPROM_write(28, batteryAmpsChR);
     EEPROM_write(32, start);
     EEPROM_write(36, counter);
    
   }
 else
   {
   EEPROM_read(0, Umax);
   EEPROM_read(4, Umin);
   EEPROM_read(8, Upodd);
   EEPROM_read(12, Izar);
   EEPROM_read(16, Iraz);
   EEPROM_read(20, sumwats);
   EEPROM_read(24, batteryAmpsChZ);
   EEPROM_read(28, batteryAmpsChR);
   EEPROM_read(32, start);
    }
   }
   
 //  floatbyte = eepromReadFloat(12);
 //  sumwats = floatbyte;
    
//////////////////////////////////////////////
    {
    pinMode(4, INPUT);
    pinMode(5, INPUT);
    pinMode(7, INPUT);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
        
    LcdClear();
    myGLCD.InitLCD();
    myGLCD.setFont(SmallFont);
    myGLCD.clrScr();
    LcdString("B",31,1);
    LcdString("A",31,2);
    LcdString("R-",45,2);
    LcdString("AZ",70,3);
    LcdString("AR",70,4);
    LcdString("W",70,5);
      
    InitTimersSafe(); 
    bool success = SetPinFrequencySafe(3, frequency);
    }
    {
////////////timer///////////////////////
    currentTime = millis();
    loopTime = currentTime; 
    } 
    //sumwats = 0;
   }

void loop()  
{
    
 
  
batteryVolts = analogRead(A0) * 0.0293; //get the battery volts
batteryVolts = (batteryVolts + lastBatteryVolts) / 2; //average the volts
lastBatteryVolts = batteryVolts;
LcdString(dtostrf(lastBatteryVolts,5,2,&tmp[0]),0,1); //display the battery volts

batteryAmps = (analogRead(A1)-512)*0.0735; //get the panelAmps
if (batteryAmps < 0.01) { batteryAmps = -batteryAmps;}
//batteryAmps = 1000;
//batteryAmps = analogRead(A1);
batteryAmps = (batteryAmps + lastBatteryAmps) / 2; //average the panelAmps
//if (batteryAmps < 0.01) { batteryAmps = -batteryAmps;} //don't let the batteryAmps go below zero
lastBatteryAmps = batteryAmps;
LcdString(dtostrf(lastBatteryAmps,5,2,&tmp[1]),0,2); //display the batteryAmps


lastBatteryWatts = lastBatteryVolts * lastBatteryAmps; //calculate the watts
//if (lastBatteryWatts < 0.005) lastBatteryWatts = 0;
//    
 

t = rtc.getTime();
LcdString(rtc.getTimeStr(),15,0);
sek=t.sec;
mi=t.min;
has=t.hour;
 if (sek != sek2)
     {
       sek2=sek;
       if (start == 1) { sumwats = sumwats + (lastBatteryWatts / 3600);}
       if (start == 3) { sumwats = sumwats - (lastBatteryWatts / 3600);}
       if (start == 1) { batteryAmpsChZ = batteryAmpsChZ + (lastBatteryAmps / 3600); }
       if (start == 3) { batteryAmpsChR = batteryAmpsChR - (lastBatteryAmps / 3600); }
       //sumwats = lastBatteryWatts;
       floatbyte = sumwats;
       eepromWriteFloat(0, floatbyte);
       LcdString(dtostrf(batteryAmpsChZ,4,4,&tmp[3]),0,3);
       LcdString(dtostrf(batteryAmpsChR,4,4,&tmp[3]),0,4);
       LcdString(dtostrf(sumwats,4,3,&tmp[3]),0,5);
     }
 
///////////////////////////////////////////////////////////////////

{
  currentTime = millis();
  if(currentTime >= (loopTime + 5))  ////   5 (200 )
     { 
     // if (digitalRead(pin_LEFT) == LOW)  if (sensorValue != 250) {sensorValue++;} 
     // if (digitalRead(pin_RIGHT) == LOW)  if (sensorValue != 0) {sensorValue--;}  
      if (digitalRead(pin_SELECT) == LOW)
        {
          
page_1:    
          sensorValue = 0;  //ostanovka zaryada chtob wattu ne propuscal
          analogWrite(6, sensorValue); //ostanovka zaryada chtob wattu ne propuscal
          pwmWrite(3, sensorValue);
          a = 0;
          c = 4;    
          LcdClear();
          myGLCD.InitLCD();
          myGLCD.setFont(SmallFont);
          myGLCD.clrScr();
          myGLCD.print("START",10,8);
          myGLCD.print("STOP",10,16);
          myGLCD.print("START CICL",10,24);
          myGLCD.print("HOME",10,32);
          myGLCD.print("MENU",10,40);
 
            ////////////////////////////////////////////
          delay(200);
          while (a < 1)
              {
               if (c > 5){ c = 1;}
               myGLCD.print(">",0,c*8);
               myGLCD.print(rtc.getDateStr(),10,0);
         /////////////////////////////////////////////////////////////////
     

               if (digitalRead(pin_LEFT) == LOW) 
                   { 
                    myGLCD.print(" ",0,c*8);
                    c = c + 1;  
                   }
               myGLCD.print(">",0,c*8);
               if (digitalRead(pin_RIGHT) == LOW) 
                   { 
                   myGLCD.print(" ",0,c*8);
                   c = c - 1;
                   if (c <= 0){ c = 5;}  
                   }
               if (digitalRead(pin_SELECT) == LOW)
                   { 
                    if (c ==1) { start = 1; c = 4; sumwats = 0; goto endmenu;}
                    if (c ==2) { start = 0; c = 4; sumwats = 0; batteryAmpsChZ = 0;batteryAmpsChR = 0; goto endmenu;}
                    if (c ==3) { start = 3; c = 4; sumwats = 0; goto endmenu;}
                   a = a + 1;
                   b_type_c = c;
                   }
                
               delay(300);
               myGLCD.update();
               }
           if (c ==4) {goto endmenu;}
            
          
///MENU////////////////////////////////////////////////////////////////////
     if (c ==5)
        {
menu:
        a = 0;
     //   n = 0;    
        myGLCD.InitLCD();
        myGLCD.setFont(SmallFont);
        myGLCD.clrScr();
        myGLCD.print("MENU",10,0);
        myGLCD.print("CICL",10,8);
        myGLCD.print("TIME",10,16);
        myGLCD.print("BATTERY TYPE",10,24);
        myGLCD.print("AMPERS",10,32);
        myGLCD.print("EXIT",10,40);
        delay(200);
        while (a < 1)
            {
             if (m > 5){ m = 1;}
             myGLCD.print(">",0,m*8);
             if (digitalRead(pin_LEFT) == LOW) 
                 { 
                  myGLCD.print(" ",0,m*8);
                  m = m + 1;  
                 }
             myGLCD.print(">",0,m*8);
             if (digitalRead(pin_RIGHT) == LOW) 
                 { 
                 myGLCD.print(" ",0,m*8);
                 m = m - 1;
                 if (m <= 0){ m = 5;}  
                 }
             if (digitalRead(pin_SELECT) == LOW)
                 { 
                 a = a + 1;
                 b_type = m;
                 }
             delay(200);
             myGLCD.update();
             }
         if (m ==5) {goto page_1;}
 //CICL/////////////////////////////////////////////////////////////////// 
         if (m ==1) 
             {
             myGLCD.InitLCD();
             myGLCD.setFont(SmallFont);
             myGLCD.clrScr();
             myGLCD.print("CICL",23,0);
             delay(100);
             while (a < 2  )
                 {
                 if (digitalRead(pin_LEFT) == LOW)  if (cicl != 255) {cicl = cicl + 1;} 
                 if (digitalRead(pin_RIGHT) == LOW)  if (cicl != 0) {cicl = cicl - 1;}
                 myGLCD.printNumF(cicl,0,23,20);
                 if (digitalRead(pin_SELECT) == LOW)
                     { 
                     a = a + 1;
                     b_type = m;
                     }
                 delay(200);
                 myGLCD.update();
                 }

            goto  menu;
            }

////TIME//////////////////////////////////////////////////////////////////////////
         if (m ==2)   
            {
menu_3:
             int n = 4;
             LcdClear();
             myGLCD.InitLCD();
             myGLCD.setFont(SmallFont);
             myGLCD.clrScr();
             myGLCD.print("TIME",25,0);
             myGLCD.print("h",10,8);
             myGLCD.print("m",10,16);
             myGLCD.print("DATA",10,24);
             myGLCD.print("EXIT",10,32);
             delay(100);
             while (a < 2  ) 
                 {
                  myGLCD.printNumF(has,0,55,8);
                  myGLCD.printNumF(mi,0,55,16);
                 if (n > 4){ n = 1;}
                  myGLCD.print(">",0,n*8);
                  if (digitalRead(pin_LEFT) == LOW) 
                      { 
                       myGLCD.print(" ",0,n*8);
                       n = n + 1;  
                       }
                  if (n < 5){myGLCD.print(">",0,n*8);} // e 5                        
                  if (digitalRead(pin_RIGHT) == LOW) 
                       { 
                       myGLCD.print(" ",0,n*8);
                       n = n - 1;
                       if (n <= 0){ n = 4;}  
                       }
                       myGLCD.print(rtc.getTimeStr(),15,40);
                 //  myGLCD.printNumF(n,1,48,40);
                   if (digitalRead(pin_SELECT) == LOW)
                       { 
                         if (n ==4) {goto menu;}
                         if (n ==3)
menu_4:                       
             {
             int n = 4;
             LcdClear();
             myGLCD.InitLCD();
             myGLCD.setFont(SmallFont);
             myGLCD.clrScr();
             myGLCD.print("PAGE2",25,0);
             myGLCD.print("den",10,8);
             myGLCD.print("mec",10,16);
             myGLCD.print("god",10,24);
             myGLCD.print("PAGE1",10,32);
             delay(100);
             while (a < 2  ) 
                 {
                  myGLCD.printNumF(den,0,55,8);
                  myGLCD.printNumF(mec,0,55,16);
                  myGLCD.printNumF(god,0,55,24);
                 if (n > 4){ n = 1;}
                  myGLCD.print(">",0,n*8);
                  if (digitalRead(pin_LEFT) == LOW) 
                      { 
                       myGLCD.print(" ",0,n*8);
                       n = n + 1;  
                       }
                  if (n < 5){myGLCD.print(">",0,n*8);} // e 5                        
                  if (digitalRead(pin_RIGHT) == LOW) 
                       { 
                       myGLCD.print(" ",0,n*8);
                       n = n - 1;
                       if (n <= 0){ n = 4;}  
                       }
                       myGLCD.print(rtc.getDateStr(),15,40);
                   if (digitalRead(pin_SELECT) == LOW)
                       { 
                         if (n ==4) {goto menu_3;}
                            while (a < 2)
                              {
                                if (n ==1) {time=den;}
                                if (n ==2) {time=mec;}
                                if (n ==3) {time=god;}
                                myGLCD.print(" ",0,n*8);
                                myGLCD.print(">",48,n*8);
                                if (n ==1)  {if (digitalRead(pin_LEFT) == LOW) if  (time != 31) { time = time + 1; }}
                                if (n ==2)  {if (digitalRead(pin_LEFT) == LOW) if  (time != 12) { time = time + 1; }}
                                if (n ==3)  {if (digitalRead(pin_LEFT) == LOW) if  (time != 2050) { time = time + 1; }}
                                if (digitalRead(pin_RIGHT) == LOW) if (time != 1) { time = time - 1; }
                                if (n ==1) {den=time;}
                                if (n ==2) {mec=time;}
                                if (n ==3) {god=time;}
                                delay(200);
                                myGLCD.printNumF(time,0,55,n*8);
                                myGLCD.update();
                                if (digitalRead(pin_SELECT) == LOW) 
                                    {
                                      
                                      //rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
                                     // rtc.setTime( has, mi, 0);     // Set the time to 12:00:00 (24hr format)
                                      rtc.setDate(den, mec, god);   // Set the date to October 3th, 2010
                                      if (n <= 3) {goto menu_4;}
                                       a = a + 1;
                                       b_type_n = n;
                                    }
                               }
                       }        
                 delay(200);
                 myGLCD.update(); 
                 }
                 
              if (n ==4) 
              {
               goto menu_3;
              }
           }
/////////////////////////////////////////////////////////////////////   n   3 
                            while (a < 2)
                              {
                                if (n ==1) {time=has;}
                                if (n ==2) {time=mi;}
                                myGLCD.print(" ",0,n*8);
                                myGLCD.print(">",48,n*8);
                                if (n ==1)  {if (digitalRead(pin_LEFT) == LOW) if  (time != 23) { time = time + 1; }}
                                if (n ==2)  {if (digitalRead(pin_LEFT) == LOW) if  (time != 59) { time = time + 1; }}
                                if (digitalRead(pin_RIGHT) == LOW) if (time != 0) { time = time - 1; }
                                if (n ==1) {has=time;}
                                if (n ==2) {mi=time;}
                                delay(200);
                                myGLCD.printNumF(time,0,55,n*8);
                                myGLCD.update();
                                if (digitalRead(pin_SELECT) == LOW) 
                                    {
                                      //rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
                                      rtc.setTime( has, mi, 0);     // Set the time to 12:00:00 (24hr format)
                                     // rtc.setDate(27, 10, 2014);   // Set the date to October 3th, 2010
                                      if (n <= 2) {goto menu_3;}
                                       a = a + 1;
                                       b_type_n = n;
                                    }
                               }
                       }        
                 delay(200);
                 myGLCD.update(); 
                 }
              if (n ==4) 
              {
                goto menu;
               }
           } 
        
////Charging lead//////////////////////////////////////////////////////////////////////////////////////// 
         if (m ==3)   
            {
menu_1:    
             LcdClear();
             myGLCD.InitLCD();
             myGLCD.setFont(SmallFont);
             myGLCD.clrScr();
             myGLCD.print("Charging lead",3,0);
             myGLCD.print("U max",10,8);
             myGLCD.print("U podd",10,16);
             myGLCD.print("U min",10,24);
             myGLCD.print("EXIT",10,32);
             delay(100);
             while (a < 2  ) 
                 {
                  myGLCD.printNumF(Umax,2,55,8);
                  myGLCD.printNumF(Upodd,2,55,16);
                  myGLCD.printNumF(Umin,2,55,24);
                  if (n > 4){ n = 1;}
                  myGLCD.print(">",0,n*8);
                  if (digitalRead(pin_LEFT) == LOW) 
                      { 
                       myGLCD.print(" ",0,n*8);
                       n = n + 1;  
                       }
                  if (n < 5){myGLCD.print(">",0,n*8);} // e 5                        
                  if (digitalRead(pin_RIGHT) == LOW) 
                       { 
                       myGLCD.print(" ",0,n*8);
                       n = n - 1;
                       if (n <= 0){ n = 4;}  
                       }
                  // myGLCD.printNumF(n,1,48,40);
                   if (digitalRead(pin_SELECT) == LOW)
                       { 
                         if (n ==4) {goto menu;}
                            while (a < 2)
                              {
                                if (n ==1) {Umenu=Umax;}
                                if (n ==2) {Umenu=Upodd;}
                                if (n ==3) {Umenu=Umin;}
                                myGLCD.print(" ",0,n*8);
                                myGLCD.print(">",48,n*8);
                                if (digitalRead(pin_LEFT) == LOW) if (Umenu <= 30.10) { Umenu = Umenu + 0.05; }
                                if (digitalRead(pin_RIGHT) == LOW) if (Umenu >= 0.05) { Umenu = Umenu - 0.05; }
                                if (n ==1) {Umax=Umenu;}
                                if (n ==2) {Upodd=Umenu;}
                                if (n ==3) {Umin=Umenu;}
                                delay(200);
                                myGLCD.printNumF(Umenu,2,55,n*8);
                                myGLCD.update();
                                if (digitalRead(pin_SELECT) == LOW) 
                                    {
                                      if (n <= 3) {goto menu_1;}
                                       a = a + 1;
                                       b_type_n = n;
                                    }
                               }
                       }        
                 delay(200);
                 myGLCD.update(); 
                 }
            if (n ==4) {goto menu;}
            }
//Load//////////////////////////////////////////////////////////////////////////////////////////
         if (m ==4)   
            {
menu_2:              
             LcdClear();
             myGLCD.InitLCD();
             myGLCD.setFont(SmallFont);
             myGLCD.clrScr();
             myGLCD.print("AMPERS",25,0);
             myGLCD.print("Izar A",10,8);
             myGLCD.print("Iraz A",10,16);
             myGLCD.print("EXIT",10,24);
             delay(100);
             while (a < 2  ) 
                 {
                  myGLCD.printNumF(Izar,2,55,8);
                  myGLCD.printNumF(Iraz,2,55,16);
                  if (n > 3){ n = 1;}
                  myGLCD.print(">",0,n*8);
                  if (digitalRead(pin_LEFT) == LOW) 
                      { 
                       myGLCD.print(" ",0,n*8);
                       n = n + 1;  
                       }
                  if (n < 4){myGLCD.print(">",0,n*8);} // e 4                        
                  if (digitalRead(pin_RIGHT) == LOW) 
                       { 
                       myGLCD.print(" ",0,n*8);
                       n = n - 1;
                       if (n <= 0){ n = 3;}  
                       }
                   if (digitalRead(pin_SELECT) == LOW)
                       { 
                         if (n ==3) {goto menu;}
                            while (a < 2)
                              {
                                if (n ==1) {Umenu=Izar;}
                                if (n ==2) {Umenu=Iraz;}
                                myGLCD.print(" ",0,n*8);
                                myGLCD.print(">",48,n*8);
                                if (digitalRead(pin_LEFT) == LOW) if (Umenu <= 9.0) { Umenu = Umenu + 0.05; }
                                if (digitalRead(pin_RIGHT) == LOW) if (Umenu >= 0.05) { Umenu = Umenu - 0.05; }
                                if (n ==1) {Izar=Umenu;}
                                if (n ==2) {Iraz=Umenu;}
                                delay(200);
                                myGLCD.printNumF(Umenu,2,55,n*8);
                                myGLCD.update();
                                if (digitalRead(pin_SELECT) == LOW) 
                                    {
                                      if (n <= 2) {goto menu_2;}
                                       a = a + 1;
                                       b_type_n = n;
                                    }
                               }
                       }        
                 delay(200);
                 myGLCD.update(); 
                 }
            if (n ==4) {goto menu;}
            }
        }
////////////////////////////////////////////////////////////////////////////////////////////
endmenu:
       EEPROM_write(0, Umax);
       EEPROM_write(4, Umin);
       EEPROM_write(8, Upodd);
       EEPROM_write(12, Izar);
       EEPROM_write(16, Iraz);
        LcdClear();
        myGLCD.InitLCD();
        myGLCD.setFont(SmallFont);
        myGLCD.clrScr();
        LcdString("B",31,1);
        LcdString("A",31,2);
        LcdString("R-",45,2);
        LcdString("AZ",70,3);
        LcdString("AR",70,4);
        LcdString("W",70,5);
        }
    //panelAmps = sensorValue; ////test
    loopTime = currentTime;
    }
    }
///////////////////////////////////////////////////////////////////////////////////cicl
{
  
  //////////////////////////////zaryad
   if (start == 1)
   {
      if (lastBatteryAmps <= Izar -0.05) { if (lastBatteryVolts <= Umax - 0.05) { sensorValue = sensorValue+1; }}   
      if (lastBatteryAmps >= Izar + 0.1 ) { if (sensorValue >= 1){ sensorValue = sensorValue-1;} }     
      if (lastBatteryVolts >= Umax -0.05 )
            {
             
            if (sensorValue >= 1) {  sensorValue = sensorValue-1; } 
         if (lastBatteryAmps <= 0.05) { start = 0; sensorValue = 0; }   
           }
       
         
     // sensorValue = 0;   
       //pwmWrite(3, sensorValue); //255
       if (batteryVolts >= Umax) { start = 0; sensorValue = 0; } //{ if (lastBatteryAmps <= 0.05) { start = 0; }} 
       
     //  int PWM_time = sensorValue;
       
       analogWrite(6, sensorValue);
       
     //  for (int k=0 ; k<PWM_time; ) PORTB=B00000001;
     //  for (int k=0 ; k<256-PWM_time; ) PORTB=B00000000;
       
    }
    //////////////////////////////razaryad
   if (start == 3)
   {
      if (lastBatteryAmps <= Iraz - 0.05 ) { if (lastBatteryVolts >= Umin + 0.05) { sensorValue = sensorValue+1; }}    
      if (lastBatteryAmps >= Iraz + 0.1 ) { if (sensorValue >= 1){ sensorValue = sensorValue-1;} }     
      if (lastBatteryVolts <= Umin + 0.05 )
            {
             
            if (sensorValue >= 1) {  sensorValue = sensorValue-1; }
            if (lastBatteryAmps <= 0.05) { start = 1; sumwats = 0; sensorValue = 0; }    
           }
       
     
       if (batteryVolts <= Umin ) { start = 1; sumwats = 0; sensorValue = 0; } //{ if (lastBatteryAmps <= 0.05) { start = 0; }} 
       pwmWrite(3, sensorValue);
       
    }
    
    
}

  
  
///////////////////////////////////////////////////////////////////////////////////

{
    { 
    if (T3 >= 150)//    
      {
      sensors.requestTemperatures(); //     .
      float T1=sensors.getTempCByIndex(0);
      LcdString("T1=",45,1); //  1  
      LcdString(dtostrf(T1,4,2,&tmp[1]),60,1);        
      T3 = 0;
      }
    else
      {
      T3 = (T3 + 1);
      }
    }
    if (sensors.getTempCByIndex(0) >= 60)
      {
       sensorValue = 0;  //ostanovka zaryada chtob wattu ne propuscal
       analogWrite(6, sensorValue); //ostanovka zaryada chtob wattu ne propuscal
       pwmWrite(3, sensorValue);
      }
      
  }
    
    if (start == 0) { LcdString("stop",60,2);}
    if (start == 1) { LcdString("zar",60,2);}
    if (start == 3) { LcdString("raz",60,2);}
    //LcdString(dtostrf(T1,4,2,&tmp[1]),60,2);
    
     EEPROM_write(20, sumwats);
     EEPROM_write(24, batteryAmpsChZ);
     EEPROM_write(28, batteryAmpsChR);
     EEPROM_write(32, start);
/////////////////////////////////////////////////////////////  
  delay(25);
}