BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Disconnection Caused by For Cycle


#20

Unfortunately anything “arduino shield”-like is beyond the scope of my expertise since I wouldn’t for the life of me toy with it over ESP8266 :stuck_out_tongue:

But in regards to the above quoted bit, that for loop will call Blynk.run way to often and fast (dont know your NUM_LEDS size) since the second operation is a quick and easy one. ( leds[i] = CHSV(32, 255, 255); )

It most likely wont hurt but I think its bad practice since it makes plenty of unnessecary calls.

I would change it to the following

for (int i = 0; i <= NUM_LEDS; i++)
      {
        if ( i % 10 = 0)
            Blynk.run();
        leds[i] = CHSV(32, 255, 255);
      }

so it only calls Blynk.run every 10th iterration,
@ 10
@ 20
@ 30
@ 40
and so on…

Or one of your choosing. Depending on the size of NUM_LEDS.
Also sprinkle your code with some more Serial.println in order to debugg when and where it failed.
Perhaps by checking if blynk is connected and just post the result and millis


#21

My NUM_LEDS is 100. Could be a nice idea to call Blyn.run() 10 times in a For cycle or I need to call it less than 10 times?

Now I will try putting some Serial.println() as you suggest in order to find bugs.

I am pretty sure that Blynk is connected because I am using the Notification Widget and it warn me when device goes offline.


#22

Not knowing the time-span of your for-loop itteration it is hard to say what is the proper amount of Blynk.runs, basically it should boil down to:

TimeSpanOfAnyFunctionOrLoop < Heartbeat timeout - 2 second (guessing here to be on some sort of safe-side)
TimeSpanOfAnyFunctionOrLoop < Hardware/Software watchdog timeout - 1 second (This should be less sensitive since its MCU related and not connection based)

Hence using millis should help you see how fast/slow your code is operating and where it would be wise to feed that heartbeat/watchdogs


#23

I’ve tried this solution but timeout problem still persist. I am also trying debug using Serial.println() but i cannot see where the real problem is because when timeout occur also Serial.println() stop running also in the while of the printing results.
If you want I can upload the entire sketch if this can help.


#24

Hey guys i have some news.

I rewrite the code of the previously posted function accendiLedCamera() with this one called modalitaNotturna() always by using a timer.setInterval() that solved the problem caused by last if condition. This new function(just renamed and modified) is the following

 void modalitaNotturna()
{
  modgiorno = digitalRead(MODGIORNO);
  modnotte = digitalRead(MODNOTTE);
  bottoneeffetti = digitalRead(MODEFFETTI);
  pirCamera = digitalRead(PIRCAMERAPIN);
  fotoresval = analogRead(FRPIN);
  frvalperc = map(fotoresval, 0, 1000, 0, 100);
  if (modnotte == HIGH && modgiorno == LOW && bottoneeffetti == LOW && bottonestriscialedsala == LOW )
  {
    Serial.println("MODALITA NOTTURNA ATTIVATA");
    if (frvalperc < 50  && pirCamera == HIGH && contnotturno == 1)
    {
      Serial.println("ACCENDO LED CAMERA MOVIMENTO RILEVATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        Serial.println(i);
        leds[i] = CRGB::Gold;
      }
      FastLED.show();
      Serial.println("USCITO");
      contnotturno = 0;
    }
    else if ((pirCamera == LOW && contnotturno == 0) || (frvalperc >= 50 && contnotturno == 0))
    {
      Serial.println("SPENGO LED CAMERA MOVIMENTO TERMINATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        leds[i] = CRGB::Black;
      }
      contnotturno = 1;
      FastLED.show();
      FastLED.clear();
    }
  }
}

This function works perfectly with other function always called by using timers.The purpose of this function is to lights on a digital strip led (WS2811) when a movement is detected and to lights off when no movement is detected.

