Memory exceeded

Hello,

I am using an Arduino Uno to drive a Neopixel WS8212b LED matrix 16h x 32w reading images from an external SD Card. When I try to compile and load the code, the compiler tells me the dynamic memory has been exceeded:

"Variáveis globais usam 2278 bytes (111%) de memória dinâmica, deixando -230 bytes para
variáveis locais. O máximo são 2048 bytes.
Not enough memory; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing your footprint.
data section exceeds available space in board"

If I reduce the number of LEDs to 256 it does work however it is not enough for my matrix. I have seen lots of examples in the FastLED library that can go beyond the 512 LEDs, not sure my code is heavier due to the SDFat library?

Any ideas how to optimize the code and avoid having to upgrade to Mega ?

Code:

#include <SdFat.h>
#include <FastLED.h>

#define LED_PIN 6
#define NUM_LEDS 512

CRGB leds[NUM_LEDS];

SdFat SD;
int Delay = 19;
// int Delay = 19; //for 33 fps @ 256 leds !!
int bright = 30;
int r = 0;
int b = 0;
int g = 0;
const int chipSelect = 4;
int count = 0;
int lednum = 0;

void setup() {

  if (!SD.begin(chipSelect)) {
    while (true);
  }

  LEDS.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(bright);
  LEDS.clear();
  LEDS.show();
  delay(Delay);
}

void loop() {

  uint8_t buf[192];
  File dataFile = SD.open("vidbin.bin");

  while (dataFile.available()) {
    int n = dataFile.read(buf, sizeof(buf));

    for (int i = 0; i < n; i++) {

      switch (count) {

        case 0:
          b = buf[i];
          count++;
          break;

        case 1:
          g = buf[i];
          count++;
          break;

        case 2:
          r = buf[i];
          count = 0;
          leds[lednum].setRGB( r, g, b);
          lednum++;

          if (lednum >= NUM_LEDS) {
            FastLED.show();
            lednum = 0;
            delay(Delay);
          }

          //wdt_reset();
          break;

      }

    }

  }

  dataFile.close();

}

Many thanks!!

yes it allocates a 512 byte buffer for the SD card. that's likely your issue.

I think your problem may be that the SD card library needs 512 bytes (i.e. 1/4 of your UNO RAM) to open a file.

Thanks @J-M-L and @markd833 . Do you happen to know of any other lighter SD libraries that can do the trick using buffers still using less memory?
Thanksssss

Probably not as the actual SD card interface transfers 512 byte blocks when it reads or writes from/to the card.

It may be possible when reading an SD card to read 512 bytes and throw away the bytes you're not interested in on the fly but it would be fairly complex to code up.

Move to an Arduino board with more memory.

You can upgrade or consider not using FastLED.
There are a few alternatives that do not use as much memory: WS2812B without fastLED.h - Using Arduino / LEDs and Multiplexing - Arduino Forum

1 Like

Alternative boards with lots more memory are not too expensive…some have the SD card bundled as well (some teensy, some esp32 for example), making it a tighter integration

Thank you @er_name_not_found ! I have now used the FAB_LED lib instead of FastLed, split the matrix into two parts separately wired to the Uno (in order to keep the led array small - within the 2k SRAM) and switch between both strips in the loop code before displaying. It works perfectly. This probably saved me a couple dollars to avoid upgrading to a higher mem board :sunglasses:

1 Like

Sweet. Great job taking the reins and getting it done.

1 Like

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