// Binary clock for a 4x4 LED matrix
#include <Time.h>
uint8_t columns[4] = {13,12,11,10};
uint8_t rows[4] = {9,8,7,6};
uint8_t matrix[4][4] = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 }
};
void setup() {
// Init the output pins
for (int i = 0; i < 4; i++) {
pinMode(rows[i], OUTPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(columns[i], OUTPUT);
}
setClock(17,12); // Set the current time here
}
void loop() {
setMatrix(); // Sets the matrix arrays to the current time in binary
showMatrix(); // Does the multiplexing for the LED matrix
}
void showMatrix() {
// Parse all rows
for (int x = 0; x < 4; x++) {
// Set previous row to 0 and
// matrix value on the corresponding pin
for (int y = 0; y < 4; y++) {
digitalWrite(rows[y], !matrix[x][y]);
}
// Set current column to 1
digitalWrite(columns[x], 1);
delay(1);
// Delete current column
digitalWrite(columns[x], 0);
}
}
void setMatrix() {
int munit = minute()%10; //sets the variable munit and hunit for the unit digits
int hunit = hour()%10;
//minutes units
if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) { matrix[3][3] = 1; } else { matrix[3][3] = 0; }
if(munit == 2 || munit == 3 || munit == 6 || munit == 7) { matrix[2][3] = 1; } else { matrix[2][3] = 0; }
if(munit == 4 || munit == 5 || munit == 6 || munit == 7) { matrix[1][3] = 1; } else { matrix[1][3] = 0; }
if(munit == 8 || munit == 9) { matrix[0][3] = 1;} else { matrix[0][3] = 0; }
//minutes
if((minute() >= 10 && minute() < 20) || (minute() >= 30 && minute() < 40) || (minute() >= 50 && minute() < 60)) { matrix[3][2] = 1; } else {matrix[3][2] = 0;}
if(minute() >= 20 && minute() < 40) {matrix[2][2] = 1;} else {matrix[2][2] = 0;}
if(minute() >= 40 && minute() < 60) {matrix[1][2] = 1;} else {matrix[1][2] = 0;}
//hour units
if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {matrix[3][1] = 1;} else {matrix[3][1] = 0;}
if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {matrix[2][1] = 1;} else {matrix[2][1] = 0;}
if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {matrix[1][1] = 1;} else {matrix[1][1] = 0;}
if(hunit == 8 || hunit == 9) {matrix[0][1] = 1;} else {matrix[0][1] = 0;}
//hour
if(hour() >= 10 && hour() < 20) {matrix[3][0] = 1;} else {matrix[3][0] = 0;}
if(hour() >= 20 && hour() < 24) {matrix[2][0] = 1;} else {matrix[2][0] = 0;}
}
void setClock(int hour, int minute) { // Helper method to set time more easily (seconds and date can be ignored for this program).
setTime(hour,minute,00,00,00,2000);
}