/*This sketch runs an hours-minutes-seconds binary clock with one LED matrix based on MAX72xx chipset and DS3231 RTC (Real time clock). Written for an Arduino nano clone. This runs a 24-hour display (i.e. not am/pm). Wiring for the MAX72xx LED matrix: different from library examples. Pin D10 is connected to the DIN (DataIn) Pin D11 is connected to CS (LOAD) Pin D12 is connected to the CLK DS3231RTC (I2C bus) Nano A4 to sca, A5 to scl. */ #include #include #include #include "LedControl.h" //Pin definition constants const int mDataIn=12, mLoad=11, mClk=10; //Max7219 LED matrix int h1, h2, m1, m2, s1, s2; // Each digit int tChange; //Scratch used during change time via pushbutton // Matrix library instance LedControl lc=LedControl(mDataIn, mClk, mLoad,1); //Pin assignments and number of matrices (1) void setup() { // Initialize pushbutton pins as input: // Remove comments to set the system time to a hard-coded date and time, // then set the RTC from the system time. // Hr Min Sec dd mm yyyy // setTime(19, 43, 30, 8, 12, 2014); // RTC.set(now()); //set the RTC from the system time // Notify Arduimo that the RTC is the external time provider setSyncProvider(RTC.get); //Initialize the MAX72XX in power-saving mode. lc.shutdown(0,false); lc.setIntensity(0,1); // Set brightness to a low value lc.clearDisplay(0); // Clear the display delay(100); // Wait after initializing display Serial.begin(9600); } void loop() { //Isolate hours, minutes, seconds, one digit to each LED matrix column. h1=n1(hour()); h2=n2(hour()); m1=n1(minute()); m2=n2(minute()); s1=n1(second()); s2=n2(second()); // Cast as (change integer to) byte, write to LED row. // (Row runs from pin to pin on model used.) lc.setRow(0,7,byte(h1)); lc.setRow(0,6,byte(h2)); lc.setRow(0,4,byte(m1)); lc.setRow(0,3,byte(m2)); lc.setRow(0,1,byte(s1)); lc.setRow(0,0,byte(s2)); } int n1(int num) // Function to isolate first digit of 2-digit integer { num = num / 10; //Integer division by 10 (discard remainder) return num; } int n2(int num) // Function to isolate second digit of 2-digit integer { num = num % 10; //Modulo division by 10 (keep remainder only) return num; }