bool _tim5I = 0;
bool _tim5O = 0;
unsigned long _tim5P = 0UL;
bool _tim1I = 0;
bool _tim1O = 0;
unsigned long _tim1P = 0UL;
bool _tim11I = 0;
bool _tim11O = 0;
unsigned long _tim11P = 0UL;
bool _tim7I = 0;
bool _tim7O = 0;
unsigned long _tim7P = 0UL;
bool _tim2I = 0;
bool _tim2O = 0;
unsigned long _tim2P = 0UL;
bool _tim12I = 0;
bool _tim12O = 0;
unsigned long _tim12P = 0UL;
bool _bounseInputD18S = 0;
bool _bounseInputD18O = 0;
unsigned long _bounseInputD18P = 0UL;
bool _tim3I = 0;
bool _tim3O = 0;
unsigned long _tim3P = 0UL;
bool _tim8I = 0;
bool _tim8O = 0;
unsigned long _tim8P = 0UL;
bool _gen1I = 0;
bool _gen1O = 0;
unsigned long _gen1P = 0UL;
bool _tim10I = 0;
bool _tim10O = 0;
unsigned long _tim10P = 0UL;
bool _tim9I = 0;
bool _tim9O = 0;
unsigned long _tim9P = 0UL;
bool _gen2I = 0;
bool _gen2O = 0;
unsigned long _gen2P = 0UL;
bool _gen3I = 0;
bool _gen3O = 0;
unsigned long _gen3P = 0UL;
bool _tim4I = 0;
bool _tim4O = 0;
unsigned long _tim4P = 0UL;
bool _tim6I = 0;
bool _tim6O = 0;
unsigned long _tim6P = 0UL;
unsigned long _SCT_2 = 0UL;
unsigned long _SCT_2IC = 0UL;
int _sCHCV = 0;
void setup()
{
    pinMode(18, INPUT_PULLUP);
    pinMode(1, OUTPUT);
    digitalWrite(1, 0);
    pinMode(3, OUTPUT);
    digitalWrite(3, 1);
    pinMode(4, OUTPUT);
    digitalWrite(4, 0);
    pinMode(5, OUTPUT);
    digitalWrite(5, 1);
    pinMode(6, OUTPUT);
    digitalWrite(6, 1);
    pinMode(7, OUTPUT);
    digitalWrite(7, 1);
    pinMode(8, OUTPUT);
    digitalWrite(8, 1);
    pinMode(9, OUTPUT);
    digitalWrite(9, 1);
    pinMode(10, OUTPUT);
    digitalWrite(10, 0);
    pinMode(11, OUTPUT);
    digitalWrite(11, 0);
    pinMode(12, OUTPUT);
    digitalWrite(12, 0);
    _bounseInputD18O =  digitalRead(18);
    pinMode(2, INPUT_PULLUP);
    TCCR2A = (1<<WGM21);
    TCCR2B = (1<<CS21);
    OCR2A = 199;
    TIMSK2 = (1<<OCIE2A);
    sei ();
    attachInterrupt(digitalPinToInterrupt (2), _SCT_2coutFunction, FALLING);
}
void loop()
{
    bool  _bounceInputTmpD18 =  (digitalRead (18));
    if (_bounseInputD18S)
    {
        if (millis() >= (_bounseInputD18P + 40))
        {
            _bounseInputD18O= _bounceInputTmpD18;
            _bounseInputD18S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD18 != _bounseInputD18O)
        {
            _bounseInputD18S=1;
            _bounseInputD18P = millis();
        }
    }
    //Плата:1
    if(((analogRead (0))) > (700)) 
    {
        _tim4O = 1;
        _tim4I = 1;
    }
     else 
    {
         if(_tim4I) 
        {
            _tim4I = 0;
            _tim4P = millis();
        }
         else 
        {
             if (_tim4O) 
            {
                if (_isTimer(_tim4P, 2000)) _tim4O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (700)) && (!(_bounseInputD18O)))) 
    {
        _tim11O = 1;
        _tim11I = 1;
    }
     else 
    {
         if(_tim11I) 
        {
            _tim11I = 0;
            _tim11P = millis();
        }
         else 
        {
             if (_tim11O) 
            {
                if (_isTimer(_tim11P, 10000)) _tim11O = 0;
            }
        }
    }
    digitalWrite(9, ((_tim4O) || (_tim11O)));
    if(((analogRead (0))) > (400)) 
    {
        _tim3O = 1;
        _tim3I = 1;
    }
     else 
    {
         if(_tim3I) 
        {
            _tim3I = 0;
            _tim3P = millis();
        }
         else 
        {
             if (_tim3O) 
            {
                if (_isTimer(_tim3P, 2000)) _tim3O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (400)) && (!(_bounseInputD18O)))) 
    {
        _tim9O = 1;
        _tim9I = 1;
    }
     else 
    {
         if(_tim9I) 
        {
            _tim9I = 0;
            _tim9P = millis();
        }
         else 
        {
             if (_tim9O) 
            {
                if (_isTimer(_tim9P, 10000)) _tim9O = 0;
            }
        }
    }
    digitalWrite(8, ((_tim3O) || (_tim9O)));
    if(((analogRead (0))) > (300)) 
    {
        _tim2O = 1;
        _tim2I = 1;
    }
     else 
    {
         if(_tim2I) 
        {
            _tim2I = 0;
            _tim2P = millis();
        }
         else 
        {
             if (_tim2O) 
            {
                if (_isTimer(_tim2P, 2000)) _tim2O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (300)) && (!(_bounseInputD18O)))) 
    {
        _tim8O = 1;
        _tim8I = 1;
    }
     else 
    {
         if(_tim8I) 
        {
            _tim8I = 0;
            _tim8P = millis();
        }
         else 
        {
             if (_tim8O) 
            {
                if (_isTimer(_tim8P, 10000)) _tim8O = 0;
            }
        }
    }
    digitalWrite(7, ((_tim2O) || (_tim8O)));
    if(((analogRead (0))) > (150)) 
    {
        _tim1O = 1;
        _tim1I = 1;
    }
     else 
    {
         if(_tim1I) 
        {
            _tim1I = 0;
            _tim1P = millis();
        }
         else 
        {
             if (_tim1O) 
            {
                if (_isTimer(_tim1P, 2000)) _tim1O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (150)) && (!(_bounseInputD18O)))) 
    {
        _tim7O = 1;
        _tim7I = 1;
    }
     else 
    {
         if(_tim7I) 
        {
            _tim7I = 0;
            _tim7P = millis();
        }
         else 
        {
             if (_tim7O) 
            {
                if (_isTimer(_tim7P, 10000)) _tim7O = 0;
            }
        }
    }
    digitalWrite(6, ((_tim1O) || (_tim7O)));
    if(((analogRead (0))) > (70)) 
    {
        _tim10O = 1;
        _tim10I = 1;
    }
     else 
    {
         if(_tim10I) 
        {
            _tim10I = 0;
            _tim10P = millis();
        }
         else 
        {
             if (_tim10O) 
            {
                if (_isTimer(_tim10P, 2000)) _tim10O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (70)) && (!(_bounseInputD18O)))) 
    {
        _tim6O = 1;
        _tim6I = 1;
    }
     else 
    {
         if(_tim6I) 
        {
            _tim6I = 0;
            _tim6P = millis();
        }
         else 
        {
             if (_tim6O) 
            {
                if (_isTimer(_tim6P, 10000)) _tim6O = 0;
            }
        }
    }
    digitalWrite(5, ((_tim10O) || (_tim6O)));
    if(((analogRead (0))) > (900)) 
    {
        _tim5O = 1;
        _tim5I = 1;
    }
     else 
    {
         if(_tim5I) 
        {
            _tim5I = 0;
            _tim5P = millis();
        }
         else 
        {
             if (_tim5O) 
            {
                if (_isTimer(_tim5P, 2000)) _tim5O = 0;
            }
        }
    }
    if(((((analogRead (0))) > (900)) && (!(_bounseInputD18O)))) 
    {
        _tim12O = 1;
        _tim12I = 1;
    }
     else 
    {
         if(_tim12I) 
        {
            _tim12I = 0;
            _tim12P = millis();
        }
         else 
        {
             if (_tim12O) 
            {
                if (_isTimer(_tim12P, 10000)) _tim12O = 0;
            }
        }
    }
    digitalWrite(10, ((_tim5O) || (_tim12O)));
    digitalWrite(12, ((analogRead (3))) > (100));
    if (((analogRead (5))) < (800)) 
    {
        if (! _gen2I) 
        {
            _gen2I = 1;
            _gen2O = 1;
            _gen2P =  millis();
        }
    }
     else 
    {
        _gen2I = 0 ;
        _gen2O= 0;
    }
     if (_gen2I) 
    {
         if (_gen2O) 
        {
             if (_isTimer(_gen2P , 300)) 
            {
                 _gen2P = millis();
                _gen2O = 0;
            }
        }
         else  
        {
             if (_isTimer(_gen2P , 700)) 
            {
                  _gen2P = millis();
                _gen2O = 1;
            }
        }
    }
    digitalWrite(11, _gen2O);
    digitalWrite(1, ((((!((_SCT_2) >= (15))) && ((_SCT_2) >= (10)))) || (((analogRead (1))) < ((analogRead (2))))));
    if (((((!((_SCT_2) >= (15))) && ((_SCT_2) >= (10)))) || (((analogRead (1))) < ((analogRead (2)))))) 
    {
         if (! _gen3I) 
        {
            _gen3I = 1;
            _gen3O = 1;
            _gen3P = micros();
        }
    }
     else 
    {
        _gen3I = 0 ;
        _gen3O= 0;
    }
    if (_gen3I) 
    {
          if (_isTimerMicros (_gen3P , 250)) 
        {
             _gen3P = micros();
            _gen3O = ! _gen3O;
        }
    }
    digitalWrite(3, _gen3O);
    if (((((!((_SCT_2) >= (15))) && ((_SCT_2) >= (10)))) || (((analogRead (1))) < ((analogRead (2)))))) 
    {
         if (! _gen1I) 
        {
            _gen1I = 1;
            _gen1O = 1;
            _gen1P = micros();
        }
    }
     else 
    {
        _gen1I = 0 ;
        _gen1O= 0;
    }
    if (_gen1I) 
    {
          if (_isTimerMicros (_gen1P , 250)) 
        {
             _gen1P = micros();
            _gen1O = ! _gen1O;
        }
    }
    digitalWrite(4, !(_gen1O));
}
bool _isTimer(unsigned long startTime, unsigned long period)
{
    unsigned long currentTime;
    currentTime = millis();
    if (currentTime>= startTime) 
    {
        return (currentTime>=(startTime + period));
    }
     else 
    {
        return (currentTime >=(4294967295-startTime+period));
    }
}
bool _isTimerMicros(unsigned long startTime, unsigned long period)
{
    unsigned long currentTime;
    currentTime = micros();
    if (currentTime>= startTime) 
    {
        return (currentTime>=(startTime + period));
    }
     else 
    {
        return (currentTime >=(4294967295-startTime+period));
    }
}
void  _SCT_2coutFunction()
{
     _SCT_2IC++;
}
ISR (TIMER2_COMPA_vect)
{
    if(_sCHCV==2500) 
    {
        _sCHCV = 0;
        _SCT_2 = _SCT_2IC;
        _SCT_2IC = 0;
    }
     else 
    {
        _sCHCV++;
    }
}