So finally I solve the previous problem but now I have another problem that is driving me crazy.
When I call this specific funciton named modalitaEffettiLuminosi (called always using timers) the first For cycle of the function modalitaNotturna() sometimes stop(timeout occurs) when the “i” counter is on 85 and sometimes the “i” counter reach 100 that is the condition to exit from the For cycle but timeout occurs again.
I dont know if the problem is in the code but I scan the code so much for hours and hours and I was not able to find a bug or a proper solution. I believe that probably the problem is in Blynk or something related to that.

The function that is causing me timeout problem is the following:

void modalitaEffettiLuminosi()
{
  bottoneeffetti = digitalRead(MODEFFETTI);
  if (bottoneeffetti == HIGH && conteffettiluminosi == 1)
  {
    Serial.println("MODALITA EFFETTI LUMINOSI");
    if (effetto == 1)
    {
      FastLED.clear();
      //effettoMultiRGB();
    }
    if (effetto == 2)
    {
      FastLED.clear();
      effettoGradiente3Colori();
    }
    if (effetto == 3)
    {
      FastLED.clear();
      //effettoScontroaMetaLungoRGB();
    }
    if (effetto == 4)
    {
      FastLED.clear();
     // effettoScontroaMetaRGB();
    }
    if (effetto == 5)
    {
      FastLED.clear();
      //effettoSerpenteMonocolore();
    }
    if (effetto == 6)
    {
      FastLED.clear();
      //effettoSerpenteRGB();
    }
    if (effetto == 7)
    {
      FastLED.clear();
     // effettoSerpenteMisto();
    }
    conteffettiluminosi = 0;
  }
  else if (bottoneeffetti == LOW && conteffettiluminosi == 0)
  {
    Serial.println("SPENGO MODALITA EFFETTI LUMINOSI");
    for (int i = 0; i <= 100; i++)
    {
      leds[i].CRGB::Black;
    }
    conteffettiluminosi = 1;
    FastLED.show();
    FastLED.clear();
  }
}

I just comment the line of the various effect to avoid other coding problems. I can confirm that this function is causing me the timeout problem because if i comment the timer, where this function is declared in the void setup(), just putting a double slash in //timer.setInterval(1000L,modalitaEffettiLuminosi), timeout problem disappear.

Can someone please help me?


#25

Depending on conditions, you have this loop running every second, and depending on how long the if-loop actually takes, this could be your timeout issue.

You have print statements, so you should be able to determine if that single long if-loop or the group of nested if-loops is the culprit.

Either way it is a code and timing issue, not a Blynk bug.

PS after posting code, please format it accordingly, as per the Welcome Topic and here…

Blynk - FTFC


#26

Thank you so much for your suggestions.

I’ve tried to monitoring the time that the For cycle need from start to finish and it is 450ms. I have also seen that the problem is not the For cycle but this specific part of code

if (bottoneeffetti == HIGH && conteffettiluminosi == 1)
  {
    Serial.println("MODALITA EFFETTI LUMINOSI");
    if (effetto == 1)
    {
      FastLED.clear();
      //effettoMultiRGB();
    }
    else if (effetto == 2)
    {
      FastLED.clear();
      effettoGradiente3Colori();
    }
    else if (effetto == 3)
    {
      FastLED.clear();
      //effettoScontroaMetaLungoRGB();
    }
    else if (effetto == 4)
    {
      FastLED.clear();
     // effettoScontroaMetaRGB();
    }
    else if (effetto == 5)
    {
      FastLED.clear();
      //effettoSerpenteMonocolore();
    }
    else if (effetto == 6)
    {
      FastLED.clear();
      //effettoSerpenteRGB();
    }
    else if (effetto == 7)
    {
      FastLED.clear();
     // effettoSerpenteMisto();
    }
    
    conteffettiluminosi = 0;
  }

Infact if I try to comment this part of code and keep the rest of code the same I don’t have more timeout.

