Arrays and problems oh my

I recently bought a Uno R4 and having quite a fun time learning C++ programming for it. I have a few questions about arrays and how to use them correctly...or most efficiently. And a general problem question or 2.

Some background...
I have max7200 LED matrix board with 4 modules for a total of 256 LED's. I ran the max7200 example 'Hardware Mapper" program and it reported I had an ICStation module.

I also found an open source software called LED Matrix Studio which I used to design my animation.

I designed a simple little eye wink animation in Matrix Studio. I did so with a setting of 32x8 since that's what my module is. It gave me the following array code. I had 4 steps in the array to show the eye closing. I'm only posting 1 for simplicity....

0x00, 0x70, 0x0E, 0x00, 0x00, 0x88, 0x11, 0x00, 0x01, 0x24, 0x24, 0x80, 0x02, 0x72, 0x4E, 0x40, 0x02, 0x72, 0x4E, 0x40, 0x01, 0x24, 0x24, 0x80, 0x00, 0x88, 0x11, 0x00, 0x00, 0x70, 0x0E, 0x00  //  1

So when I plugged all 32 of those values into the array like this...

byte winkLFrame1[32] = { 0x00, 0x70, 0x0E, 0x00, 0x00, 0x88, 0x11, 0x00, 0x01, 0x24, 0x24, 0x80, 0x02, 0x72, 0x4E, 0x40, 0x02, 0x72, 0x4E, 0x40, 0x01, 0x24, 0x24, 0x80, 0x00, 0x88, 0x11, 0x00, 0x00, 0x70, 0x0E, 0x00 }; // Left eye frame 1

and set up the for loop to go through all 32 parts of the array line it didn't work right. So I did a new design using an 8x8 setting in Matrix Studio and that worked. Here is my working code. I'm not sure why it wouldn't work with the 32 byte long array lines. It would have been much easier. Could anyone point me to where I might have went wrong? Also looking at my code can I get some pointers as to a better way to do those arrays? Is it better to put them in a .h file? My end goal is it have several animations and you press a different button on the breadboard to fire off a particular animation. So that'll be a lot of lines of array code in my main program. I don't know if it would be that big a deal with the R4 since it has so much more memory, but I'd like to learn good habits now instead of trying to break bad ones later.

Sorry for the long post. Just hoping to cram as much learning into one post as I can :slight_smile:

#include <MD_MAX72xx.h>
#include <SPI.h>
#define delay_blink 75        // how long to wait between steps in the wink animation
#define delay_look 75         // how long to wait between steps in the look animation
#define delay_betweenAni 1000  // how long to wait between animation sets

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

// CLK Pin  > 13 CLK
// Data Pin > 11 DIN
// CS Pin   > 10 CS

#define CS_PIN 10

// Hardware SPI connection
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// Left eye wink animation
byte winkLFrame1[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 }; // Left eye frame 1
byte winkLFrame2[8] = { 0x00, 0x3C, 0x42, 0x99, 0x99, 0x42, 0x3C, 0x00 }; // Left eye frame 2
byte winkLFrame3[8] = { 0x00, 0x00, 0x7E, 0x99, 0x99, 0x7E, 0x00, 0x00 }; // Left eye frame 3
byte winkLFrame4[8] = { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00 }; // Left eye frame 4
byte winkLFrame5[8] = { 0x00, 0x00, 0x7E, 0x99, 0x99, 0x7E, 0x00, 0x00 }; // Left eye frame 5
byte winkLFrame6[8] = { 0x00, 0x3C, 0x42, 0x99, 0x99, 0x42, 0x3C, 0x00 }; // Left eye frame 6
byte winkLFrame7[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 }; // Left eye frame 7

