Fastled with blynk with/without Wi-Fi and reconnection

Hello, I’m trying to implement this code in order to work with/without WI-FI and trying to reconnect in case. I can’t figure out how to do it, I tried using some code found on this topic but it doesn’t work. Some help would be appreciated.

Thank you.

#include <BlynkSimpleWiFiNINA.h>

/*VAR*/
int program = 1;
int brightness = 50;
int lampStatus = 0;
int stripRedVal = 0; 
int stripGreenVal = 0; 
int stripBlueVal = 0;
int stripRedVal_2 = 0; 
int stripGreenVal_2 = 0; 
int stripBlueVal_2 = 0;

/*BLYNK*/
BLYNK_CONNECTED()
{
    Blynk.syncAll();
}

//Program write form the app
BLYNK_WRITE(V0)
{
    program = param.asInt();
}

//Brightness write from the app
BLYNK_WRITE(V1)
{
    brightness = param.asInt();
}

//On off write from the app
BLYNK_WRITE(V2)
{
    lampStatus = param.asInt();
}

//Red value write from the app
BLYNK_WRITE(V3)
{
    stripRedVal = param.asInt();
}

//Green value write from the app
BLYNK_WRITE(V4)
{
    stripGreenVal = param.asInt();
}

//Blue value write from the app
BLYNK_WRITE(V5)
{
    stripBlueVal = param.asInt();
}

BLYNK_WRITE(V7)
{
    stripRedVal_2 = param.asInt();
}

BLYNK_WRITE(V8)
{
    stripGreenVal_2 = param.asInt();
}

BLYNK_WRITE(V9)
{
     stripBlueVal_2 = param.asInt();
}


#include <FastLED.h>
#include <SPI.h>
#include <math.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <BlynkUtility.h>

/* WiFi */
char auth[] = "auth";
char ssid[] = "ssid";
char pass[] = "pass";

/*FastLED*/
#define LED_PIN_1           3
#define LED_PIN_2           4
#define LED_PIN_3           5
#define LED_PIN_4           6

#define NUM_LEDS            31
#define START_BRIGHTNESS    30

CRGB leds_1[NUM_LEDS];
CRGB leds_2[NUM_LEDS];
CRGB leds_3[NUM_LEDS];
CRGB leds_4[NUM_LEDS];
CRGBPalette16 gPal;

/* Buttons */
#define BUTTON_PIN_1        7
#define BUTTON_PIN_2        8
#define BUTTON_PIN_3        9
#define BUTTON_PIN_4        10
#define PROGRAMS            8
byte prevKeyStateOne = HIGH; 
byte currKeyStateOne = HIGH; 

byte prevKeyStateTwo = HIGH; 
byte currKeyStateTwo = HIGH; 

/* Utility funcion */
void buttonBrightness();
void buttonProgram();

/* fire */
void fire();
#define COOLING  55
#define SPARKING 120

bool gReverseDirection = false;

/* FireBlue */
void fireBlue();

/* FireYell */
void fireYell();
void fire2014();

/* RGB */
void rainbow(int speed);

int startIndex = 0;
int speedEffect = 1;

/* blendme */
void blendme();

/* fireMultiColor */
void fireMultiColor(CRGB, CRGB); 

/* solid */
void solid(CRGB);

void setup() 
{
    //delay( 3000 );
    Serial.begin(9600);

    Blynk.begin(auth, ssid, pass);
    FastLED.addLeds<NEOPIXEL, LED_PIN_1>(leds_1, NUM_LEDS).setCorrection(TypicalLEDStrip);
    FastLED.addLeds<NEOPIXEL, LED_PIN_2>(leds_2, NUM_LEDS).setCorrection(TypicalLEDStrip);
    FastLED.addLeds<NEOPIXEL, LED_PIN_3>(leds_3, NUM_LEDS).setCorrection(TypicalLEDStrip);
    FastLED.addLeds<NEOPIXEL, LED_PIN_4>(leds_4, NUM_LEDS).setCorrection(TypicalLEDStrip);
    FastLED.setBrightness(START_BRIGHTNESS);

    gPal = CRGBPalette16( CRGB::Black, CRGB::Black, CRGB::Black,  CRGB::Black);

    pinMode(BUTTON_PIN_1, INPUT_PULLUP);
    pinMode(BUTTON_PIN_2, INPUT_PULLUP);
    pinMode(BUTTON_PIN_3, INPUT_PULLUP);
    pinMode(BUTTON_PIN_4, INPUT_PULLUP);
}