When timeout occurs the function placed in the group of nested if-loops are commented as I posted so the problem is only thegroup of nested if-loops.

I have also tried to put a Serial.println() inside every single if-loop but I never had a reply as I expect. I give you this information only to specify that the problem is given by a non-used(at the moment) group of nested if-loops.

Why is this causing me timeout problem?

What solutions do you suggest?


#27

what mcu are you using?
if esp, than what is the core version?

what is the reason for the 7 “if” conditions, if you do the same thing in all of them? FastLED.clear();

also, if you use esp, and if the problem persists with the 2.4 esp core, try to add yield() into the if statements. as you mentioned, the condition takes 450 ms to complete. maybe this is too long, without calling delay(1) or yield() for the esp.


#28

I am using an Arduino Mega 2560 with an Ethernet shield W5100. So itàs not an ESP.

The reason of 7 if condition is because I am using a Digital Led Strip in this particular case a WS2811 Strip Led and with every single if condition I can control a single different digital effect.

I am using a Menu Widget to choose the proper if-condition so the proper effect that I want in that moment.


#29

i see. than the tips wih the yield or delay are not walid.

you could put a single blynk.run after the if segment, maybe that will help.


#30

Thanks for your advice I will try it now and I will let you know if this work. :grinning:


#31

To apply your advice I had to modify the code in the following way

if (bottoneeffetti == HIGH)
  {
    Serial.println("MODALITA EFFETTI LUMINOSI");
    if (effetto == 1)
    {
      effettoMultiRGB();
    }
    Blynk.run();
     if (effetto == 2)
    {
      effettoGradiente3Colori();
    }
    Blynk.run();
     if (effetto == 3)
    {
      effettoScontroaMetaLungoRGB();
    }
    Blynk.run();
     if (effetto == 4)
    {
      effettoScontroaMetaRGB();
    }
    Blynk.run();
     if (effetto == 5)
    {
      effettoSerpenteMonocolore();
    }
    Blynk.run();
     if (effetto == 6)
    {
      effettoSerpenteRGB();
    }
    Blynk.run();
     if (effetto == 7)
    {
      effettoSerpenteMisto();
    }
    Blynk.run();
    conteffettiluminosi = 0;
  }

By the way the problem still persist. I want also to specify that this specific part of code is never running because the condition

if (bottoneeffetti == HIGH)

is never true.

I want also to specify that if I comment the entire group of nested if-condition I don’t have more timeout disconnection.


#32

no, i mean to put a blynk.run() AFTER the whole if segment, ONLY here:

but i do not understand, if the condition is always false, you still have the timeouts?
you wrote that if you comment out the entire group of nested if-condition, you do not have timeouts. but if it never gets true, it never gets executed, it is nearly the same as it would be commented out. isn’t it?

edit:

may i see a serial output with those timeouts?


#33

So I have modified the sketch as follow

  if (bottoneeffetti == HIGH)
  {
    Serial.println("MODALITA EFFETTI LUMINOSI");
    if (effetto == 1)
    {
      //effettoMultiRGB();
    }
     if (effetto == 2)
    {
      effettoGradiente3Colori();
    }
     if (effetto == 3)
    {
      effettoScontroaMetaLungoRGB();
    }
     if (effetto == 4)
    {
      effettoScontroaMetaRGB();
    }
     if (effetto == 5)
    {
      effettoSerpenteMonocolore();
    }
     if (effetto == 6)
    {
      effettoSerpenteRGB();
    }
     if (effetto == 7)
    {
      effettoSerpenteMisto();
    }
    conteffettiluminosi = 0;
  }
  Blynk.run();

By the way the problem still remain.

I supposed the same but I’ve seen that the behavior is not the same.

In particular this timeout occur when my pirCamera = HIGH so when this if-condition is satisfied

