#define CLK 14
#define DIN 13
#define CS  15

#define NUM_MAX7219 2


void shiftAll(byte send_to_address, byte send_this_data)
{
  digitalWrite(CS, LOW);
  for (int i = 0; i < NUM_MAX7219; i++) {
    shiftOut(DIN, CLK, MSBFIRST, send_to_address);
    shiftOut(DIN, CLK, MSBFIRST, send_this_data);
  }
  digitalWrite(CS, HIGH);
}


void setup() {
  Serial.begin(115200);
  pinMode(CLK, OUTPUT);
  pinMode(DIN, OUTPUT);
  pinMode(CS, OUTPUT);

  // Setup each MAX7219
  shiftAll(0x0f, 0x00); //display test register - test mode off
  shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
  shiftAll(0x0c, 0x01); //shutdown register - normal operation
  shiftAll(0x0a, 0x0f); //intensity register - max brightness
  shiftAll(0x09, 0x00); //decode mode register - No decode
}

byte data [] = {1, 2, 4, 8, 16, 32 , 64, 128, 255};

void show() {
  byte index = 0;
  for (byte row = 1; row < 9; row++) {
    digitalWrite(CS, LOW);
    for (byte num = 0; num < NUM_MAX7219; num++) {
      shiftOut(DIN, CLK, MSBFIRST, row);
      shiftOut(DIN, CLK, MSBFIRST, data[(index+millis()/48)%9]);
    }
    index++;
    digitalWrite(CS, HIGH);
  }
}

void loop() {
  show();
  delay(50);
}