/* LED Clock
* by Dan Griffin 11/15
*
* 12:53
* Hour Hour Min Min
* ---- ---- --- ---
* 1 222 55 333
* 1 222 55 333
* 1 222 55 333
*
* Randomly light LEDs depicting the numeric value
* of the current time every second.
*
* Column anodes + (HIGH)
* Row anodes -(LOW)
*/
#include <Wire.h> // RTC Clock.
#include "RTClib.h" // RTC Clock.
// LED arrays - [ON/OFF][Row][Column].
int intLed[27] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
const int conRow[27] = {2,3,4,2,3,4,2,3,4,2,3,4,2,3,4,2,3,4,2,3,4,2,3,4,2,3,4};
const int conCol[27] = {5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13,13};
int xCount; // Temp counter for how many LEDs are set to ON.
unsigned long startTime; // Timer to insure 1 second each of ON then OFF.
long x; // Temp val for random number generated.
int aTime[5] = {1,2,3,4}; // Array of current time digits (12:34).
int iSec = 0; // Temp val for change in seconds.
RTC_DS1307 RTC; // Create RTC object.
// ------------------------------------------------------------------------------------------------
void setup() {
pinMode( 2, OUTPUT); // Rows (3)...
pinMode( 3, OUTPUT); // Anode (+).
pinMode( 4, OUTPUT);
pinMode( 5, OUTPUT); // Colums (9)...
pinMode( 6, OUTPUT); // Cathode (-).
pinMode( 7, OUTPUT);
pinMode( 8, OUTPUT);
pinMode( 9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, LOW); // Make sure all LEDs are OFF.
digitalWrite(3, LOW);
digitalWrite(4, LOW);
for(int i = 5; i < 27; i++) { digitalWrite(i, HIGH); }
pinMode(16, OUTPUT); // Pin A4 (- GND) for RTC.
pinMode(17, OUTPUT); // Pin A3 (+ 5v) for RTC.
analogWrite(16,0); // Set pin A4 LOW.
analogWrite(17, 255); // Set pin A3 HIGH.
Serial.begin(9600); // Only used to display error.
RTC.begin(); // Initialize RTC in 24 hour format.
// RTC.adjust(DateTime(__DATE__, __TIME__)); // Sets RTC to your computer's time at the moment of upload to Uno.
}
// ------------------------------------------------------------------------------------------------
void loop() {
DateTime dTime = RTC.now(); // Get the current time.
if(dTime.second() != iSec) {
iSec = dTime.second(); // Reset second temp val.
for(int i = 0; i < 27; i++) { intLed[i] = 0; } // Zero out the LED array.
aTime[0] = char(int(dTime.hour() /10)); // Store the time digits.
aTime[1] = char(int(dTime.hour() -(aTime[0] *10)));
aTime[2] = char(int(dTime.minute() /10));
aTime[3] = char(int(dTime.minute() -(aTime[2] *10)));
// Hours (high).
xCount = 0; // Reset the counter.
while(xCount < aTime[0]) { // Randomly set LED array to display "tens" of hours.
x = random(0,3);
if(intLed[x] == 0) { // If we haven't already set this one...
intLed[x] = 1; // ...set it to ON.
xCount++ ; // Keep track of how many have been set.
}
}
// Hours (low).
xCount = 0; // Reset the counter.
while(xCount < aTime[1]) { // Randomly set LED array to display "ones" of hours.
x = random(3,12);
if(intLed[x] == 0) { // If we haven't already set this one...
intLed[x] = 1; // ...set it to ON.
xCount++ ; // Keep track of how many have been set.
}
}
// Minutes (high).
xCount = 0; // Reset the counter.
while(xCount < aTime[2]) { // Randomly set LED array to display "tens" of minutes.
x = random(12,18);
if(intLed[x] == 0) { // If we haven't already set this one...
intLed[x] = 1; // ...set it to ON.
xCount++ ; // Keep track of how many have been set.
}
}
// Minutes (low).
xCount = 0; // Reset the counter.
while(xCount < aTime[3]) { // Randomly set LED array to display "ones" of minutes.
x = random(18,27);
if(intLed[x] == 0) { // If we haven't already set this one...
intLed[x] = 1; // ...set it to ON.
xCount++ ; // Keep track of how many have been set.
}
}
}
// ------------------------------------------------------ Light LEDs.
for(int i = 0; i < 27; i++) {
if(intLed[i] == 1) { // IF we need to light this LED...
digitalWrite(conRow[i], HIGH); // ...ground the row (cathode)...
digitalWrite(conCol[i], LOW ); // ...and power the col (anode).
delay(1); // (0 = dim / 2 = flicker)
digitalWrite(conRow[i], LOW ); // Turn it OFF.
digitalWrite(conCol[i], HIGH);
}
}
}