Strange delay behavior with FastLed

I have a simple program to light multiple LEDs in a WS2811 50 LED string. I want multiple LEDS lit in a "Chasing" fashion. When I try to make the number of LEDS lit greater than 3, the delay is affected. Instead of 25 ms (or whatever value I try), the delay seems to be on the order of one second. Every value I've tried greater than 3 has the same affect. My code is included below.

#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 50

#define DATA_PIN 5

// Define the array of leds
CRGB leds[NUM_LEDS];

// How many leds to light up at one time
byte xLeds = 4;

void setup() {
Serial.begin(57600);
Serial.println("resetting");
LEDS.addLeds<WS2811,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(84);
}

void loop() {
static uint8_t hue = 0;
Serial.print("x");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
// Set the i'th led to green
leds = CHSV(96,255,255);

  • leds[i-xLeds] = CRGB::Black;*
  • // Show the leds*
  • FastLED.show();*
  • // Wait a little bit before we loop around and do it again*
  • delay(25);*
  • }*
  • Serial.print("x");*
  • // Now go in the other direction. *
  • for(int i = (NUM_LEDS)-xLeds; i >= 0; i--) {*
  • //for(int i = 0; i < NUM_LEDS; i++) {*
  • // Set the i'th led to red*
    _ leds = CHSV(0,255,255);_
    * leds[i+xLeds] = CRGB::Black;*
    * // Show the leds*
    * FastLED.show();*

* // Wait a little bit before we loop around and do it again*
* delay(25);*
* }*
}

Now you can see why we ask you to use code tags when posting code.

Please edit your post.

Look at this (snippets):

#define NUM_LEDS 50
int xLeds = 4;

for(int i = 0; i < NUM_LEDS; i++) {
  //...
  leds[i-xLeds] = CRGB::Black; //Will access elements: -4, -3, -2, -1
  //...
  leds[i+xLeds] = CRGB::Black; //Will access elements: 50, 51, 52, 53
  //...
}

By accessing memory you should not, your sketch will act wonky.

Move the FastLED.show( ) and the delay( 25 ) out of the for loop (2 places).

Danois90:
Look at this (snippets):

#define NUM_LEDS 50

int xLeds = 4;

for(int i = 0; i < NUM_LEDS; i++) {
 //...
 leds[i-xLeds] = CRGB::Black; //Will access elements: -4, -3, -2, -1
 //...
 leds[i+xLeds] = CRGB::Black; //Will access elements: 50, 51, 52, 53
 //...
}




By accessing memory you should not, your sketch will act wonky.

The code that creates negative values for the led number is the correct answer. The other line will not create values greater than 50 because the for conditions prevent that "for(int i = (NUM_LEDS)-xLeds; i >= 0; i--) "

Here's the corrected code to prevent negative led numbers:

// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
Serial.println(i);
// Set the i'th led to green
leds = CHSV(96,255,255);

  • if (i>= xLeds){leds[i-xLeds] = CRGB::Black;}*
    // Show the leds
    FastLED.show();
    // Wait a little bit before we loop around and do it again
    delay(25);