Max7219 + 74HC595

Hello, I'm working on combining 2 projects into one on an Arduino Mega and having problems understanding the difference between Communication with the Max7219 on one LED Matrix and another LED Matrix that uses 74HC595.

I have one (solid Color) Led matrix that uses a Max7219 chip that I got to work perfectly using <LedControl.h>.

And another RGB Matrix that uses 74HC595 and <SPI.h> that I ALSO got to work Perfectly.

However, I cannot get them to work together When Combining the sketches. The SPI (74HC595) works fine, while the other Matrix only lights one led.

I've used <LedControl.h> before in the past with LED matrices with no problem but I've never used <SPI.h> to control one. Is there anything I need to know about how these two Libraries may conflict, or am I just missing something?

The sketch I'm using for the SPI is a modified version of the sketch from this Tutorial which Is the only one I could find on this Particular Matrix I purchased a long time ago.

Can Anyone Help Me combine these to or explain where I may have problems?

74HC595 Changes From blue to red with short Half and half In-between

#include <SPI.h>                  //  include the head file to enable the  library.

const unsigned long eventInterval = 500; // 90000= 1.5 min change value to change chatter time
unsigned long previousTime = 0;
int randomLight;
static uint8_t data[4] = {0x0, 0x0, 0x0, 0x0};         // defined a data matrix
static uint8_t i = 1;                                                    // defined a variable vector
const int CE = 53;                                                      // defined  the CE function pin

void Blue()                                                  // defined a function called "heart big".
{
  int j;
  int x = 2;
  static uint8_t blue[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};    // you need to calculate which led should be light up by this array, it defined line by line on your matrix, for example , 0x00 means the led of the first line is off,  and the 0x66 means the second line's first led will go off and the fourth led will go off and fifth led will go off and eight led will go off. others will turn on....and so on.

  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;       // color red
    data[2] = ~blue[j];             // color blue
    data[1] = 0xFF;            // color green
    data[3] = 0x01 << j ;    // display the data on matrix.
    digitalWrite(CE, LOW);     // when CE is low, it begin to receive data.
    SPI.transfer(data[0]);         //transfer data[0] to the matrix(red)
    SPI.transfer(data[2]);         //transfer data[2] to the matrix(green)
    SPI.transfer(data[1]);        // transfer   data[1] to the matrix(blue)
    SPI.transfer(data[3]);      // tansfer data[3] to the matrix( scanning and display the data on matrix)
    digitalWrite(CE, HIGH);    // when CE is High, means that matrix begin to display the array's information to the matrix.
    delay(x);                          // a little bit delay, let the led light up and stay for a while so that it looks like it brightness.
  }
};

