Void loop() clean

Hi

I’m trying to figure out if I’m doing the right thing. I have this code in my “void loop()”

void loop(){
  Blynk.run();
  timer.run();
 switch (arrayCurrent[0]) {
    case 1:
      fill_solid(leds, LED_NUMBER, CHSV(arrayCurrent[1], arrayCurrent[2], arrayCurrent[3]));
      break;
    case 2:
      FastLED.clear();
      for (int led = 0; led < testLEDnumber; led++) leds[led] = CRGB::Blue;
      break;
    default:
      gPatterns[gCurrentPatternNumber]();
      EVERY_N_MILLISECONDS( 20 ) gHue++;  // slowly cycle the "base color" through the rainbow
      break;

  }
  FastLED.show();
}

The Blynk rules say that the “void loop ()” must be clean but I need the code to be repeated very quickly otherwise the leds don’t work as they should.
So I fixed the code like that

I used blynktimer and in the void setup() add this:

timer.setInterval(20L, inizio); //timer della routine iniziale ogni 20ms secondo

and then I cleaned the “void loop ()” like this

void loop(){
  Blynk.run();
  timer.run(); 
}



void inizio()
{
switch (arrayCurrent[0]) {
    case 1:
      fill_solid(leds, LED_NUMBER, CHSV(arrayCurrent[1], arrayCurrent[2], arrayCurrent[3]));
      break;
    case 2:
      FastLED.clear();
      for (int led = 0; led < testLEDnumber; led++) leds[led] = CRGB::Blue;
      break;
    default:
      gPatterns[gCurrentPatternNumber]();
      EVERY_N_MILLISECONDS( 20 ) gHue++;  // slowly cycle the "base color" through the rainbow
      break;
 }
  FastLED.show();  
}

everything seems to work fine. I wanted an opinion from you if it is the right way because I had to insert a timer of only 20ms to obtain acceptable effects on the LEDs. Could such a fast timer give Blynk any problems?

Thank you

Best Regards

Should be fine either way. This whole thing about keeping the void loop clean is over rated. I mean, what if one of your timer service routines takes 2 or 3 seconds? Blynk.run() will just have to wait for that 2 seconds.

Hi ScottB

I can not tell you, but on the documentation of Blynk and also in many posts on the forum they say that it is a very important thing

You should keep your void loop clean for a good reason, check this out :

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

I think with “keep it clean” means this:

  • Make sure Blynk.run() (and timer.run() ) is called “often enough”.

Therefore, don’t use “delay(NNN)” in the loop as this prevents from fulfilling “often enough”.
Also, dont use “slow code” (much code taking long time to execute) in the void loop, as that also prevents from fulfilling “often enough”.
Dont use code “waiting” for other events.

It is perfectly OK to have a “large code chunk” in the void loop. Just make sure it executes fast. No slow Serial.prints, no delays, no long for-loops, no while with external dependencies, etc. I have switch cases which are “long” but fast.

Jonas

Hi
The discussion is getting interesting !!!
I have followed the guide that says John93 now I am trying to understand if the value of my timer is too high and it can create problems. I entered 20ms so if I’m not mistaken the cycle should be repeated 50 times per second. what do you think can cause problems?

Thanks

Best Regards

I don’t know if you tested case 2, but this is a blocking function , depend on testLEDnumber value

Hi Blynk_Coeur

in fact i removed case 2 and i don’t find any difference. The code is part of another project that I am re-adapting for my project I don’t know why it was put in case 2 by the original creator

1 Like

ok I would like to close this post tell me the procedure to insert “solved” or must an administrator do it?

thank you all