/*This sketch runs a clock with five LED matrices based on MAX72xx chipset.
An example that shows writing and scrolls text across 4 LED matrices is shown at
http://www.instructables.com/id/Multiple-LED-Matrixes-with-Arduino
This sketch uses the LedControlMS.h (from Makerspace) from the page above, a modificarion of the LEDControl.h library.
This sketch uses the Arduino time & wire libraries, and the DS3131RTC (DS3232RCTC Library)
Sketch assumes the following wiring for the MAX72xx LED matrices--
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
Dataout (middle output pin) is daisy-chained to the DataIn of the next LED for each matrix except the last.
Wiring for the DS3231RTC
VCC and ground are connected to +5VDC and Gnd, respectively
Clock SDA --> Analog 4
SCL --> Analog 5
*/

#include "DS3232RTC.h" // DS3232RTC Clock library that also handles DS3231RTC
#include <Time.h> //Used for clock
#include <Wire.h> //Used for clock
#include "LedControlMS.h" // Note that this is not library LEDControl
int h1, h2, m1, m2; //Each LED matrix will display one number: hour 1&2, minute 1&2. These variables hold the single digits
int s1=2; // LED that counts off the seconds
int hr12; //Used for 12 hour time
int noMatrix=5; //Number of matrices. The library addresses up to 8.
boolean am=true; //To mark am or pm. Initialize as am
LedControl lc=LedControl(12,11,10,noMatrix); //Pin assignments and number of matrices (5)

void setup() {
//First, set the system time to a hard-coded date and time, and then sets the RTC from the system time.
//The setTime() function is part of the Time library.
// Hr Min Sec dd mm yyyy
setTime(23, 45, 00, 10, 2, 2016); //Manually set Arduino clock
RTC.set(now()); //set the RTC from the system time

//Initialize the MAX72XXs (in power-saving mode on startup--wakeup call.
for (int i=0; i< noMatrix; i++){ //For each of the matrices...
lc.shutdown(i,false);
lc.setIntensity(i,1); // Set brightness to a low value
lc.clearDisplay(i); // Clear the display
}
delay(100); // Wait between updates of the display
lc.clearAll();
}

void loop() {
// Set am or pm Boolean (am was initialized as true)
if (hour() > 12)
{am=false;
hr12=hour()-12;
}

/* Isolate hours and minutes, one digit to each LED matrix.
I think a single-digit hour looks better with a blank first digit.
*/
//Place each digit of the hour in its own variable, h1 and h2
if (hr12 < 10)
{
/*
Blank 1st LED matrix if <10 hrs. (62 is a blank character.)
Could also insert a zero, as with minutes.
*/
h1=151;
h2=hr12; //2nd LED displays single digit of hour
}
else
{
//Othewise, just use the two digits of the hour on h1 and h2.
h1=hr12/10; //Integer division by 10 returns the first digit of the hour.
h2=hr12 % 10; //Modulo division by 10 returns the 2nd digit.
}

//Minutes are displayed by m1 and m2
if (minute() < 10)
{
m1=0; //First minute LED d1splays zero
m2=minute(); //2nd digit of minutes
}
else
{
m1=minute()/10;
m2=minute() % 10;
}

/*
Display hours and minutes using the displayChar function. This uses two integer arguments:
the LED matrix number (0-7) and the character to be displayed. Numbers are displayed simply
by passing the number itself as the 2nd argument: that is, lc.displayChar(0,h1)
*/
lc.displayChar(0,h1); //Hours 1 digit to left-most (#0) LED.
lc.displayChar(1,h2); //Hours 2 digit to LED #1
lc.displayChar(3,m1); //Minutes 1 digit to LED #3. (Recall that #2 is used to mark off the seconds.)
lc.displayChar(4,m2); //Minutes 1 digit to #4

/* This routine determines which single LED to light to count off the seconds.
The middle LED counts off the seconds by illuminating a single LED at a time
across each of the 8 rows. Count 8 LEDs across odd rows, and 7 across even rows.
Thus, two rows count 15 seconds.
*/
for (int r=7; r > -1; r--){ //For each of 8 rows, starting from the top (#7)...
for (int c=0; c < 8; c++){ //For each of the 8 leds in a row (0 on left)...

/*
Illuminate dot on current column and row. These seem to be interchanged on my hardware.
I therefore swapped the arguments to the setLed() function below.
Properly, it is addressed as setLed(int led#, int row, int col, boolean).
Variable s1 holds the LED number (counting from 0) of the LED matrix used for this.
*/

if (r%2 == 0 && c==7) //Even row AND col 7
{}
else
lc.setLed(s1,c,r,true); //Turn addressed LED on for 3/4 sec
delay(750);
lc.setLed(s1,c,r,false); //Turn off for 1/4 sec
delay(250);
}
}
}
