Blynk - Arduino Mega2560

Hi, I use Mega2560 with Blynk application,sometimes the system gets stuck, Blynk application shows “Device disconnected”. Need to press the reset button for the system to work again. Most often, the system freezes when the ''digitalInput (dig)" changes its state. How to solve this problem? maybe it is possible when the system gets stuck restarting it without pressing the Reset button? Thanks in advance

[code]


//#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "**************";
#define W5100_CS  10
#define SDCARD_CS 4
#define BLYNK_RED "#D3435C"
#define BLYNK_GREEN "#23C48E"


BlynkTimer timer;

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
RTC_DS1307 rtc;

 int SR = (A9);
 int MR = (A8);
 int NR = (A10);
 int MM = (A11);

 boolean dopen = true;
 boolean dclose = true;
  
 int inpu1 = 22;
 int inpu2 = 23;
 int inpu3 = 24;
 int inpu4 = 25;
 int inpu5 = 26;
 int inpu6 = 27;


                    //int bdoor = 0;



const int reverse = 30;
const int door = 31;
const int window = 32;
const int watering = 33;
const int wout = 34;







char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 100);
}

void setup () {
  Serial.begin(9600);
 
   pinMode(SDCARD_CS, OUTPUT); //blynk
  digitalWrite(SDCARD_CS, HIGH);
Blynk.begin(auth);
timer.setInterval(1000L, myTimerEvent);


    
    pinMode(reverse ,OUTPUT);
    pinMode(door ,OUTPUT);
    pinMode(window ,OUTPUT);
    pinMode(watering ,OUTPUT);
    pinMode(wout, OUTPUT);
    pinMode(inpu1,INPUT_PULLUP);
    pinMode(inpu2,INPUT_PULLUP);
    pinMode(inpu3,INPUT);
    pinMode(inpu4,INPUT);
    pinMode(inpu5, INPUT);
    pinMode(inpu6, INPUT);
   

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

    
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // 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(2020, 04, 28, 21, 23, 0));   // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS
  }
  delay(100);
  lcd.begin(16,2);

 
 rtc.adjust(DateTime(2020, 05, 3, 23, 30, 5)); // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS   

}



                  //BLYNK_WRITE(V4)

                  //{
                  //bdoor = param.asInt();

                  // if (param.asInt()== 1){
                  //bdoor == HIGH;
                  //  } else { bdoor == LOW;
                  // }

                  //}






void loop () {

    int dig = digitalRead(inpu1);
    int dig1 = digitalRead(inpu2);
    int dig2 = digitalRead(inpu3);
    int dig3 = digitalRead(inpu4);
    int dig4 = digitalRead(inpu5);
    int dig5 = digitalRead(inpu6);





Blynk.run();


if (dig==HIGH)                                               
{                                             
  Blynk.virtualWrite(V11,255);
  Blynk.setProperty(V11, "color", BLYNK_GREEN);
  Blynk.setProperty(V11, "label", "DOOR IS OPEN");
}


if (dig1==HIGH)                                             
  {
  Blynk.virtualWrite(V11,255);
  Blynk.setProperty(V11, "color", BLYNK_RED);
  Blynk.setProperty(V11, "label", "DOOR IS CLOSE");
 
}

if ((dig==LOW) && (dig1==LOW))                             
{
  Blynk.virtualWrite(V11,0);
  Blynk.setProperty(V11, "label", "RUNING");
}









if (dig2==HIGH)

{ 
  Blynk.virtualWrite(V12,255);
  Blynk.setProperty(V12, "color", BLYNK_GREEN);
  Blynk.setProperty(V12, "label", "WINDOW IS OPEN");
}


if (dig3==HIGH) {
  Blynk.virtualWrite(V12,255);
  Blynk.setProperty(V12, "color", BLYNK_RED);
  Blynk.setProperty(V12, "label", "WINDOW IS CLOSE");
 
}


if ((dig3==LOW) && (dig2==LOW))                           
{
  Blynk.virtualWrite(V12,0);
  Blynk.setProperty(V12, "label", "RUNING");
}



if (dig4==LOW)

{ 
  Blynk.virtualWrite(V13,255);
  Blynk.setProperty(V13, "color", BLYNK_GREEN);
  Blynk.setProperty(V13, "label", "WATERING INSIDE ON");
}

else
{ (dig4==HIGH);
  Blynk.virtualWrite(V13,0);
  Blynk.setProperty(V13, "label", "WATERING INSIDE OFF ");
}

if (dig5==LOW)

{ 
  Blynk.virtualWrite(V14,255);
  Blynk.setProperty(V14, "color", BLYNK_GREEN);
  Blynk.setProperty(V14, "label", "WATERING OUTSIDE ON");
}

else
{ (dig5==HIGH);
  Blynk.virtualWrite(V14,0);
  Blynk.setProperty(V14, "label", "WATERING OUTSIDE OFF");
}



if ( (dig == HIGH) && (dig2 ==  HIGH)) {
  digitalWrite(reverse, HIGH);

}
if ( (dig1 == HIGH) && (dig3 == HIGH)) { 

digitalWrite(reverse, LOW);
}



digitalWrite(wout, HIGH);






DateTime now = rtc.now();



    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    delay(200); //Print date and time every 1 sec
 
    

  
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print ('/');
  lcd.print(now.day(), DEC);

  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);

  
  
 int atidaryti = (now.hour() == 7 && now.minute() == 5 && now.second() == 5);
 int uzdaryti = (now.hour() == 22 && now.minute() == 21 && now.second() == 30 );

    
