Arduino Based Desk Clock

Its kinda sloppy but let me know if you have any questions:

include "LedControl.h"

include <avr/pgmspace.h>

//RTC

include "Wire.h"

define DS3231_I2C_ADDRESS 0x68

//char set fullsize byte nmbrChar[]={ B11111111,//0 B10000001, B11111111, B00000000,//1 B11111111, B00000000, B11111001,//2 B10001001, B10001111, B10001001,//3 B10001001, B11111111, B00001111,//4 B00001000, B11111111, B10001111,//5 B10001001, B11111001, B11111111,//6 B10001000, B11111000, B00000001,//7 B11110001, B00001111, B11111111,//8 B10001001, B11111111, B00001111,//9 B00001001, B11111111, }; /* smaller font byte nmbrChar[]={ B01111110,//0 B01000010, B01111110, B00000000,//1 B01111110, B00000000, B01111010,//2 B01001010, B01001110, B01001010,//3 B01001010, B01111110, B00001110,//4 B00001000, B01111110, B01001110,//5 B01001010, B01111010, B01111110,//6 B01001000, B01111000, B00000010,//7 B01110010, B00001110, B01111110,//8 B01001010, B01111110, B00001110,//9 B00001010, B01111110, }; */

byte decToBcd(byte val) { return( (val/1016) + (val%10) ); } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) { return( (val/1610) + (val%16) ); }

boolean ticState=true; unsigned long ticToc=0;

/* DIN, CLK, LOAD/CS, # of Devices (max 8) */ LedControl lc=LedControl(13,11,12,2);

void setup() { Wire.begin(); Serial.begin(9600);

// Sets time for DS3231 seconds, minutes, hours, day(Sun[1]-Sat[7]), date, month, year //setDS3231time(00,35,05,3,20,1,15);

int devices=lc.getDeviceCount(); for(int address=0;address<devices;address++) { /The MAX72XX is in power-saving mode on startup/ lc.shutdown(address,false); /* Set the brightness to a medium values / lc.setIntensity(address,1); / and clear the display */ lc.clearDisplay(address); } }

void loop() { if ((millis()-ticToc)>1000){ ticToc=millis(); if (ticState){ sTic(); ticState=!ticState; } else{ sToc(); ticState=!ticState; } } displayTime(); }

void displayNum (byte spot, byte dspNum){ //number in 0-3 spots switch (spot){ case 0: lc.setRow(0,0,nmbrChar[0+dspNum3]); lc.setRow(0,1,nmbrChar[1+dspNum3]); lc.setRow(0,2,nmbrChar[2+dspNum3]); break; case 1: lc.setRow(0,4,nmbrChar[0+dspNum3]); lc.setRow(0,5,nmbrChar[1+dspNum3]); lc.setRow(0,6,nmbrChar[2+dspNum3]); break; case 2: lc.setRow(1,1,nmbrChar[0+dspNum3]); lc.setRow(1,2,nmbrChar[1+dspNum3]); lc.setRow(1,3,nmbrChar[2+dspNum3]); break; case 3: lc.setRow(1,5,nmbrChar[0+dspNum3]); lc.setRow(1,6,nmbrChar[1+dspNum3]); lc.setRow(1,7,nmbrChar[2+dspNum3]);
break; } }

void sTic (){ lc.setRow(0,7,B00000000); lc.setRow(1,0,B00100100); }

void sToc (){ lc.setRow(0,7,B00100100); lc.setRow(1,0,B00000000); }

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) { // sets time and date data to DS3231 Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set next input to start at the seconds register Wire.write(decToBcd(second)); // set seconds Wire.write(decToBcd(minute)); // set minutes Wire.write(decToBcd(hour)); // set hours Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) Wire.write(decToBcd(month)); // set month Wire.write(decToBcd(year)); // set year (0 to 99) Wire.endTransmission(); }

void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set DS3231 register pointer to 00h Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes of data from DS3231 starting from register 00h *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); }

void displayTime() { byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; // retrieve data from DS3231 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // send it to the serial monitor Serial.print(hour, DEC); // convert the byte variable to a decimal number when displayed Serial.print(":"); if (minute<10) { Serial.print("0"); } Serial.print(minute, DEC); Serial.print(":"); if (second<10) { Serial.print("0"); } Serial.print(second, DEC); Serial.print(" "); Serial.print(dayOfMonth, DEC);

//There are 4 spots 0-3 from left to right if (hour>12)hour=hour-12;

if (hour < 10) displayNum(0,0); //spot, number else displayNum(0,hour/10);

displayNum(1,hour%10);

displayNum(2,minute/10);

displayNum(3,minute%10); }

/r/arduino Thread Parent Link - imgur.com