#include <Ds1302.h>
#include <LiquidCrystal.h>
#define DS1302_RAMSIZE 31
Ds1302 rtc(3, 4, 5);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
byte segment[8][8] =
{
{ B01111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 },
{ B11110, B11111, B11111, B11111, B11111, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B11111, B11111, B11111, B11111, B01111 },
{ B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11110 },
{ B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 },
{ B11111, B00000, B00000, B00000, B00000, B11111, B11111, B11111 }
};
byte zero = 0x00;
int col = 0;
int number, lastMin1, lastMin2, lastHour1, lastHour2;
void setup()
{
rtc.init();
Ds1302::DateTime dt = {
.year = 23,
.month = Ds1302::MONTH_JAN,
.day = 18,
.hour = 22,
.minute = 13,
.second = 30,
.dow = Ds1302::DOW_WED
};
rtc.setDateTime(&dt);
lcd.begin(20, 2);
for (byte i = 0; i < 8; i++)
{
lcd.createChar(i, segment[i]);
}
}
void loop()
{
// Extracts the time from the DS1307 and shows it on the LCD
showClock();
delay(1000);
}
byte convertToBCD(byte val) { // Converts the decimal number to BCD
return ( (val / 10 * 16) + (val % 10) );
}
byte convertToDecimal(byte val) { // Converts from BCD to decimal
return ( (val / 16 * 10) + (val % 16) );
}
void digit0() // Set the segments to the number 0
{
lcd.setCursor(col, 0); // Select the top line
lcd.write((byte)0); // Segment 0 selected
lcd.write(1); // Segment 1 selected
lcd.write(2);
lcd.setCursor(col, 1); // Select the bottom line
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void digit1() // Set the segments to the number 1
{
lcd.setCursor(col, 0);
lcd.write(1);
lcd.write(2);
lcd.setCursor(col + 1, 1);
lcd.write(5);
}
void digit2() // Set the segments to the number 2
{
lcd.setCursor(col, 0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(col, 1);
lcd.write(3);
lcd.write(7);
lcd.write(7);
}
void digit3() // Set the segments to the number 3
{
lcd.setCursor(col, 0);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(7);
lcd.write(5);
}
void digit4() // Set the segments to the number 4
{
lcd.setCursor(col, 0);
lcd.write(3);
lcd.write(4);
lcd.write(2);
lcd.setCursor(col + 2, 1);
lcd.write(5);
}
void digit5() // Set the segments to the number 5
{
lcd.setCursor(col, 0);
lcd.write((byte)0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(col, 1);
lcd.write(7);
lcd.write(7);
lcd.write(5);
}
void digit6() // Set the segments to the number 6
{
lcd.setCursor(col, 0);
lcd.write((byte)0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(col, 1);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
void digit7() // Set the segments to the number 7
{
lcd.setCursor(col, 0);
lcd.write(1);
lcd.write(1);
lcd.write(2);
lcd.setCursor(col + 1, 1);
lcd.write((byte)0);
}
void digit8() // Set the segments to the number 8
{
lcd.setCursor(col, 0);
lcd.write((byte)0);
lcd.write((byte)6);
lcd.write(2);
lcd.setCursor(col, 1);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
void digit9() // Set the segments to the number 9
{
lcd.setCursor(col, 0);
lcd.write((byte)0);
lcd.write((byte)6);
lcd.write((byte)2);
lcd.setCursor(col + 2, 1);
lcd.write((byte)5);
}
void showDigit() // Shows the number in the position defined by "X"
{
switch (number) {
case 0: digit0();
break;
case 1: digit1();
break;
case 2: digit2();
break;
case 3: digit3();
break;
case 4: digit4();
break;
case 5: digit5();
break;
case 6: digit6();
break;
case 7: digit7();
break;
case 8: digit8();
break;
case 9: digit9();
break;
}
}
void showClock()
{
Ds1302::DateTime now;
rtc.getDateTime(&now);
now.hour = now.hour - 12;
if (now.second & 1) {
lcd.setCursor(8, 0);
lcd.print(".");
lcd.setCursor(8, 1);
lcd.print(".");
} else {
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(" ");
}
col = 0;
number = now.hour / 10;
// Conditional to avoid overlapping characters
if (lastHour1 != now.minute % 10) {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lastHour1 = now.minute % 10;
}
showDigit(); // Show the customized number on the display
col = 4;
number = now.hour % 10;
if (lastHour2 != now.minute % 10) {
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(" ");
lastHour2 = now.minute % 10;
}
showDigit();
col = 9;
number = now.minute / 10;
if (lastMin1 != now.minute % 10) {
lcd.setCursor(9, 0);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(" ");
lastMin1 = now.minute % 10;
}
showDigit();
col = 13;
number = now.minute % 10;
if (lastMin2 != now.minute % 10) {
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setCursor(13, 1);
lcd.print(" ");
lastMin2 = now.minute % 10;
}
showDigit();
}