Ran out of memory trying to display image on LEDs

I'm trying to display a 16x16 rgb image using a LED matrix I have. I'm running out of memory though and was wondering if there was a way to stop the colors array from loading into RAM? Or if there was any other way to do this without using extra memory.

#include <FastLED.h>

#define LED_PIN     5
#define NUM_LEDS    256
#define BRIGHTNESS 10
const int colors[] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 233, 236, 236, 231, 234, 234, 231, 234, 234, 231, 234, 234, 233, 236, 236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 218, 219, 219, 57, 38, 38, 41, 21, 21, 41, 21, 21, 41, 21, 21, 57, 38, 38, 218, 219, 219, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 253, 253, 218, 219, 219, 75, 36, 36, 216, 0, 0, 234, 0, 0, 231, 0, 0, 231, 0, 0, 213, 0, 0, 72, 36, 36, 218, 219, 219, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 236, 239, 239, 54, 36, 35, 192, 0, 0, 234, 0, 0, 240, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 213, 0, 0, 54, 36, 36, 211, 217, 217, 233, 236, 236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 253, 253, 218, 219, 219, 25, 33, 35, 42, 22, 20, 28, 18, 21, 38, 7, 19, 218, 0, 2, 253, 0, 0, 255, 0, 0, 236, 0, 0, 22, 0, 0, 38, 19, 19, 58, 38, 38, 220, 219, 219, 253, 253, 253, 255, 255, 255, 234, 232, 231, 40, 51, 57, 58, 169, 217, 186, 219, 238, 57, 177, 238, 0, 80, 218, 34, 5, 20, 235, 0, 0, 255, 0, 0, 231, 0, 0, 39, 0, 0, 209, 0, 0, 210, 0, 0, 56, 38, 38, 233, 237, 237, 255, 255, 255, 234, 233, 231, 39, 45, 57, 8, 82, 217, 19, 96, 238, 4, 91, 238, 0, 74, 218, 35, 6, 20, 235, 0, 0, 255, 0, 0, 232, 0, 0, 38, 0, 0, 197, 1, 1, 196, 0, 0, 35, 18, 18, 232, 235, 235, 255, 255, 255, 253, 253, 253, 218, 218, 220, 20, 24, 38, 12, 5, 21, 16, 6, 21, 38, 6, 19, 219, 1, 2, 254, 0, 0, 252, 0, 0, 228, 0, 0, 37, 0, 0, 190, 1, 1, 190, 0, 0, 37, 21, 21, 232, 235, 235, 255, 255, 255, 255, 255, 255, 235, 238, 237, 36, 20, 18, 171, 0, 0, 196, 0, 0, 229, 0, 0, 254, 0, 0, 248, 0, 0, 217, 1, 1, 191, 1, 1, 33, 0, 0, 189, 1, 1, 189, 0, 0, 34, 18, 18, 232, 235, 235, 255, 255, 255, 255, 255, 255, 232, 235, 235, 37, 21, 21, 189, 0, 0, 214, 1, 1, 214, 1, 1, 195, 0, 0, 211, 1, 1, 210, 1, 1, 187, 1, 1, 29, 0, 0, 140, 0, 0, 141, 0, 0, 50, 38, 38, 235, 237, 237, 255, 255, 255, 255, 255, 255, 232, 235, 235, 37, 21, 21, 187, 0, 0, 210, 1, 1, 177, 3, 3, 61, 17, 17, 177, 3, 3, 210, 1, 1, 190, 1, 1, 20, 2, 2, 30, 19, 19, 50, 38, 38, 220, 219, 219, 253, 253, 253, 255, 255, 255, 255, 255, 255, 232, 235, 235, 34, 18, 18, 192, 0, 0, 194, 0, 0, 50, 19, 19, 191, 196, 196, 50, 19, 19, 194, 0, 0, 192, 0, 0, 33, 17, 17, 211, 215, 215, 234, 236, 236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 234, 237, 237, 55, 41, 41, 174, 2, 2, 174, 2, 2, 57, 44, 44, 215, 221, 221, 57, 44, 44, 174, 2, 2, 174, 2, 2, 55, 41, 41, 237, 239, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 253, 253, 217, 215, 215, 32, 16, 16, 32, 16, 16, 217, 216, 216, 251, 251, 251, 217, 216, 216, 32, 16, 16, 32, 16, 16, 217, 215, 215, 253, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
CRGB leds[NUM_LEDS];

void setup() {
  //Serial.begin(9600);
  delay(1000);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  for(int i = 0; i < NUM_LEDS*3; i = i + 3){
    leds[i%3] = CRGB(colors[i], colors[i+1], colors[i+2]);
  }
  FastLED.show();
}

void loop() {

}```
const int colors[]

What is the maximum value that will be held in this array ?

PROGMEM - Arduino Reference

1 Like

I tried doing this just now but when I look at the serial monitor the values are all over the place, in the thousands and stuff.

#include <FastLED.h>

#define LED_PIN     5
#define NUM_LEDS    256
#define BRIGHTNESS  5
const int colors[NUM_LEDS * 3] PROGMEM = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(9600);
  delay(1000);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  for(int i = 0; i < NUM_LEDS*3; i = i + 3){
    leds[i/3] = CRGB(colors[i], colors[i+1], colors[i+2]);
    for(int j = 0; j < 3; j++){
      Serial.print(colors[i+j]);
      Serial.print(" ");
    }
    Serial.println();
    FastLED.show();
  }
}

void loop() {
  delay(50);
}

768

PROGMEM stores data in Flash Memory, not SRAM. It does not work exactly the same as an array would.

Instead of doing colors[SomeIndex]
you would do pgm_read_word_near(colors + SomeIndex);

so in your code from Post #4, instead of Serial.print(colors[i+j]);
you would do
Serial.print(pgm_read_word_near(colors + i + j));

Read the reference more closely if you want to learn more about PROGMEM:

note:
pgm_read_word_near is for two-byte "int's" while pgm_read_byte_near is for one-byte "byte's"
In your case, if you dont need number larger than 255 or lower than 0, then use byte instead of int here:
const int colors[NUM_LEDS * 3] PROGMEM = {255, 255, ...}
const byte colors[NUM_LEDS * 3] PROGMEM = {255, 255, ...}

note2: also you dont need to set the size of PROGMEM, you anyway cant change the values while running, so this is better:
const byte colors[] PROGMEM = {255, 255, ...}

1 Like

Thanks, this worked, I still needed the input to the function to be ints though so I ended up just using an int array b/c I was too lazy to cast.

Your Welcome and good luck with your project :slightly_smiling_face:

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