void modalitaNotturna()
{
  modgiorno = digitalRead(MODGIORNO);
  modnotte = digitalRead(MODNOTTE);
  bottoneeffetti = digitalRead(MODEFFETTI);
  pirCamera = digitalRead(PIRCAMERAPIN);
  fotoresval = analogRead(FRPIN);
  frvalperc = map(fotoresval, 0, 1000, 0, 100);
  if (modnotte == HIGH && modgiorno == LOW && bottoneeffetti == LOW && bottonestriscialedsala == LOW )
  {
    Serial.println("MODALITA NOTTURNA ATTIVATA");
    if (frvalperc < 50  && pirCamera == HIGH && contnotturno == 1)
    {
      unsigned long tempoinizio = millis();
      Serial.println("ACCENDO LED CAMERA MOVIMENTO RILEVATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        Serial.print("i = ");
        Serial.println(i);
        leds[i] = CRGB::Gold;
      }
      unsigned long tempofine = millis();
      Serial.print("Tempo uscita For Mod Notte = ");
      Serial.println(tempofine - tempoinizio);
      FastLED.show();
      Serial.println("USCITO");
      contnotturno = 0;
    }
    else if ((pirCamera == LOW && contnotturno == 0) || (frvalperc >= 50 && contnotturno == 0))
    {
      Serial.println("SPENGO LED CAMERA MOVIMENTO TERMINATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        leds[i] = CRGB::Black;
      }
      contnotturno = 1;
      FastLED.show();
      FastLED.clear();
    }
  }
}

This screen are referred to this last function where timeout occur

If I delete the only

Serial.print("i = ");

I obtain the followin timeout result

After this timeout crash I need to restart Arduino because it seems frozen.


#34

ok. please do no post screenshots if not necessary. you just simply can copy paste the serial monitor like with your code. and if you do screenshot, do with the printscr button, this is what it used for, not some shady 3rd party website.

now back to the problem:
you mention you have system crash. this can also be caused by low memory. when you click on compile (verify) button in the arduino ide, what are the values showed for flash and ram % used?


#35
Lo sketch usa 26066 byte (10%) dello spazio disponibile per i programmi. Il massimo è 253952 byte.
Le variabili globali usano 1707 byte (20%) di memoria dinamica, lasciando altri 6485 byte liberi per le variabili locali. Il massimo è 8192 byte.

#36

ok, so the memory is not a problem. i’m on phone right now, and anyway i’m too tired to check your last posted code. will do it tomorrow.

until that, you can put serial println in several places in the suspicious modalitaNotturna function where the timeout / crash occures. try to print millis and see how long it takes / where it crashes. also, put a serial println millis in the main loop, with some label to see how often the blynk run function gets executed. blynk run should run at least every 500 millis to avoid timeout.


#37

I try to do a summary of the problem I still have

My void loop() and void setup() are the following :

void setup()
{
  Serial.begin(9600);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deseleziono il lettore SD card
  pinMode(FRPIN , INPUT);
  pinMode(PIRCAMERAPIN , INPUT);
  pinMode(PIRSALAPIN , INPUT);
  pinMode(PIRMAGAZZINOPIN , INPUT);
  pinMode(MODALLARME, INPUT);
  pinMode(MODGIORNO, INPUT);
  pinMode(MODNOTTE, INPUT);
  pinMode(MODEFFETTI, INPUT);
  Blynk.begin(auth, IPAddress(xxx,xxx,x,x));
  dht.begin();
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
  
 
  timer.setInterval(1L, modalitaNotturna);
  timer.setInterval(1L, modalitaEffettiLuminosi);
}

void loop()
{
  unsigned long inizioloop = millis();
  Blynk.run();
  timer.run();
  unsigned long fineloop = millis();
  unsigned long tempesec = fineloop-inizioloop;
  Serial.print("Tempo esecuzione Blynk.run = ");
  Serial.println(tempesec);
}

The 2 functions called using timer.setInterval() are the following

