Servo + BH1750 arduino gets time out

Hello i have issue

Today i found solution why i was getting timeout when using BH1750 i solved it. It works fine. Then i wanted to implement servo motors but when i included servo.h i started getting timeout

I tried code in ofline first and it worked.

 #define BLYNK_TEMPLATE_ID "****"
 #define BLYNK_TEMPLATE_NAME "*****"
 #define BLYNK_AUTH_TOKEN "****"
 #define BLYNK_PRINT Serial
 #define EspSerial Serial
 #define ESP8266_BAUD 115200
 #define cas 10000
 #define red 7
 #define sound 5 
 
 #include <ESP8266_Lib.h>
 #include <BlynkSimpleShieldEsp8266.h>
 #include <BH1750.h>
 #include <Servo.h>
 
 unsigned long newtime = 0;
 int number = 0;
 int wire = 0;
 int stat;
 int last;
 int nove = 0;
 int OS = 0;
 int ZS = 120;
 int OD = 179;
 int ZD = 20;
 
 char auth[] = BLYNK_AUTH_TOKEN;
 char ssid[] = "*****";
 char pass[] = "****";

 BlynkTimer timer;
 ESP8266 wifi(&EspSerial);
 BH1750 lightMeter;
 Servo Door;
 Servo Silo;
 
 void sendData(void)
 {
   if(millis() > newtime + cas)
   {
       float lights = lightMeter.readLightLevel();
       
       Wire.begin();
       Wire.write(number);
       Wire.end();
       newtime = millis();
       if(lights > 1)
       {
         number = 1;
       }  
   }
 }
   void sendSensor()
   {
   float light = lightMeter.readLightLevel();
   float lastH = light;
   
     if(millis() > newtime + cas)
   {
       lightMeter.begin();
       Wire.begin();
       Wire.write(number);
       Wire.end();
       newtime = millis();
       
       if(light > 1)
       {
         number = 1;
       }  
   }
   
   Blynk.virtualWrite(V6, light);
  
  if(light <= lastH && light <= 0)
   {
     stat = 0; 
   }
   
   if(light >= lastH  && light >= 1) //den
   {
     stat = 1;
   }
   
   last = stat;
   
   if(stat == 1) //den
   {
     if(nove != last)
       {
       analogWrite(red, 255);
       delay(500);
       analogWrite(sound,80);
       delay(1000);
       analogWrite(sound,255);
       Silo.write(OS);
       delay(2000);
       Silo.write(ZS);
       nove = 1;
       }
     
     Door.write(179);
     delay(2000);
     analogWrite(red, 0);
     Serial.println("dzen");
   }
   
   if(stat == 0) //noc
 {
 Serial.println("cma");
 
 if(nove != last)
 {
   analogWrite(sound,80);
   delay(1000);
   analogWrite(sound,255);
   analogWrite(red, 255);
   delay(5000);
 }
 Door.write(ZD);
 delay(5000);
 analogWrite(red, 0);
 nove = 0; 
 }
}
 void setup()
 {
   Serial.begin(115200);
   EspSerial.begin(ESP8266_BAUD);
   delay(10);
   Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80); 
   timer.setInterval(2000L, sendSensor);
   Silo.attach(3);
   Door.attach(2);
   Door.write(ZD);
   Silo.write(ZS);
  }
 void loop()
 { 
   Blynk.run();
   timer.run();
 }

Where could be problem ?

Ps: i know that the code looks horible…

You are using the same serial port for your debug messages from Blynk, your serial print messages from your code, and for communication with your ESP-01.
You can’t do this, as it messes-up the communication between the ESP-01 and your board (presumably an Uno ?).

You should trad this for more info about why this isn’t possible, and possible ways to fix it (the best way is to throw away your current hardware and use an ESP32)…

In addition, your code has lots of blocking delay() commands, which aren’t compatible with Blynk, as they will cause timeouts.

One option is to use non-blocking BlynkTimer Timeout timers, but that would be rather tricky with your current code structure, as you’d need to use cascaded functions, which gets rather messy if you do it using lambda functions, so it might be better to restructure your code to use discreet functions that are called in sequence once the timeout timer has completed.
More info here…

Pete.