PaulStoffregen / WS2812Serial

Non-Blocking WS2812 LED Library
137 stars 28 forks source link

Defining more than 89 leds won't work #1

Closed psitech closed 6 years ago

psitech commented 6 years ago

Platform: Teensy LC [pin 24 wired to pin 17], Arduino 1.8.5, Teensyduino 1.40, latest FastLED lib with WS2812Serial support [downloaded on Nov 2], 144pcs WS2812B LED strip.

When I define more than 89 LEDs, LED strip stays blank.

// FastLED Cylon Example, using Non-Blocking WS2812Serial

#include <WS2812Serial.h>
#define USE_WS2812SERIAL
#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 89 // >89, e.g. 90 or 144 won't work

// Usable pins:
//   Teensy LC:   1, 4, 5, 24
//   Teensy 3.2:  1, 5, 8, 10, 20, 31
//   Teensy 3.5:  1, 5, 8, 10, 20, 26, 32, 33, 48
//   Teensy 3.6:  1, 5, 8, 10, 20, 26, 32, 33
#define DATA_PIN 24

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

void setup() {
  //    Serial.begin(57600);
  //    Serial.println("resetting");
  LEDS.addLeds<WS2812SERIAL, DATA_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(32);
}

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

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 red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(20);
  }
  Serial.print("x");

  // Now go in the other direction.
  for (int i = (NUM_LEDS) - 1; i >= 0; i--) {
    // Set the i'th led to red
    leds[i] = CHSV(hue++, 255, 255);
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    // leds[i] = CRGB::Black;
    fadeall();
    // Wait a little bit before we loop around and do it again
    delay(20);
  }
}

89 LED's defined:

Sketch uses 18440 bytes (29%) of program storage space. Maximum is 63488 bytes. Global variables use 4624 bytes (56%) of dynamic memory, leaving 3568 bytes for local variables. Maximum is 8192 bytes.

90 LEDs defined:

Sketch uses 18440 bytes (29%) of program storage space. Maximum is 63488 bytes. Global variables use 4628 bytes (56%) of dynamic memory, leaving 3564 bytes for local variables. Maximum is 8192 bytes.

Regards, Paul

psitech commented 6 years ago

WS2812Serial BasicTest works at 106 LEDs, 107 and more does not work. 106 LEDs defined:

Sketch uses 16092 bytes (25%) of program storage space. Maximum is 63488 bytes. Global variables use 5900 bytes (72%) of dynamic memory, leaving 2292 bytes for local variables. Maximum is 8192 bytes.

107 LEDs defined:

Sketch uses 16092 bytes (25%) of program storage space. Maximum is 63488 bytes. Global variables use 6160 bytes (75%) of dynamic memory, leaving 2032 bytes for local variables. Maximum is 8192 bytes.

PaulStoffregen commented 6 years ago

You're running out of RAM. Even though it looks like you have 3568 bytes left, unfortunately FastLED is allocating a bunch of RAM with malloc(), which Arduino can't know in advance and show you in this summary.

There is a known bug in Serial1, Serial2 and Serial3 which causes them to consume RAM, even if you don't ever use them. So you might be able to get a little more by editing those files in hardware/teensy/avr/cores/teensy3. You can also try editing usb_desc.h to reduce the number of USB buffers.

Unfortunately, beyond trying to trim other memory usage, there just isn't much anyone can do. FastLED's features need RAM, and the USB code and other infrastructure does too, all functions & interrupts need stack space for their local variables, and this library needs 15 bytes per LED. Teensy LC simply does not have a lot of RAM. To use a lot of LEDs with this non-blocking library, you need more RAM. You'll need to step up to Teensy 3.2 or 3.5 or 3.6.

Or if you can live with the normal blocking I/O, just use FastLED's normal driver.

psitech commented 6 years ago

Hi Paul, thanks for your detailed explanation. Learned a bit again. At this moment, I don't have a specific need for non-blocking I/O, I was just playing with your new library whan I ran into this behaviour. And I just used a Teensy LC since it has this useful 5V output to drive WS2812 LED strips. FastLED's normal driver works for me with driving 144 LEDs.

Regards, Paul