// Right eye doesn't blink
byte winkRFrame1[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame2[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame3[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame4[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame5[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame6[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };
byte winkRFrame7[8] = { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 };

void setup() {
  mx.begin();
  mx.control(MD_MAX72XX::INTENSITY, 3);
  mx.clear();
}

void loop() {
  drawWink();
  delay(delay_betweenAni);
}

// Wink animation
void drawWink() {
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame1[i]);
    mx.setRow(2, 2, i, winkRFrame1[i]);
    }
  delay(delay_blink);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame2[i]);
    mx.setRow(2, 2, i, winkRFrame2[i]);
    }
    delay(delay_blink);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame3[i]);
    mx.setRow(2, 2, i, winkRFrame3[i]);
    }
  delay(delay_blink);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame4[i]);
    mx.setRow(2, 2, i, winkRFrame4[i]);
    }
  delay(1000);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame5[i]);
    mx.setRow(2, 2, i, winkRFrame5[i]);
    }
  delay(delay_blink);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame6[i]);
    mx.setRow(2, 2, i, winkRFrame6[i]);
    }
  delay(delay_blink);
  for (int i = 0; i <= 8; i++) {
    mx.setRow(1, 1, i, winkLFrame7[i]);
    mx.setRow(2, 2, i, winkRFrame7[i]);
    }
  delay(delay_blink);
}
for (int i = 0; i <= 8; i++)

Try

for (int i = 0; i < 8; i++)

Hi Larry. Thanks for the tip. Now that I think about it I guess the = is kind of useless there lol.

  • You will be going out of the array's bounds.
1 Like

When I was trying to use the 32 byte array so the animation would fit to my entire 4 module matrix it didn't work at all. I got really strange results. It's like the eyes were halfway off the bottom of the screen. I had the for loop set up like...

for (int i = 0; i <= 32; i++) {
    mx.setRow(1, 1, i, winkLFrame1[i]);
    }

I did have the = in there. Do you think something as simple as removing that from the statement would have fixed my problem? My brain is hurting at this point so I'm going to wait until tomorrow to re-write that to check for myself if I don't hear back.


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

  • You have 32 elements in your array, you then iterating thru 33, big problem here.
1 Like

look this over -- less redundant code

# include <MD_MAX72xx.h>
# include <SPI.h>

#define delay_blink 75
#define delay_look  75
#define delay_betweenAni 1000  // how long to wait between animation sets

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

// CLK Pin  > 13 CLK
// Data Pin > 11 DIN
// CS Pin   > 10 CS

#define CS_PIN 10

// Hardware SPI connection
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

const int NFr = 8;

byte winkLFrame [][NFr] = {
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x00, 0x3C, 0x42, 0x99, 0x99, 0x42, 0x3C, 0x00 },
    { 0x00, 0x00, 0x7E, 0x99, 0x99, 0x7E, 0x00, 0x00 },
    { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00 },
    { 0x00, 0x00, 0x7E, 0x99, 0x99, 0x7E, 0x00, 0x00 },
    { 0x00, 0x3C, 0x42, 0x99, 0x99, 0x42, 0x3C, 0x00 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
};

byte winkRFrame [][NFr] = {
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
    { 0x18, 0x24, 0x42, 0x99, 0x99, 0x42, 0x24, 0x18 },
};

const int NFrames = sizeof(winkRFrame)/(NFr);

unsigned long delays [NFrames] = {
    delay_blink, delay_blink, delay_blink, delay_blink,
   1000,         delay_blink, delay_blink };

// -----------------------------------------------------------------------------
void setup() {
    mx.begin();
    mx.control(MD_MAX72XX::INTENSITY, 3);
    mx.clear();
}


void loop() {
    drawWink();
    delay(delay_betweenAni);
}


// Wink animation
void drawWink() {
    int n;
    for (n = 0; n < NFrames; n++)  {
        for (int i = 0; i < NFr; i++) {
            mx.setRow(1, 1, i, winkLFrame [n][i]);
            mx.setRow(2, 2, i, winkRFrame [n][i]);
        }
        delay(delay_blink);
    }
}

@SlimPikkins
Your count starts at "0" as element 1 and stops before ( < ) counting the "Nth" element.

Haha, far from useless… dangerously bad. Messing with array elements that are not there can cause almost anything to happen almost anywhere in the code.

It can make for errors that are very hard to figure out, as they may crop up in seemingly unrelated places, and change with the slightest edits to the code.

Improper array access is one of the few things that we look for immediately just because.

a7