void modalitaNotturna()
{
  modnotte = digitalRead(MODNOTTE);
  pirCamera = digitalRead(PIRCAMERAPIN);
  fotoresval = analogRead(FRPIN);
  frvalperc = map(fotoresval, 0, 1000, 0, 100);
  if (modnotte == HIGH)
  {
    Serial.println("MODALITA NOTTURNA ATTIVATA");
    if (frvalperc < 50  && pirCamera == HIGH && contnotturno == 1)
    {
      unsigned long tempoinizio = millis();
      Serial.println("ACCENDO LED CAMERA MOVIMENTO RILEVATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        if (i %10 == 0)
        {
          Blynk.run();
        }
        Serial.println(i);*/
        leds[i] = CRGB::Gold;
      }
      unsigned long tempofine = millis();
      Serial.print("Tempo uscita For Mod Notte = ");
      Serial.println(tempofine - tempoinizio);
      FastLED.show();
      contnotturno = 0;
    }
    else if ((pirCamera == LOW && contnotturno == 0) || (frvalperc >= 50 && contnotturno == 0))
    {
      Serial.println("SPENGO LED CAMERA MOVIMENTO TERMINATO");
      for (int i = 0; i <= NUM_LEDS; i++)
      {
        leds[i] = CRGB::Black;
      }
      contnotturno = 1;
      FastLED.show();
      FastLED.clear();
    }
  }
}
void modalitaEffettiLuminosi()
{
  bottoneeffetti = digitalRead(MODEFFETTI);
  if (bottoneeffetti == HIGH)
  {
    Serial.println("MODALITA EFFETTI LUMINOSI");
   /*
    unsigned long tin = millis();
    if (effetto == 1)
    {
      //effettoMultiRGB();
    }
     if (effetto == 2)
    {
      effettoGradiente3Colori();
    }
     if (effetto == 3)
    {
      effettoScontroaMetaLungoRGB();
    }
     if (effetto == 4)
    {
      effettoScontroaMetaRGB();
    }
     if (effetto == 5)
    {
      effettoSerpenteMonocolore();
    }
     if (effetto == 6)
    {
      effettoSerpenteRGB();
    }
     if (effetto == 7)
    {
      effettoSerpenteMisto();
    }
    /*
    unsigned long tfin = millis();
    Serial.print("Tempo Esecuzione IF = ");
    Serial.println(tfin-tin);
    */
    conteffettiluminosi = 0;
  }
  else if (bottoneeffetti == LOW && conteffettiluminosi == 0)
  {
    Serial.println("SPENGO MODALITA EFFETTI LUMINOSI");
    for (int i = 0; i <= 100; i++)
    {
      leds[i].CRGB::Black;
    }
    conteffettiluminosi = 1;
    FastLED.show();
    FastLED.clear();
  }
}

As you can see in this last function I have the group of nested if-condition commented and in this case I dont have timeout problem.

When i remove the comment by eliminating this character

*/ /*

at the beginning and at the end of if-conditions code stop running and I perform timeout.

The for-loop monitored with millis takes 450ms to be executed and the void loop in this case(so when the for is executed) takes 520ms

If i remove from this for-loop

if (i %10 == 0)
        {
          Blynk.run();
        }
        Serial.println(i);

It takes 40ms to be executed and the void loop() takes 108ms.

I was able to do all of this try only because the if-conditions were commented otherwise the code stop running inside the for-loop monitored by millis and I don’t know what happens.

So in the end I need this group of nested if-condition but their causing me timeout.

Can someone please help me solving this problem?


#38

Is this a joke? :joy:
The whole purpose of timers is to avoid running the same code a gazillion times in loop. You are specifying that your functions should run every 1 millisecond… Start by adjusting there :stuck_out_tongue:


#39

It’s not a joke.

I think that 1ms is a problem if a call a Blynk.virtualWrite in my function but as you can see in my function there’s any virtualWrite.

Anyway the problem still persist also if a put 1sec of timer