ESP8266 Running Blynk and Neopixel randomly resets

I am running Blynk on ESP8266 NodeMCU module with two Neopixels attached to it. It runs fine until it doesnt. It resets and the lamp goes back to its default state. Plus its annoying to watch to randomly turn on and off. I have tried adding caps but no joy. My code is as follows. Please let me know if anything stands out to you. Any help is greatly appreciated.

#define BLYNK_PRINT Serial

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

char auth[] = "";

char ssid[] = "";
char pass[] = "";

Adafruit_NeoPixel strip1(45, D5, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(45, D6, NEO_GRB + NEO_KHZ800);

uint8_t bars[7] = {3, 9, 15, 21, 27, 34, 40};
uint8_t non_bars[38] = {1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13,
                      14, 16, 17, 18, 19, 20, 22, 23, 24,
                      25, 26, 28, 29, 30, 31, 32, 33, 35, 
                      36, 37, 38, 39, 41, 42, 43, 44, 45};

bool power = true;        
int lightMode = 3;
int brightness = 100;
long lastTime = 0;
long firstPixelHue = 0;
int delayTime = 10;

//--------------------------- Main program loops ---------------------------

void setup() {

  strip1.begin();          
  strip1.show();           
  strip1.setBrightness(brightness);
  strip2.begin();
  strip2.show();            
  strip2.setBrightness(brightness);
  Blynk.begin(auth, ssid, pass);
  stateSync();
}

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

//--------------------------- Blynk Call Backs ---------------------------

BLYNK_WRITE(V0)
{
  lightMode = param.asInt();
  stateSync();
}

BLYNK_WRITE(V1)
{
 brightness = param.asInt();
 strip1.setBrightness(brightness); 
 strip2.setBrightness(brightness); 
 strip1.show();
 strip2.show();
}

BLYNK_WRITE(V2)
{
  power = param.asInt();
}

BLYNK_WRITE(V3)
{
  delayTime = param.asInt();
}

BLYNK_WRITE(V4)
{
  int R = param[0].asInt();
  int G = param[1].asInt();
  int B = param[2].asInt();
  firstPixelHue = strip1.Color(R,G,B);
  colorWipe(firstPixelHue);
  
}

//--------------------------- Light control functions ---------------------------

void lightControl(){
  if((millis()-lastTime)>delayTime){
    if(power){  
      switch(lightMode){
        case 1:
          colorWipe(firstPixelHue);
          break;
        case 2:
          staticRainbow();
          break;
        case 3:
          runningRainbow();
          break;
      }
    }
    else {
      turnOff();
    }
  lastTime = millis();  
  }
}

void colorWipe(uint32_t color) {
  for(int i=0; i<strip1.numPixels(); i++) { 
    strip1.setPixelColor(i, color);
    strip2.setPixelColor(i, color);                                                         
  }
  strip1.show();
  strip2.show();
}

void staticRainbow(){
    for(int i=0; i<strip1.numPixels(); i++) {
          strip1.setPixelColor(i, strip1.gamma32(strip1.ColorHSV(firstPixelHue)));
          strip2.setPixelColor(i, strip2.gamma32(strip2.ColorHSV(firstPixelHue)));
        }
        strip1.show();
        strip2.show();
        firstPixelHue += 256; 
}


void runningRainbow(){
        for(int i=0; i<strip1.numPixels(); i++) {
          int pixelHue = firstPixelHue + (i * 65536L / strip1.numPixels());
          strip1.setPixelColor(i, strip1.gamma32(strip1.ColorHSV(pixelHue)));
          strip2.setPixelColor(i, strip2.gamma32(strip2.ColorHSV(pixelHue)));
        }
        strip1.show();
        strip2.show();
        firstPixelHue += 256; 
}

void turnOff(){
  strip1.clear();
  strip2.clear();
  strip1.show();
  strip2.show();
}

void stateSync(){
  Blynk.virtualWrite(V0, lightMode);
  Blynk.virtualWrite(V1, brightness);
  Blynk.virtualWrite(V2, power);
  Blynk.virtualWrite(V3, delayTime);
}

Hi,

See this post about Keeping Your Loop Clean . . .
http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

Remove this from the void loop():

and initiate that function with a timer, it’s a quite complex function and calls additional functions, it’s possible that it is causing Blynk to timeout by having this in the loop.

Try this and see how you go.

cul
billd

Thank you for the info. I tried to use timers but for some reason it wasnt working. I reduced the frequency of that function executing and that solved the problem. I didnt think that having a function execute quickly would result in device reset. But it might be causing some sort of memory issue at the back end resulting in the problem. Thanks.

You do t say how you’ve reduced the frequency of the lightControl function, but the correct way to do that is to call the function with a timer, and dictating the frequency by changing the delay between the timer calls.

If your timer isn’t working then you should look at why that is. If you don’t get this right then you will experience Blynk disconnections in the medium term.

Pete.