FastLED using 5 arrays, each will animate on individual button press

Hi all

I'm stuck at a point while using FastLED to animate WS2812B LED light strips, this code is yet incomplete (checking only one array with one button) because I noticed the lights are working even without calling FastLED[0].showLeds(gbrightness) function. My objective is to keep the animation sequence working in IC but show it only when the button is pressed.

Your replies are considered valuable.

Code:

#include "FastLED.h"

#define strip1 48
#define strip2 48
#define strip3 48
#define strip4 48
#define strip5 48
 
#define FRAMES_PER_SECOND  30

#define DATA_strip1 3
#define DATA_strip2 4
#define DATA_strip3 5
#define DATA_strip4 6
#define DATA_strip5 7

// Define the array of leds

CRGB rpv[strip1];
CRGB sg[strip2];
CRGB first_loop[strip3];
CRGB second_loop[strip4];
CRGB third_loop[strip5];

const int sw1 = 9;
int sw1state;


void setup() { 
  FastLED.addLeds<WS2812B,DATA_strip1,GRB>(rpv,strip1);
  FastLED.addLeds<WS2812B,DATA_strip2,GRB>(sg,strip2);
  FastLED.addLeds<WS2812B,DATA_strip3,GRB>(first_loop,strip3);
  FastLED.addLeds<WS2812B,DATA_strip4,GRB>(second_loop,strip4);
  FastLED.addLeds<WS2812B,DATA_strip5,GRB>(third_loop,strip5);

	FastLED.setBrightness(200);

  pinMode (sw1,INPUT);

}

//void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() { 

static uint8_t huei = 0;
static uint8_t huej = 24;
uint8_t j=24;
	
for(int i = 0; i < strip3; i++) {
  
huei=i;

first_loop[i] = CHSV(huei, 255, 255);

  fadeToBlackBy( first_loop, strip3, 150);

	FastLED.delay(1000/FRAMES_PER_SECOND); 
  
  if (i==strip3) i=0;
	if (j==strip3) j=0;
  
huej=j;
 
 first_loop[j] = CHSV(huej, 255, 255);

 j++;

}


sw1state = digitalRead (sw1);

if (sw1state==HIGH)
    FastLED[2].showLeds(200); 

}

sw1state = digitalRead (sw1);
if (sw1state==HIGH)
FastLED[2].showLeds(200);

i want this part of code to work.

Are you using pull up or pull down resistors? If not, your input is floating and can 've in any state when you read it.

sorry for late reply.

i have used buttons for a long time, even to verify your question i used input_pullup and grounded the pin. its not the hardware issue im sure but how the fastLED library works that is getting me difficult to operate the LEDs. i have put the "for loop" under button press 'if' statement, so it works but the LEDs dont turn off. taking help from fastLED examples, i still couldnt make lights to off when the button is released.

You need to forget about the current state of the button and concentrate on when the button state changes.

There is a "State change" example in the IDE.

What you have to do is to have a variable that remembers the last state of the button, this is updated to the current state as the last thing you do in the loop function.
The the code becomes

if(lastButtonState != buttonState && buttonState == LOW) { // just detected a press

//.....................

if(lastButtonState != buttonState && buttonState == HIGH) { // just detected a release