Storing an led array for matrix

Hi, im new to the ardiuno and just getting to grasps with it, this is my first program and learning along the way, i have created a 49x18 led matrix using led strips which is all working fine, i have created 2 seperate led array patterns (will be creating more to add) but they are stored globally, where should i be storing these as its using most of my memory, im running an arduino mega, the arrays don’t need to be changed as they are static and won’t be changing during run time

“Global variables use 7308 bytes (89%) of dynamic memory”

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUM_LEDS 882

#include "GAME2.h"
#include "GAME3.h"

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

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

      Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    
    switch (inByte) {
      
      case 'a':
      for (int t = 0; t < NUM_LEDS; t++)
    {
      strip.setPixelColor(t, leddataindex[ledarray[t]]); 
    }
    strip.show();
        break;
        
      case 'b':
      for (int t = 0; t < NUM_LEDS; t++)
    {
      strip.setPixelColor(t, leddataindex2[ledarray2[t]]); 
    }
    strip.show();
        break;
        
        
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 7; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
}
uint32_t leddataindex[] = {
0x00000000, 0x000000FF, 0x00FF0000, 0x00FFFFFF};

uint32_t ledarray[] = {
                       3, 3, 3, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 
                       0, 3, 3, 3, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 3, 3, 3, 
                       3, 3, 3, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 
                       0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
                       1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 
                       0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
                       2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
                       0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 
                       2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
                       0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
                       1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 
                       0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
                       2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
                       0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 
                       2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
                       0, 3, 3, 3, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 
                       3, 3, 3, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 3, 3, 3, 0, 
                       0, 3, 3, 3, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 
                       };
uint32_t leddataindex2[] = {
0x000000FF, 0x0000FF00, 0x0000FF40, 0x008000FF, 0x00FF0000};

uint32_t ledarray2[] = {
                       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                       1, 1, 0, 0, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 1, 2, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 3, 3, 3, 
                       3, 3, 3, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 2, 1, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4, 0, 0, 1, 1, 
                       1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 1, 
                       1, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 
                       1, 1, 0, 0, 1, 1, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 
                       1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 1, 1, 0, 0, 1, 1, 
                       1, 1, 4, 4, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 
                       1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 4, 4, 1, 1, 
                       1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 1, 
                       1, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 
                       1, 1, 4, 4, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 
                       1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 4, 4, 1, 1, 
                       1, 1, 0, 0, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 
                       1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 4, 4, 1, 1, 1, 1, 0, 0, 1, 1, 
                       3, 3, 4, 4, 1, 1, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 
                       1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 4, 4, 0, 0, 4, 4, 1, 1, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 0, 0, 4, 4, 0, 0, 1, 1, 1, 1, 4, 4, 3, 3, 
                       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                       };

GAME_SKETCH_with_button_2.ino (1.13 KB)

GAME2.h (3.15 KB)

GAME3.h (3.17 KB)

Why, oh, why would you use 32 bits to store only 3?

uint32_t ledarray[] = {
                       3, 3, 3, 1, 1, 1

use

uint8_t ledarray[] = {
                       3, 3, 3, 1, 1, 1

This was just the compiled array code taken from led matrix studio, I have very basic coding knowledge, just taken this up as a hobby so just learning along the way

w4ynz:
This was just the compiled array code taken from led matrix studio, I have very basic coding knowledge, just taken this up as a hobby so just learning along the way

It's not compiled code. For the learning part, do you understand my recommendation? Did you try it out? The mistake is in the matrix, not your code so it's on them.

Do you realize that the gates to skilled programming have burned down and the town is on fire with pretenders flaunting the worst C code in history? :slight_smile:

Compiled/exported code. Yeah I get your recommendations, not had a chance to try it out as not on pc at moment currently browsing on the phone.

So where I have the arrays stored is this best practise? I just need to change them to 8 bits

w4ynz:
So where I have the arrays stored is this best practise? I just need to change them to 8 bits

It depends how fast you need to access them. You can put them in PROGMEM (flash memory) where there is much more room, but it is a little bit slower to read. It's definitely worth considering.

Thanks will defently have a read up on that,doesn't need to be paticually fast for what I'm trying to achieve at the moment, but this is just a small part of a bigger overall program so will defently come in handy knowing the difference

I will change my array code tomorrow and let you know how I get on

Thanks for your help

aarg:
Why, oh, why would you use 32 bits to store only 3?

uint32_t ledarray[] = {

3, 3, 3, 1, 1, 1



use


uint8_t ledarray = {
                      3, 3, 3, 1, 1, 1

Thanks this worked, got it down to (24%) of dynamic memory, now i just need to learn about storing and recieving in the flash memory