if ((dopen == true)||(dclose == true)) {         
  
 digitalWrite(door, LOW);
 digitalWrite(window, LOW);
 
}
 else {         
 
 digitalWrite(door, HIGH);
 digitalWrite(window, HIGH);

}
  

if (now.hour() >= 21 && now.minute() >= 49) {
  digitalWrite(wout, LOW);
}

if (now.hour() >= 21 && now.minute() >= 50 ) {
  digitalWrite(wout, HIGH);
}





int NR = analogRead(A10);  // Agurkai vidus
int SR = analogRead(A9);   // Ridikai, salotos
int MR = analogRead(A8);   // Laukas
int MM = analogRead(A11);  // Vandens lygis




 SR = map(SR, 650, 340, 0,100); // Ridikai, salotos
 SR = constrain(SR, 0, 100);

  MR = map(MR, 650, 340, 0,100); // Laukas
 MR = constrain(MR, 0, 100);

 NR = map(NR, 650, 340, 0,100); // Agurkai vidus
 NR = constrain(NR, 0, 100); 

  MM = map(MM, 86, 36, 0,100); // Agurkai vidus
 
 
                                                              //int vala= analogRead(A9);
                                                                 //SR = map(vala, 340, 650, 100, 0);
lcd.setCursor(12,1);
lcd.println(SR);
lcd.setCursor(15,1);
lcd.println('%');
Blynk.virtualWrite(V20, SR);  // blynk Ridikai, salotos



Blynk.virtualWrite(V21, MR); // Laukas
Blynk.virtualWrite(V22, NR); // Agurkai vidus
Blynk.virtualWrite(V23, MM); // Vandens lygis















}

[/code]

I have this problem too. It goes away after some minutes so I guess the connections wait on something then timeout. Since the count of connections is limited to 8 in W5500 if the count of occupied connection is reached, no new connection can be made.
I plan to try to set a larger connection timeout for the Ethernet or in Blynk library or both, to wait longer for successful connection and not abandon it early. Blynk calls stop() after it stops waiting for the connection to server, but stop() is negotiated in background with the peer before closing the connection and making it free. And Ethernet library doesn't support aborting of the connection.

_blynkEthernetClient.setConnectionTimeout(4000);

made it much better.

If you are using Blynk you have to clean up your loop. The only lines in your loop should be

Blynk.run();
timer.run();

The Blynk server relies on this clean loop to stay connected. If your loop is not as I have described it will throw you off as your loop will be flooding the server. There is plenty of examples and information on this on the Blynk forum. I suggest you have a look.