#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 8, d5 = 7, d6 = 6, d7 = 5, LED = 9;
LiquidCrystal lcd(12,11,9,8,7,6);
// usually the rotary encoders three pins have the ground pin in the middle
int PBS1 = 2;
int PBS2 = 3;
int PBS3 = 4;
int PBS4 = 5;
int hourcounter, minutecounter;
int h, m, oldm; // hous, minutes, old minute
// varialbe to know if we are in time setting mode
boolean set_h = false; //hours
boolean set_m = false; //minutes
/////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
// set up the encoder
pinMode(PBS1, INPUT_PULLUP);
pinMode(PBS2, INPUT_PULLUP);
pinMode(PBS3, INPUT_PULLUP);
pinMode(PBS4, INPUT_PULLUP);
attachInterrupt(0, PBS1, CHANGE);
// encoder pin on interrupt 1 (pin 3)
attachInterrupt(1, PBS4, CHANGE);
// *******DEFINE CUSTOM CHARACTERS FOR BIG FONT*****************
byte A[8] = {
B00011,
B00111,
B01111,
B01111,
B01111,
B01111,
B01111,
B01111
};
byte B[8] = {
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111
};
byte C[8] = {
B11000,
B11100,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110
};
byte D[8] = {
B01111,
B01111,
B01111,
B01111,
B01111,
B01111,
B00111,
B00011
};
byte E[8] = {
B11111,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
};
byte F[8] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11100,
B11000
};
byte G[8] = {
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte H[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
};
lcd.createChar(8, A);
lcd.createChar(6, B);
lcd.createChar(2, C);
lcd.createChar(3, D);
lcd.createChar(7, E);
lcd.createChar(5, F);
lcd.createChar(1, G);
lcd.createChar(4, H);
if (!rtc.begin()) {
lcd.setCursor(16, 1);
lcd.print("Retrying...");
delay(5000);
}
delay(500);
// print all the fix text - never changes
lcd.setCursor(0, 0);
lcd.print(" __ __" + String(char(165)) + " __ __"); // char(165) is big point symbol
lcd.setCursor(0, 1);
lcd.print(" " + String(char(165)) + " ");
lcd.setCursor(0, 2);
// set a particular date and time if you want (yyyy,mm,dd,hh,mm,ss)
// rtc.adjust(DateTime(2021, 2, 16, 00, 00, 00));
// read all time and date and display
DateTime now = rtc.now();
h = now.hour();
m = now.minute();
oldm = m;
// fill the display with all the data
printbig((h % 10), 3);
printbig((h / 10), 0);
printbig((m % 10), 10);
printbig((m / 10), 7);
}
void loop() {
DateTime now = rtc.now();
m = now.minute(); // read minutes
if (oldm != m) {
printbig((m % 10), 10); // display minutes
printbig((m / 10), 7);
oldm = m;
if (m == 0) { // hours change
h = now.hour(); // read hours
printbig((h % 10), 3); // display hours
printbig((h / 10), 0);
}
}
while (digitalRead(PBS1)==LOW){
int hourcounter, minutecounter, h, m = 0;
timeset();
}
}
void timeset() {
do {
// set hours
if ((digitalRead(PBS4) == LOW) && hourcounter == 0)
{
line1:
if (digitalRead(PBS2) == LOW)
{
h = h + 1;
if (h > 23)
{
h = 0;
}
goto line1;
lcd.setCursor(0, 0);
lcd.print(" __ __" + String(char(165)) + " __ __" + (char(165)) + " __ __"); // char(165) is big point symbol
lcd.setCursor(0, 1);
lcd.print(" " + String(char(165)) + " " + (char(165)) + " ");
printbig((h / 10), 0);
printbig((h % 10), 3);
delay(100); // debounce again
rtc.adjust(DateTime(2022, 2, 16, h, m, 00));
}
else if (digitalRead(PBS3) == LOW)
{
h = h - 1;
if (h< 0)
{
h = 23;
}
goto line1;
lcd.setCursor(0, 0);
lcd.print(" __ __" + String(char(165)) + " __ __" + (char(165)) + " __ __"); // char(165) is big point symbol
lcd.setCursor(0, 1);
lcd.print(" " + String(char(165)) + " " + (char(165)) + " ");
printbig((h / 10), 0);
printbig((h % 10), 3);
delay(100); // debounce again
rtc.adjust(DateTime(2022, 2, 16, h, m, 00));
}
else if ((digitalRead(PBS1)==LOW) && hourcounter == 0 && minutecounter == 0)
{
int hourcounter = 1;
}
}
// set minutes
else if ((digitalRead(PBS4) == LOW) && hourcounter == 1)
{
if (digitalRead(PBS2) == LOW)
{
m = m + 1;
if (m > 59)
{
m = 0;
}
lcd.setCursor(0, 0);
lcd.print(" __ __" + String(char(165)) + " __ __" + (char(165)) + " __ __"); // char(165) is big point symbol
lcd.setCursor(0, 1);
lcd.print(" " + String(char(165)) + " " + (char(165)) + " ");
printbig(m / 10, 7);
printbig(m % 10, 10);
delay(100); // debounce again
rtc.adjust(DateTime(2021, 2, 16, h, m, 00));
}
else if (digitalRead(PBS3) == LOW)
{
m = m - 1;
if (m < 0)
{
m = 59;
}
lcd.setCursor(0, 0);
lcd.print(" __ __" + String(char(165)) + " __ __" + (char(165)) + " __ __"); // char(165) is big point symbol
lcd.setCursor(0, 1);
lcd.print(" " + String(char(165)) + " " + (char(165)) + " ");
printbig(m / 10, 7);
printbig(m % 10, 10);
delay(100); // debounce again
rtc.adjust(DateTime(2021, 2, 16, h, m, 00));
}
else if (digitalRead(PBS1)==LOW)
{
int minutecounter = 1;
}
}
//fill in the clock numbers
printbig((h % 10), 3);
printbig((h / 10), 0);
printbig((m % 10), 10);
printbig((m / 10), 7);
DateTime now = rtc.now(); // read again the time
} while ((digitalRead(PBS4) == LOW) && hourcounter == 1 && minutecounter == 1);
}
void printbig(int i, int x) {
// prints each segment of the big numbers
if (i == 0) {
lcd.setCursor(x, 0);
lcd.write(8);
lcd.write(1);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(4);
lcd.write(5);
} else if (i == 1) {
lcd.setCursor(x, 0);
lcd.write(8);
lcd.write(255);
lcd.print(" ");
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.write(255);
lcd.print(" ");
}
else if (i == 2) {
lcd.setCursor(x, 0);
lcd.write(1);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(7);
lcd.write(4);
}
else if (i == 3) {
lcd.setCursor(x, 0);
lcd.write(1);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(4);
lcd.write(7);
lcd.write(5);
}
else if (i == 4) {
lcd.setCursor(x, 0);
lcd.write(3);
lcd.write(4);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.write(5);
}
else if (i == 5) {
lcd.setCursor(x, 0);
lcd.write(255);
lcd.write(6);
lcd.write(1);
lcd.setCursor(x, 1);
lcd.write(7);
lcd.write(7);
lcd.write(5);
}
else if (i == 6) {
lcd.setCursor(x, 0);
lcd.write(8);
lcd.write(6);
lcd.print(" ");
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
else if (i == 7) {
lcd.setCursor(x, 0);
lcd.write(1);
lcd.write(1);
lcd.write(5);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.write(8);
lcd.print(" ");
}
else if (i == 8) {
lcd.setCursor(x, 0);
lcd.write(8);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
else if (i == 9) {
lcd.setCursor(x, 0);
lcd.write(8);
lcd.write(6);
lcd.write(2);
lcd.setCursor(x, 1);
lcd.print(" ");
lcd.write(4);
lcd.write(5);
}
}