Dividing LEDs into different groups

Hello, I wanted to ask if it is possible to make different groups in your code.

I have the following code

#include "FastLED.h"
#define NUM_LEDS 88
CRGB leds[NUM_LEDS];
#define PIN 6

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}


void loop() {
  meteorRain(0xCE,0x08,0xFF,10, 50, false, 26);
}

void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
      }
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}

void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
   
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
   
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

It's working good but I'd like to let the first and the last LED blink while the others use the animation. Can I add this? I saw that this is possible in Mu Editor where you just say group1 = and then the number of the LED and then you can define each group. However, I haven't discovered where to do that in Arduino IDE and I really want to use that programme.

There's some tricks in:

Edit: Oops, I thought that covered the CPixelView stuff, which enables using subsets of an LED array as a virtual array.

https://fastled.io/docs/group___pixel_set.html

  • leds[NUM_LEDS] we assume you know this array is where the pixel state (colour) is stored.

  • Element 0 and NUM_LEDS - 1 are the 1st and last pixels.

  • These can be updated as you need in your sketch.

1 Like

You won't be able to do anything, including blinking your end LEDs while this delay is happening. It blocks all code execution until it is finished.

1 Like

I deleted this line and added my blinking animation. But now everything is blinking fast and I don't have the meteor animation anymore

#include "FastLED.h"
#define NUM_LEDS 88
CRGB leds[NUM_LEDS];
#define PIN 6

// Deklaration der Funktion slowBlink vor dem Setup
void slowBlink(CRGB color, int interval);

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}


void loop() {
  meteorRain(CRGB(0xCE, 0x08, 0xFF), 10, 30, false, 25);
  slowBlink(CRGB(0xCE, 0x08, 0xFF), 500); // Lila Blinken für die erste und letzte LED (500 ms Intervall)
}

void meteorRain(CRGB color, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  fill_solid(leds, NUM_LEDS, CRGB::Black);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
    // fade brightness all LEDs one step
    for(int j = 0; j < NUM_LEDS; j++) {
      if ((!meteorRandomDecay) || (random(10) > 5)) {
        leds[j].fadeToBlackBy(meteorTrailDecay);        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if ((i - j < NUM_LEDS) && (i - j >= 0)) {
        leds[i - j] = color;
      }
    }
   
    FastLED.show();
  }
}

// Definition der slowBlink Funktion
void slowBlink(CRGB color, int interval) {
  static unsigned long lastUpdateTime = 0;
  static boolean state = false;
  
  if (millis() - lastUpdateTime >= interval) {
    lastUpdateTime = millis();
    state = !state;
    if (state) {
      // Lila für die erste LED
      leds[0] = color;
      // Lila für die letzte LED
      leds[NUM_LEDS - 1] = color;
    } else {
      // LEDs ausschalten
      fill_solid(leds, NUM_LEDS, CRGB::Black);
    }
    FastLED.show();
  }
}

You need similar to this.

  • Set the animation to operate on the array pixels element 1 to NUM_LEDS - 2.

  • Update pixel states for elements 0 and NUM_LEDS - 1 (these are your flashing pixels).

  • execute strip.show(); OR FastLED.show();

1 Like

You can't just delete the delay(), you have to use millis() like you did for the blinking

#include "FastLED.h"
#define NUM_LEDS 88
CRGB leds[NUM_LEDS];
#define PIN 6

// Deklaration der Funktion slowBlink vor dem Setup
void slowBlink(CRGB color, int interval);

void setup()
{
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}


void loop() {
  meteorRain(CRGB(0xCE, 0x08, 0xFF), 10, 30, false, 25);
  slowBlink(CRGB(0xCE, 0x08, 0xFF), 500); // Lila Blinken für die erste und letzte LED (500 ms Intervall)
}

void meteorRain(CRGB color, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  static unsigned long lastUpdateTime = 0;
  if (millis() - lastUpdateTime >= SpeedDelay) {
    lastUpdateTime = millis();
    fill_solid(leds + 1, NUM_LEDS - 1, CRGB::Black);

    // fade brightness all LEDs one step
    for (int j = 1; j < NUM_LEDS - 1; j++) {
      if ((!meteorRandomDecay) || (random(10) > 5)) {
        leds[j].fadeToBlackBy(meteorTrailDecay);
      }
    }

    // draw meteor
    for (int j = 0; j < meteorSize; j++) {
      if ((i - j < NUM_LEDS - 1) && (i - j >= 1)) {
        leds[i - j] = color;
      }
    }
    FastLED.show();
  }
}

// Definition der slowBlink Funktion
void slowBlink(CRGB color, int interval) {
  static unsigned long lastUpdateTime = 0;
  static boolean state = false;

  if (millis() - lastUpdateTime >= interval) {
    lastUpdateTime = millis();
    state = !state;
    if (state) {
      // Lila für die erste LED
      leds[0] = color;
      // Lila für die letzte LED
      leds[NUM_LEDS - 1] = color;
    } else {
      // LEDs ausschalten
      fill_solid(leds, NUM_LEDS, CRGB::Black);
    }
    FastLED.show();
  }
}

If you replace the blocking for(int i=0... to manage it without blocking loop(), you need to add a global or static i variable and manage it explicitly. Maybe something like:

void meteorRain(CRGB color, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  static unsigned long lastUpdateTime = 0;
  static int i = 0;
  if (millis() - lastUpdateTime >= SpeedDelay) {
    lastUpdateTime = millis();
    fill_solid(leds + 1, NUM_LEDS - 1, CRGB::Black);

    // fade brightness all LEDs one step
    for (int j = 1; j < NUM_LEDS - 1; j++) {
      if ((!meteorRandomDecay) || (random(10) > 5)) {
        leds[j].fadeToBlackBy(meteorTrailDecay);
      }
    }

    // draw meteor
    for (int j = 0; j < meteorSize; j++) {
      if ((i - j < NUM_LEDS - 1) && (i - j >= 1)) {
        leds[i - j] = color;
      }
    }
    FastLED.show();
    ++i;
    if (i >= NUM_LEDS - 2) {
      i = 0;
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.