FastLED Library does not loop without Serial.print

Hi Everyone.

1.Using Arduino Nano (classic)
2.Power supply is 5V 5Amp
3.2200uF CAP close to strip across the 5V
4.Data pin is connected with a 390 ohm resistor
5.Examples from Library does work (Twinklefox and Cyclon tested)

I experimented with some code and see that the loop only runs once but when I add a Serial.print the loop does repeat.

/// @file    Blink.ino
/// @brief   Blink the first LED of an LED strip
/// @example Blink.ino

#include <FastLED.h>

#define NUM_LEDS 60
#define BRIGHTNESS  60
#define DEL100  20

uint8_t i = 0;
uint8_t j = 0;
uint8_t a = 0;
uint8_t b = 0;
uint8_t c = 0;

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN 3
//#define CLOCK_PIN 13

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

void setup()
{

  Serial.begin(115200);
  // Uncomment/edit one of the following lines for your leds arrangement.
  // ## Clockless types ##
  // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
  // FastLED.addLeds<SM16703, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1829, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1812, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS1904, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<UCS2903, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<WS2852, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<GS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA106, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<PL9823, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2813, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GE8822, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD1886, DATA_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<LPD1886_8BIT, DATA_PIN, RGB>(leds, NUM_LEDS);
  // ## Clocked (SPI) types ##
  // FastLED.addLeds<LPD6803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<WS2803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  // FastLED.addLeds<SK9822, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical

  FastLED.setBrightness(  BRIGHTNESS );
}

void loop()
{

  //-----run 3 groups of leds on strip-
  for (i = 0; i < 60; i++)
  {
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Blue;
    leds[c += 1] = CRGB::Blue;
    leds[c += 1] = CRGB::Blue;
    c = c - 8;
    FastLED.show();
    delay(100);

    //Serial.println("c = ");
    //Serial.print(c);

    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    leds[j += 1] = CRGB::Black;
    j = j - 8;
    FastLED.show();

  }

  j = 0;
  c = 0;
}

You're writing data outside the leds array. And possibly writing memory that is used for something else in your code.

Adding a Serial.print will change the memory layout and suddenly you're not overwriting that memory that is used for something else but possibly some memory that is not used.

1 Like

Thanks , when I change 'i' the leds does loop but only runs to LED 48 , so this code is not the way to do it.

 for (i = 0; i < 40; i++)

The empirical way: loop to 50. It works? If not, reduce by one, if yes, increment by one.

I changed: NUM_LEDS 100
Now the code loops and pushes the last LED to the end of the strip but with a small delay all LEDS off.
Then the loop starts again.

Your code, with 60 LEDs works (three blue, three green, three red... chasing from beginning to end). Neopixels rarely fail. I suspect your power connection.

This is because you are telling the Nano to write to Neopxels that are not there. You could have the same effect with NUM_LEDS 69 (or do not write to pixels > 60).

The reason there was a small delay is because I used (i < 70) in the for loop , just for a test.
Then the code writes to 10 extra pixels that does not exsist.

When I change (i < 60) and NUM_LEDS 100 this works when the last LED goes off the pattern starts again without a delay.

You should actually calculate it; I am just too lazy but I think that 50 is the limit. That will increment c to 59 what should be fine.

You're approach also works but wastes additional bytes (3 per non-existing pixel); possibly not a big issue now but it might be in the future.

The delay you see is the delay() you progrzmmed.

A few changes can make this much smaller...

#include <FastLED.h>

#define NUM_LEDS 65 // increase from 60 to 65
#define BRIGHTNESS  255
#define DEL100  20

uint8_t i = 0;
uint8_t c = 0;

#define DATA_PIN 3
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  for (i = 0; i < NUM_LEDS; i++) {
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Red;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Green;
    leds[c += 1] = CRGB::Blue;
    leds[c += 1] = CRGB::Blue;
    leds[c += 1] = CRGB::Blue;
    c = c - 8;
    leds[c - 1] = CRGB::Black; // add this color to the end
    FastLED.show();
    delay(100);
  }
  c = 0;
}

Thanks I will test this ,at work now.

I made one more change...

#include <FastLED.h>

#define NUM_LEDS 68
#define BRIGHTNESS 255
#define DEL100  20

int i = 0;
int j = 0;
int c = 0;
#define DATA_PIN 3
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  train();
}

void train()  {
  for (i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < 3; j++) {
      leds[j + 0 + i] = CRGB::Black;
      leds[j + 3 + i] = CRGB::Red;
      leds[j + 6 + i] = CRGB::Green;
      leds[j + 9 + i] = CRGB::Blue;
      FastLED.show();
    }
    delay(100);
  }
}

... but it lacks a smooth transition... that will be next... which will be something like: when (x > 59) draw (60 - x)