void Red()
{
  int j;
  int x = 2;
  static uint8_t red[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~red[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};
void Green()
{
  int j;
  int x = 2;
  static uint8_t green[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;
    data[2] = 0xFF;
    data[1] = ~green[j];
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void Halfrb()
{
  int j;
  int x = 2;
  static uint8_t HalfR[8] = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  static uint8_t HalfB[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}; // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~HalfR[j];
    data[2] = ~HalfB[j];
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void matrixoff()
{
  int j;
  int x = 2;
  static uint8_t Off[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};        // you can see, all of the led will go off here.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~Off[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void setup() {
  pinMode(CE, OUTPUT);                          //initialized the pin's mode.
  SPI.begin();                                              // start spi function
}

void loop()                                                  //defined a loop function
{
  int m = 10;
  for ( m = 500; m > 0; m--) {                     // make a for loop to let the data displayed on the matrix.
    Blue();
  };
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  };                                         // delay 100 ms
  for ( m = 500; m > 0; m--) {                 // let the data displayed on the matrix
    Red();
  };
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  };
  delay(20);
}

Max7219 Blinks random LEDs

#include <LedControl.h>
LedControl lc = LedControl(47, 49, 45, 1); // din, clk, load, # of matrix (47,49,45,1);
// pin 22 (12) is connected to the MAX7219 pin 1 labelled DIN
// pin 24 (11) is connected to the CLK pin 13 labelled CLK
// pin 26 (10) is connected to LOAD pin 12 labelled as CS
// 1 as we only have 1 MAX 7219 atatched
long randNumberX;
long randNumberY;
void setup()
{
  // the zero refers to the MAX7219 number
  lc.shutdown(0, false); // turn off power saving
  lc.setIntensity(0, 4); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  Serial.begin(9600);
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  randomSeed(analogRead(0));
}
void loop()
{
  randNumberX = random(0, 8);
  randNumberY = random(0, 8);
  lc.setLed(0, randNumberX, randNumberY, true); // turns on LED at col, row
  delay(0);
  lc.setLed(0, randNumberX, randNumberY, false); // turns on LED at col, row
  delay(0);
}

Are these the working sketches for each display? If so please post the combined, non-working sketch.

aarg:
Are these the working sketches for each display? If so please post the combined, non-working sketch.

Yes, independently they work exactly as intended.

With all pins connected, the Max7219 sketch works perfectly with nothing happening on the other display.

However with the SPI matrix, It works exactly as intended but One Led lights up on the second (max7219) Matrix. I I don't know how as those pins should not be used at all. And are not used in SPI (pins 45 47 49) as far as I know.

Combined, nothing happens on the Max matrix, while the SPI matrix works correctly.

#include <SPI.h>                  //  include the head file to enable the  library.
#include <LedControl.h>
LedControl lc = LedControl(47, 49, 45, 1); // din, clk, load, # of matrix

long randNumberX;
long randNumberY;

const unsigned long eventInterval = 500; // 90000= 1.5 min change value to change chatter time
unsigned long previousTime = 0;
int randomLight;
static uint8_t data[4] = {0x0, 0x0, 0x0, 0x0};         // defined a data matrix
static uint8_t i = 1;                                                    // defined a variable vector
const int CE = 53;                                                      // defined  the CE function pin

void Blue()                                                  // defined a function called "heart big".
{
  int j;
  int x = 2;
  static uint8_t blue[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};    // you need to calculate which led should be light up by this array, it defined line by line on your matrix, for example , 0x00 means the led of the first line is off,  and the 0x66 means the second line's first led will go off and the fourth led will go off and fifth led will go off and eight led will go off. others will turn on....and so on.

  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;       // color red
    data[2] = ~blue[j];             // color blue
    data[1] = 0xFF;            // color green
    data[3] = 0x01 << j ;    // display the data on matrix.
    digitalWrite(CE, LOW);     // when CE is low, it begin to receive data.
    SPI.transfer(data[0]);         //transfer data[0] to the matrix(red)
    SPI.transfer(data[2]);         //transfer data[2] to the matrix(green)
    SPI.transfer(data[1]);        // transfer   data[1] to the matrix(blue)
    SPI.transfer(data[3]);      // tansfer data[3] to the matrix( scanning and display the data on matrix)
    digitalWrite(CE, HIGH);    // when CE is High, means that matrix begin to display the array's information to the matrix.
    delay(x);                          // a little bit delay, let the led light up and stay for a while so that it looks like it brightness.
  }
};

void Red()
{
  int j;
  int x = 2;
  static uint8_t red[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~red[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};
void Green()
{
  int j;
  int x = 2;
  static uint8_t green[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;
    data[2] = 0xFF;
    data[1] = ~green[j];
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void Halfrb()
{
  int j;
  int x = 2;
  static uint8_t HalfR[8] = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  static uint8_t HalfB[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}; // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~HalfR[j];
    data[2] = ~HalfB[j];
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void matrixoff()
{
  int j;
  int x = 2;
  static uint8_t Off[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};        // you can see, all of the led will go off here.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~Off[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
};

void setup() {
  pinMode(CE, OUTPUT);                          //initialized the pin's mode.
  SPI.begin();                                              // start spi function
  // the zero refers to the MAX7219 number
  lc.shutdown(0, false); // turn off power saving
  lc.setIntensity(0, 4); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  Serial.begin(9600);
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  randomSeed(analogRead(0));
}

void loop()                                                  //defined a loop function
{
  randNumberX = random(0, 8);
  randNumberY = random(0, 8);
  int m = 10;
  for ( m = 500; m > 0; m--) {                     // make a for loop to let the data displayed on the matrix.
    Blue();
  };
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  };                                         // delay 100 ms
  for ( m = 500; m > 0; m--) {                 // let the data displayed on the matrix
    Red();
  };
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  };
  delay(20);
  lc.setLed(0, randNumberX, randNumberY, true); // turns on LED at col, row
  delay(0);
  lc.setLed(0, randNumberX, randNumberY, false); // turns on LED at col, row
  delay(0);
}

I've been playing around with the code, and placed the Random flashing led matrix on its own void function
outside the loop:

void LogicsRandom() {
  randNumberX = random(0, 8);
  randNumberY = random(0, 8);
  lc.setLed(0, randNumberX, randNumberY, true); // turns on LED at col, row
  delay(0);
  lc.setLed(0, randNumberX, randNumberY, false); // turns on LED at col, row
  delay(0);
}

after some playing around with the Loop, and calling different functions starting with LogicsRandom(); and then adding Blue(); by itself just using delays, I found that every time I added another "color" (ie: Blue(), Red() ext..) the "flashing" or random blinking on the max7219 display simply started to slow down with each color, so that when the whole code is added, it either is slowing it down to nothing, or its intrupting the LogicsRandom() section of the code all together.

Anyone have any thoughts on Why this may be? or ideas on how I could fix it?

#include <SPI.h>                  //  include the head file to enable the  library.
#include <LedControl.h>
LedControl lc = LedControl(47, 49, 45, 1); // din, clk, load, # of matrix

long randNumberX;
long randNumberY;
const unsigned long eventInterval = 500; // 90000= 1.5 min change value to change chatter time
unsigned long previousTime = 0;
int randomLight;
static uint8_t data[4] = {0x0, 0x0, 0x0, 0x0};         // defined a data matrix
static uint8_t i = 1;                                                    // defined a variable vector
const int CE = 53;                                                      // defined  the CE function pin

void setup() {
  pinMode(CE, OUTPUT);                          //initialized the pin's mode.
  SPI.begin();                                              // start spi function
  // the zero refers to the MAX7219 number
  lc.shutdown(0, false); // turn off power saving
  lc.setIntensity(0, 4); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  Serial.begin(9600);
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  randomSeed(analogRead(0));
}

void loop()                                                  //defined a loop function
{
  LogicsRandom();
  int m = 10;
  for ( m = 500; m > 0; m--) {                     // make a for loop to let the data displayed on the matrix.
    Blue();
  }
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  }                                         // delay 100 ms
  for ( m = 500; m > 0; m--) {                 // let the data displayed on the matrix
    Red();
  }
  for ( m = 10; m > 0; m--) {                 // let the data displayed on the matrix
    Halfrb();
  }
}

void Blue()                                                  // defined a function called "heart big".
{
  int j;
  int x = 2;
  static uint8_t blue[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};    // you need to calculate which led should be light up by this array, it defined line by line on your matrix, for example , 0x00 means the led of the first line is off,  and the 0x66 means the second line's first led will go off and the fourth led will go off and fifth led will go off and eight led will go off. others will turn on....and so on.

  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;       // color red
    data[2] = ~blue[j];             // color blue
    data[1] = 0xFF;            // color green
    data[3] = 0x01 << j ;    // display the data on matrix.
    digitalWrite(CE, LOW);     // when CE is low, it begin to receive data.
    SPI.transfer(data[0]);         //transfer data[0] to the matrix(red)
    SPI.transfer(data[2]);         //transfer data[2] to the matrix(green)
    SPI.transfer(data[1]);        // transfer   data[1] to the matrix(blue)
    SPI.transfer(data[3]);      // tansfer data[3] to the matrix( scanning and display the data on matrix)
    digitalWrite(CE, HIGH);    // when CE is High, means that matrix begin to display the array's information to the matrix.
    delay(x);                          // a little bit delay, let the led light up and stay for a while so that it looks like it brightness.
  }
}

void Red()
{
  int j;
  int x = 2;
  static uint8_t red[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~red[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
}
void Green()
{
  int j;
  int x = 2;
  static uint8_t green[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};          // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = 0xFF;
    data[2] = 0xFF;
    data[1] = ~green[j];
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
}

void Halfrb()
{
  int j;
  int x = 2;
  static uint8_t HalfR[8] = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  static uint8_t HalfB[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}; // change the hard to be the smaller one, all you need to do is change this parameter.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~HalfR[j];
    data[2] = ~HalfB[j];
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
}

void matrixoff()
{
  int j;
  int x = 2;
  static uint8_t Off[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};        // you can see, all of the led will go off here.
  for ( j = 0; j < 8; j++)
  {
    data[0] = ~Off[j];
    data[2] = 0xFF;
    data[1] = 0xFF;
    data[3] = 0x01 << j ;
    digitalWrite(CE, LOW);
    SPI.transfer(data[0]);
    SPI.transfer(data[2]);
    SPI.transfer(data[1]);
    SPI.transfer(data[3]);
    digitalWrite(CE, HIGH);
    delay(x);
  }
}
void LogicsRandom() {
  randNumberX = random(0, 8);
  randNumberY = random(0, 8);
  lc.setLed(0, randNumberX, randNumberY, true); // turns on LED at col, row
  delay(0);
  lc.setLed(0, randNumberX, randNumberY, false); // turns on LED at col, row
  delay(0);
}

delay() does what its name says delaying.
samet hing with your for loops.
They count through and only after finishing the counting throughthe commands below are executed.

So this means your code must be changed to

If I understood right you want to display some pattern for some amount of time and only after some time has passed by the pattern should change.

if the two displays should act in parallel and with independent time-constants you have to use the programming-technique of "blink without delay"

the main idea is to run through the loop at highest possible speed and each time the loop runs down to compare timestamps with the actual time and only if a defined amount of time has passed by to execute a pattern-change.

see this guide.
https://forum.arduino.cc/index.php?topic=503368.0

if you are not familiar with this concept it will take some time to understand it because it is very different.

But if you have understood it you will be able to do a blinking of the blue LEDs at let's say two times per second, the red with 5 times per second and switch on/off the green every ten seconds while your other matrix is doing completely different things.
best regards Stefan

StefanL38:
if the two displays should act in parallel and with independent time-constants you have to use the programming-technique of "blink without delay"

the main idea is to run through the loop at highest possible speed and each time the loop runs down to compare timestamps with the actual time and only if a defined amount of time has passed by to execute a pattern-change.

see this guide.
Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

Ok thank you, I've actually used this concept once before to have something happen once every second or so.

Building on what you suggested, I was playing around with a few things and determined that it is the delay(x); at the end of the void Blue() section (same with other colors) that is the major problem.
So Yes I'll need to try and come up with another way to do this.

The delay(x) is needed for the brightness of the led on the display however. I did try it using
while(millis() < currentTime + period); (int period = 1)
but while it did not effect the second display as I wanted, it DID effect the brightness of the Leds, EXCIPT one full line of leds that remained fully lit, just as they were before when the others were off, or that matrix were removed from the sketch.

I'm not sure whats causing that one row to be lit. or how to fully change the sketch to work using millis. as the current Sketch is really the ONLY information on this module I've been able to find. and Ive never used SPI to run an LED matrix.

I'll keep messing around and reading up on it, but any help you can provide would be appreciated!