Problem displaying sprites on 16x16 LED Matrix

I’ve purchased a 16 x 16 ws2812 LED matrix and am just starting to play with it. I’m using the Adafruit NeoPixel library to drive the data pin on the Arduino, and their example program works perfectly. So I thought about displaying some old arcade sprite animations on it since those are also 16x16. So I grabbed the three sprites that make up the pacman animation: mouth all the way open, mouth halfway open, and mouth closed. I converted them into three compilable arrays, and am trying to write a very simple program to animate them, but I’m not having much luck. Here’s a code snippet. I tried to include all the code, but that pushed me over the character limit for a message, so I attached the .ino file as well:

#include <Adafruit_NeoPixel.h>
#define PIN 8

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(256, PIN, NEO_RGB + NEO_KHZ800);

unsigned long frame0[256] = {
 // see attached .ino file for initialization
};
unsigned long frame1[256] = {
 // see attached .ino file for initialization
};
unsigned long frame2[256] = {
 // see attached .ino file for initialization
};

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  displayFrame0();
  delay(80);
  displayFrame1();
  delay(80);
  displayFrame2();
  delay(80);
}

// Fill the frames
void displayFrame0()
{
  for (int i=0; i < 256; i++)
  {
    strip.setPixelColor(i, frame0[i]);
  }
  strip.show();
}

void displayFrame1()
{
  for (int i=0; i < 256; i++)
  {
    strip.setPixelColor(i, frame1[i]);
  }
  strip.show();
}

void displayFrame2()
{
  for (int i=0; i < 256; i++)
  {
    strip.setPixelColor(i, frame2[i]);
  }
  strip.show();
}

So in loop(), I’m attempting to cycle through the three frames using displayFrame0(), displayFrame1(), & displayFrame2() calls. If all three lines are compiled in, nothing happens on the matrix. If I comment out any two of three displayFrameX() calls, I see the one remaining frame, which is the correct behavior at that point of course, but it’s kind of boring! Any ideas what can be happening? When I compile the sketch with everything uncommented, it comes in at 5,962 bytes (of a 32,256 byte maximum), so it doesn’t seem like it should be a memory problem, but maybe it’s a stack vs. heap problem? I’m really confused…

I’m using just a stock Arduino Uno with no shields.

pacman.ino (8.33 KB)

Got a working solution, so I thought I'd post back to help anyone out that stumbles across this. This forum thread: Question about memory management. - #3 by system - Programming Questions - Arduino Forum points to You know you have a memory problem when... | Memories of an Arduino | Adafruit Learning System, which explains static ram, stack and heap quite well, and recommends that in my case, I should make sure my constant data is in program memory instead of static RAM. This is accomplished by adding the PROGMEM keyword when declaring the variable and then using the pgm_read_x() functions. So changing my sprite declarations from const unsigned long frame0[256]  = {...};
to
const unsigned long frame0[256] PROGMEM = {...}; and then changing my reads from strip.setPixelColor(i, frame0[i]);
to
strip.setPixelColor(i, pgm_read_dword(&frame0[i])); fixed everything. And still leaves me 27k of memory to add more sprites...

Try this: GitHub - masteruan/NeoPixel-Matrix-Sprite: Sprite animation for led matrix WS based on Adafruit_NeoPixel, Adafruit_NeoMatrix, Adafruit_GFX Library.

HI Masteruan,

I have tried to download your files in Github but I haven't found the .ino file.

Thanks!