#include <Wire.h>//includes the Wire library in Arduino
#include <LiquidCrystal_I2C.h>//includes the LiquidCrystal library in Arduino
LiquidCrystal_I2C lcd(0x27, 20, 4); //Setting the I2C address of the LCD screen and the size of the screen
unsigned long previousMillis = 0; // to keep track of the previous time value, such as in implementing time-based events or timing operations
const long interval = 1000; // Counting time in milliseconds (1000 ms = 1 second).
unsigned long totalSeconds = 0;// to keep track of the total number of seconds elapsed in a program or a specific task.
unsigned int seconds = 0;// seconds will have an initial value of 0
unsigned int minutes = 0;// minutes will have an initial value of 0
unsigned int hours = 0;// hours will have an initial value of 0
unsigned int days = 0;// days will have an initial value of 0
void setup() {
lcd.init(); // To start using an LCD screen
lcd.backlight(); // Turn on the backlight of the LCD screen
lcd.setCursor(0, 0); //any subsequent text or data written to the LCD will start from this position.
lcd.print("Time: "); //LCD displays to print text on the LCD screen.
}
void loop() {
unsigned long currentMillis = millis();//will hold the number of milliseconds that have elapsed since the Arduino board started running the current program.
if (currentMillis - previousMillis >= interval)//allowing to perform actions at specific intervals without pausing the execution of the rest of the code.
{
previousMillis = currentMillis; //updating previousMillis to hold the value of currentMillis
totalSeconds++;//is an increment operation that increases the value of the variable totalSeconds by 1.
seconds = totalSeconds % 60;// calculates the remainder when totalSeconds is divided by 60
minutes = (totalSeconds / 60) % 60;//calculates the remainder when the total number of minutes is divided by 60
hours = (totalSeconds / 3600) % 24;//calculates the remainder when the total number of hours is divided by 24.
days = totalSeconds / 86400;//convert a total number of seconds into days
lcd.setCursor(6, 0);//to set the cursor position on the LCD screen.
lcd.print(days); //printing the variable days on the LCD screen
lcd.print("d "); //prints the string "d " on the LCD screen
lcd.print(hours);//printing the variable hours on the LCD screen
lcd.print("h ");//prints the string "h " on the LCD screen
lcd.print(minutes);//printing the variable minutes on the LCD screen
lcd.print("m ");//prints the string "m " on the LCD screen
lcd.print(seconds);//printing the variable second at LCD
lcd.print("s ");//prints the string "s " on the LCD screen
}
}