Blynk project on Arduino gets disconnected while initiating theater rainbow chase animation with NeoPixel

I am trying to create a simple project using Blynk on D1 Mini where I can select any animation any time and that should play unless I stop that using my Blynk App. While everything else working perfectly, I am not able to figure out what is wrong on my theaterchaserainbow function, that while selected, my D1 gets disconnected and connected again. Here is the code.

I understood from various forum that I must use blynk.timer instead and set count more than 1000L in setup. But that won’t help me either for this particular theaterchaserainbow function. All other functions works perfectly. Any help would be greatly appreciated.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#include <wifi_credentials.h>

#define BLYNK_PRINT Serial
#define myPixelPin D2
#define myPixels 16

char auth[] = "xxxxxxxmy_authxxxxxx";

int select;

BlynkTimer timer;

// Instatiate the NeoPixel from the ibrary
Adafruit_NeoPixel strip = Adafruit_NeoPixel(myPixels, myPixelPin, NEO_GRB + NEO_KHZ800);

// Timer to repeat animations
void myTimerEvent() {
  if (select == 1) {
    allOff();
  } else if (select == 12) {
    theaterChaseRainbow(50);
  }
}

// NeoPixel all off Switch
BLYNK_WRITE(V0) {
  int pinValue = param.asInt();
  select = 1;
}

// Menu based Animation Selection
BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  switch (pinValue) {
    case 1: // Item 1
      select = 11;
      break;
    case 2: // Item 2
      select = 12;
      break;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  strip.begin();
  strip.show();
  timer.setInterval(1000L, myTimerEvent);
}

void loop() {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer;
}

// Theatre Chase Rainbow
//*****************************************************************************
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
        }
        strip.show();
        delay(wait);
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, 0);        //turn every third pixel off
        }
    }
  }
}

//  Clear Program
//*****************************************************************************
void allOff() {
  for ( int i = 0; i < strip.numPixels(); i++ ) {
    strip.setPixelColor(i, 0, 0, 0 );
  }
  strip.show();
}

// Default Wheel defination
//*****************************************************************************
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

You’re calling this effect with a wait value of 50ms. 256 x 50 = 12.8 seconds to complete this effect cycle,
You’re also triggering this effect from within the myTimerEvent function, which is being triggered every 1 second:

Blynk’s default timeout is 10 seconds. If the server hasn’t heard from the device (and this is done from within the Blynk.run() command) for 10 seconds then it thinks the device is MIA and shows it as disconnected in the app.
It’s also the Blynk.run() command that allows the device to know if any of the widgets within the app have changed state (from On to Off for example).

Adding a Blynk.run() to your theaterChaseRainbow function, maybe just before the delay(wait); line of code, may solve your disconnection problem. However, you still face the issue that you’ve structured your code so that you are calling a function that takes 12.8 seconds to execute once every second.

Pete.

1 Like

Yes this worked. Thank you very much @PeteKnight. However while it is running, it is taking almost 24 Sec to respond to other command. Can this time be reduced?