void loop()
{
    Blynk.run();

    buttonBrightness();
    buttonProgram();
    FastLED.setBrightness(brightness);

    if (lampStatus == 0)
    { 
        FastLED.clear();
        FastLED.show();
    }
    else 
    {
        switch (program)
        {
        case 1:
            fire();
            break;
        
        case 2:
            rainbow(startIndex);
            break;

        case 3:
            blendme();
            break;

        case 4: 
            fireBlue();
            break;

        case 5:
            break;

        case 6:
            fireYell();
            break;

        case 7:
            if (stripRedVal + stripGreenVal + stripBlueVal + stripRedVal_2 + stripGreenVal_2 + stripBlueVal_2 == 0)
            {
                FastLED.clear();
                FastLED.show();
            }
            else
                fireMultiColor(CRGB(stripRedVal, stripGreenVal, stripBlueVal), CRGB(stripRedVal_2, stripGreenVal_2, stripBlueVal_2));
            break;

        case 8:
            solid(CRGB(stripRedVal, stripGreenVal, stripBlueVal));
            break;
        default:
                FastLED.clear();
                FastLED.show();
                Serial.println("default case");
            break;
        }
        
    }
}


/*      Programs implementation      */

//fire
void fire()
{
    static byte heat[NUM_LEDS];

    for(int i = 0; i < NUM_LEDS; i++)
    {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
    }
  
    for(int k= NUM_LEDS - 1; k >= 2; k--) 
    {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; 
    }
    
    if(random8() < SPARKING) 
    {
      int y = random8(7);
      heat[y] = qadd8( heat[y], random8(160, 255) ); 
    }

    for( int j = 0; j < NUM_LEDS; j++) 
    {
      CRGB color = HeatColor(heat[j]);
      int pixelnumber;

      if( gReverseDirection ) 
      {
        pixelnumber = (NUM_LEDS-1) - j;
      } 
      else 
      {
        pixelnumber = j;
      }

      leds_1[pixelnumber] = color;
      leds_2[pixelnumber] = color;
      leds_3[pixelnumber] = color;
      leds_4[pixelnumber] = color;
    }
    FastLED.show();
    delay(16);
}

/* fire color */
void fireBlue()
{
    random16_add_entropy(random());

    fireMultiColor(CRGB::Blue, CRGB::Aqua);  
}

/* rainbow */
void rainbow(int colorIndex)
{    
    for( int i = 0; i < NUM_LEDS; ++i) 
    {
        leds_1[i] = ColorFromPalette( RainbowColors_p, colorIndex, 255, LINEARBLEND);
        leds_2[i] = ColorFromPalette( RainbowColors_p, colorIndex, 255, LINEARBLEND);
        leds_3[i] = ColorFromPalette( RainbowColors_p, colorIndex, 255, LINEARBLEND);
        leds_4[i] = ColorFromPalette( RainbowColors_p, colorIndex, 255, LINEARBLEND);
        colorIndex += 3;
    }

    FastLED.show();
    FastLED.delay(10);
    startIndex += speedEffect;
}

/* blendme */
void blendme() 
{
    uint8_t starthue = beatsin8(1, 0, 255);
    uint8_t endhue = beatsin8(2, 0, 255);
  
  
    if (starthue < endhue) 
    {
        fill_gradient(leds_1, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);   
        fill_gradient(leds_2, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);    
        fill_gradient(leds_3, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);    
        fill_gradient(leds_4, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), FORWARD_HUES);    

    } 
    else 
    {
        fill_gradient(leds_1, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);
        fill_gradient(leds_2, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);
        fill_gradient(leds_3, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);
        fill_gradient(leds_4, NUM_LEDS, CHSV(starthue,255,255), CHSV(endhue,255,255), BACKWARD_HUES);
    }
    FastLED.show();
}

/* FireYell */
void fireYell()
{
  random16_add_entropy(random());

  fireMultiColor(CRGB::Yellow, CRGB::Yellow); 
}

