// Fnu Shoaib Ahmed, 652420658, fahme8
// Lab 6 - Liquid Crystal Display - Time and Date displayer
// Description - This code is designed to power up an LCD with time and date according to serial monitor input.
// Assumptions - The potentiometer will control display brightness and the wiring is correct.
// References - I used the prelab links to calculate the Leap Year formula and also to install Time library.
#include <LiquidCrystal.h>
#include <TimeLib.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int photoResistorPin = A0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
// Initial prompt for user input
Serial.println("Enter date & time as mm/dd/yyyy hh:mm:ss");
}
void loop() {
// Stores the last time the clock was displayed
static time_t prevDisplay = 0;
// Check if any data is available on the serial monitor input
if (Serial.available() > 0) {
// Read the input string until a newline is encountered
String dateTime = Serial.readStringUntil('\n');
// Process the date and time input from the user
if (processDateTime(dateTime)) {
// Confirm valid input
Serial.println("New date/time set.");
// Prompt for the next input
Serial.println("Enter date & time as mm/dd/yyyy hh:mm:ss");
} else {
// Inform the user of an invalid input
Serial.println("Invalid date/time. Try again.");
// Prompt for correction
Serial.println("Enter date & time as mm/dd/yyyy hh:mm:ss");
}
}
// Update the display if the time has changed
if (now() != prevDisplay) {
// Update the previous display time
prevDisplay = now();
// Display the updated date and time on the LCD
displayDateTime();
}
}
// Processes the date and time input from the user, validates it, and sets the system time
bool processDateTime(String dateTime) {
// Variables to store extracted values
int month, day, year, hour, minute, second;
// Buffers to hold formatted date and time strings
char dateBuffer[11], timeBuffer[9];
// Parse the input string in the format mm/dd/yyyy hh:mm:ss
sscanf(dateTime.c_str(), "%d/%d/%d %d:%d:%d", &month, &day, &year, &hour, &minute, &second);
// Check if the parsed date and time are valid
if (!isValidDateTime(month, day, year, hour, minute, second)) {
return false; // Return false if invalid
}
// If valid, set the Arduino's internal clock to the input date and time
setTime(hour, minute, second, day, month, year);
return true; // Return true indicating successful processing
}
// Validates the date and time values provided by the user
bool isValidDateTime(int month, int day, int year, int hour, int minute, int second) {
// Check if time components are within valid ranges
if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
return false; // Return false if time is out of range
}
// Check if month and day are valid
if (month < 1 || month > 12 || day < 1) {
return false; // Return false if month/day is out of range
}
// Handle different month cases, especially for February and months with fewer than 31 days
switch (month) {
case 2: // Special case for February
if (isLeapYear(year)) {
if (day > 29) return false; // In a leap year, February can have up to 29 days
} else {
if (day > 28) return false; // In a non-leap year, February can only have up to 28 days
}
break;
case 4: case 6: case 9: case 11: // April, June, September, November
if (day > 30) return false; // These months have only 30 days
break;
default: // For all other months, the maximum number of days is 31
if (day > 31) return false;
}
return true; // Return true if all checks are passed
}
// Determines if the provided year is a leap year
bool isLeapYear(int year) {
// A leap year is divisible by 4, but not by 100 unless it's divisible by 400
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}
// Displays the current date and time on the LCD
void displayDateTime() {
lcd.clear(); // Clear the current display
// Set cursor at the first row and print the date
lcd.setCursor(0, 0);
lcd.print(month()); // Print the month
lcd.print("/");
lcd.print(day()); // Print the day
lcd.print("/");
lcd.print(year()); // Print the year
// Set cursor at the second row and print the time
lcd.setCursor(0, 1);
lcd.print(hour()); // Print the hour
lcd.print(":");
lcd.print(minute()); // Print the minutes
lcd.print(":");
lcd.print(second()); // Print the seconds
}