// Forum: https://forum.arduino.cc/t/why-is-my-time-slow/1204090
// This Wokwi project: https://wokwi.com/projects/385226419203141633
#include <TimeLib.h>
#include <LiquidCrystal.h>
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned long targetTime;
bool momTimeReached = false; // Flag to track whether "Mom Time!" has been displayed
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
targetTime = now() + (180UL * 24 * 60 * 60); // 180 days in seconds
lcd.setCursor(0, 0);
lcd.print("Countdown Timer");
}
void loop() {
if (!momTimeReached) { // Check if "Mom Time!" has not been displayed
unsigned long currentTime = now();
unsigned long timeDiff = targetTime - currentTime;
if (timeDiff > 0) {
unsigned long days = timeDiff / 86400;
unsigned long hours = (timeDiff % 86400) / 3600;
unsigned long minutes = (timeDiff % 3600) / 60;
unsigned long seconds = timeDiff % 60;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D:");
lcd.print(days);
lcd.print(" H:");
lcd.print(hours);
lcd.setCursor(0, 1);
lcd.print("M:");
lcd.print(minutes);
lcd.print(" S:");
lcd.print(seconds);
} else {
// If countdown is over
momTimeReached = true; // Set the flag to true
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time Up!");
}
}
delay(1000); // Update every second
}