This clock will be able to collect time data from a RTC Clock and to display them on a LCD.
Code explanation#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ds3231.h>
struct ts t;
LiquidCrystal_I2C lcd(0x27,20,4);
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
t.hour=8;
t.min=30;
t.sec=0;
t.mday=23;
t.mon=12;
t.year=2019;
DS3231_set(t);
if(t.hour<13){
if(t.hour>0){
lcd.setCursor(3,0);
lcd.print("Good Morning !");
}
}
if(t.hour<23){
if(t.hour>19){
lcd.setCursor(4,0);
lcd.print("Good Night !");
}
}
if(t.hour<20){
if(t.hour>12){
lcd.setCursor(2,0);
lcd.print("Good Afternoon !");
}
}
}
void loop(){
DS3231_get(&t);
lcd.setCursor(6,2);
lcd.print(t.hour);
lcd.setCursor(8,2);
lcd.print(":");
lcd.setCursor(9,2);
lcd.print(t.min);
lcd.setCursor(11,2);
lcd.print(":");
lcd.setCursor(12,2);
lcd.print(t.sec);
if(t.mon==1){
lcd.setCursor(5,1);
lcd.print("Jan ");
}
if(t.mon==2){
lcd.setCursor(5,1);
lcd.print("Feb ");
}
if(t.mon==3){
lcd.setCursor(5,1);
lcd.print("Mar ");
}
if(t.mon==4){
lcd.setCursor(5,1);
lcd.print("Apr ");
}
if(t.mon==5){
lcd.setCursor(5,1);
lcd.print("May ");
}
if(t.mon==6){
lcd.setCursor(5,1);
lcd.print("Jun ");
}
if(t.mon==7){
lcd.setCursor(5,1);
lcd.print("Jul ");
}
if(t.mon==8){
lcd.setCursor(5,1);
lcd.print("Aug ");
}
if(t.mon==9){
lcd.setCursor(5,1);
lcd.print("Sep ");
}
if(t.mon==10){
lcd.setCursor(5,1);
lcd.print("Oct ");
}
if(t.mon==11){
lcd.setCursor(5,1);
lcd.print("Nov ");
}
if(t.mon==12){
lcd.setCursor(5,1);
lcd.print("Dec ");
}
lcd.setCursor(9,1);
lcd.print(t.mday);
lcd.setCursor(12,1);
lcd.print(t.year);
}
Firstly, we define all libraries and we realize some functions necessary to set the clock and the LCD :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ds3231.h>
struct ts t;
LiquidCrystal_I2C lcd(0x27,20,4);
Then, we initialize all elements of the clock :
lcd.init();
lcd.backlight();
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
t.hour=8;
t.min=30;
t.sec=0;
t.mday=23;
t.mon=12;
t.year=2019;
DS3231_set(t);
We say if it's the Morning, the night or the afternoon ( between midnight and 1PM : Morning / between 7PM and 11PM : the Night / between midday and 8PM : the Afternoon ) :
if(t.hour<13){
if(t.hour>0){
lcd.setCursor(3,0);
lcd.print("Good Morning !");
}
}
if(t.hour<23){
if(t.hour>19){
lcd.setCursor(4,0);
lcd.print("Good Night !");
}
}
if(t.hour<20){
if(t.hour>12){
lcd.setCursor(2,0);
lcd.print("Good Afternoon !");
}
}
In
the void loop, we set a command which give us informations coming from the RTC Clock :
void loop(){
DS3231_get(&t);
Then we draw the last line of our clock. The last line is composed of the hour/minute/seconds :
lcd.setCursor(6,2);
lcd.print(t.hour);
lcd.setCursor(8,2);
lcd.print(":");
lcd.setCursor(9,2);
lcd.print(t.min);
lcd.setCursor(11,2);
lcd.print(":");
lcd.setCursor(12,2);
lcd.print(t.sec);
Then we define that January is t.month = 01, February is t.month = 02, etc...
if(t.mon==1){
lcd.setCursor(5,1);
lcd.print("Jan ");
}
if(t.mon==2){
lcd.setCursor(5,1);
lcd.print("Feb ");
}
if(t.mon==3){
lcd.setCursor(5,1);
lcd.print("Mar ");
}
if(t.mon==4){
lcd.setCursor(5,1);
lcd.print("Apr ");
}
if(t.mon==5){
lcd.setCursor(5,1);
lcd.print("May ");
}
if(t.mon==6){
lcd.setCursor(5,1);
lcd.print("Jun ");
}
if(t.mon==7){
lcd.setCursor(5,1);
lcd.print("Jul ");
}
if(t.mon==8){
lcd.setCursor(5,1);
lcd.print("Aug ");
}
if(t.mon==9){
lcd.setCursor(5,1);
lcd.print("Sep ");
}
if(t.mon==10){
lcd.setCursor(5,1);
lcd.print("Oct ");
}
if(t.mon==11){
lcd.setCursor(5,1);
lcd.print("Nov ");
}
if(t.mon==12){
lcd.setCursor(5,1);
lcd.print("Dec ");
}
And finally we write the 2nd line of our clock ( month/day/year ) :
lcd.setCursor(9,1);
lcd.print(t.mday);
lcd.setCursor(12,1);
lcd.print(t.year);
}
First line :
Second line :
Third line :
Comments