/* fireMultiColor */
void fireMultiColor(CRGB first, CRGB second) //fire yellow 
{
    static byte heat[NUM_LEDS];

    for( int i = 0; i < NUM_LEDS; i++) 
    {
        heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
    }
    
    for( int k= NUM_LEDS - 1; k >= 2; k--)
    {
        heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
        
    if( random8() < SPARKING )
    {
        int y = random8(7);
        heat[y] = qadd8( heat[y], random8(100,255) );
    }

    for( int j = 0; j < NUM_LEDS; j++)
    {
        byte colorindex = scale8( heat[j], 240);
        CRGB color = ColorFromPalette( CRGBPalette16( CRGB::Black, first, second,  CRGB::White), colorindex);
        int pixelnumber;

        if(gReverseDirection)
        {
            pixelnumber = (NUM_LEDS-1) - j;
        } 
        else 
        {
            pixelnumber = j;
        }
        leds_1[pixelnumber] = color;
        leds_2[pixelnumber] = color;
        leds_3[pixelnumber] = color;
        leds_4[pixelnumber] = color;
    }

    FastLED.show();
    FastLED.delay(16);
}

/* solid */
void solid(CRGB color)
{
    for (int i = 0; i < NUM_LEDS; i++)
    {
        leds_1[i] = color;
        leds_2[i] = color;
        leds_3[i] = color;
        leds_4[i] = color;
    }
    FastLED.show();
}

/* Utility functions */
void buttonBrightness()
{
    if (digitalRead(BUTTON_PIN_1) == 0)
    {
        if (brightness + 3 < 255)
        {
            brightness += 3;
            Blynk.virtualWrite(V1, brightness);
        }

    }
    else if (digitalRead(BUTTON_PIN_2) == 0)
    {
        if (brightness - 3 > 5)
        {
            brightness -= 3;
            Blynk.virtualWrite(V1, brightness);
        }
    }
}

void buttonProgram()
{
    currKeyStateOne = digitalRead(BUTTON_PIN_3);
    currKeyStateTwo = digitalRead(BUTTON_PIN_4);

    if (prevKeyStateOne == 1 && currKeyStateOne == 0)
    {
        if (program + 1 <= PROGRAMS)
        {
            program += 1;
            Blynk.virtualWrite(V0, program);
        }

        prevKeyStateOne = currKeyStateOne; 
    }

    
    if (prevKeyStateTwo == 1 && currKeyStateTwo == 0)
    {
        if (program - 1 > 0)
        {
            program -= 1;
            Blynk.virtualWrite(V0, program);
        }

        prevKeyStateTwo = currKeyStateTwo; 
    }

    prevKeyStateTwo = currKeyStateTwo; 
    prevKeyStateOne = currKeyStateOne; 
}

Would you care to be more specific?

Your void loop isn’t compatible with Blynk. You should read this:

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

oh thanks i didn’t know that i tried to correct my code, anyway i’d like to make this code works with and without wifi and in case your home wifi goes off the code don’t stop, because now if you aren’t connected to wifi or your wifi goes off the code will stop working.
Thank you very much.

[Unformatted code removed by moderator]

@Mrnobody I’ve deleted your unformatted code.
When you post code it has to be formatted with triple backticks, as you did in your first post.

Pete.

So, I still don’t know what:

means in practice, and your void loop is still a mess.

Blynk.begin() is a blocking function which will prevent all further code execution if a connection to WiFi or the Blynk server isn’t possible. You should establish your own WiFi connection then use Blynk.config and Blynk.connect if you want a non-blocking solution. This has been discussed many times on the forum and if you search you will find code examples.

However, I suspect that your real issue is your void loop, which is causing the disconnections in the first place.

Pete.

Ok thx I’ll try that, but what should i do to solve my void loop ?
Thank you

Either attach hardware interrupts to your physical button pins, and use debounce code, or use a timer to poll your buttons to check if they have been pressed.

Pete.

I’m sorry i didn’t understand what i should do to solve the problem i’m not a very good in this things, may you try to explain this in a simpler way.
Thank you for patience and sorry for my little knowledge.

You’ll need to do some research to boost your knowledge. If you search the forum you’ll find lots of topics about polling switches with BlynkTimer. If you want to go down the hardware interrupt route then that is also discussed here to a degree, but you’d probably be best to start with some google research on which pins support hardware interrupts on whichever board it is that you are using.

The choice of polling/interrupts will depend on the responsiveness to the physical buttons that you are looking for, and how seamlessly you what the LEDs to look. Interrupts will probably give the better result, but may be beyond your current skill level.

Pete

ok thx i’ll take a look in this forum.