Sure here's the code sketch to set and read time in my application:
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DS3231 rtc(SDA, SCL);
Time t;
int currentDigit = 0; // Current digit being edited (0 for hours tens, 1 for hours ones, 2 for minutes tens, etc.)
int timeInput[6]; // Array to store user input for hours tens, hours ones, minutes tens, minutes ones, seconds tens, and seconds ones
#define BUTTON_UP 2
#define BUTTON_DOWN 3
#define BUTTON_ENTER 4
#define BUTTON_BACK 5
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 150;
bool isSetTimeMode = false; // Flag to indicate if in set time mode
void displayTime();
void adjustTime(int direction);
void switch_digit();
void set_time();
void setup()
{
lcd.init();
lcd.backlight();
rtc.begin();
pinMode(BUTTON_ENTER, INPUT_PULLUP);
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_BACK, INPUT_PULLUP);
}
void loop()
{
if (!isSetTimeMode)
{
// Fetch current time from RTC
t = rtc.getTime();
timeInput[0] = t.hour / 10; // Tens digit of hours
timeInput[1] = t.hour % 10; // Ones digit of hours
timeInput[2] = t.min / 10; // Tens digit of minutes
timeInput[3] = t.min % 10; // Ones digit of minutes
timeInput[4] = t.sec / 10; // Tens digit of seconds
timeInput[5] = t.sec % 10; // Ones digit of seconds
}
displayTime();
// Check for button presses
if (digitalRead(BUTTON_UP) == LOW)
{
adjustTime(1); // Increment time
isSetTimeMode = true;
delay(debounceDelay); // Debounce delay
}
if (digitalRead(BUTTON_DOWN) == LOW)
{
adjustTime(-1); // Decrement time
isSetTimeMode = true;
delay(debounceDelay); // Debounce delay
}
if (digitalRead(BUTTON_ENTER) == LOW)
{
switch_digit(); // Switch to the next digit
isSetTimeMode = true;
delay(debounceDelay); // Debounce delay
}
if (digitalRead(BUTTON_BACK) == LOW)
{
set_time(); // Set the time
isSetTimeMode = false;
delay(debounceDelay); // Debounce delay
}
}
void displayTime()
{
lcd.setCursor(0, 0);
lcd.print("Set Time:");
lcd.setCursor(3, 1);
for (int i = 0; i < 6; i++)
{
lcd.print(timeInput[i]); // Display each digit of the time
if (i == 1 || i == 3)
{
lcd.print(":"); // Add colon between hours, minutes, and seconds
}
}
}
void adjustTime(int direction)
{
// Increment or decrement the current digit
timeInput[currentDigit] += direction;
// Ensure the digit stays within valid range
switch (currentDigit)
{
case 0: // Tens digit of hours
if (timeInput[currentDigit] > 2)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 2;
if (timeInput[currentDigit] == 2 && timeInput[1] > 3) // If the tens digit is 2 and the ones digit exceeds 3, wrap it around
timeInput[1] = 0;
break;
case 1: // Ones digit of hours
if (timeInput[0] == 2) {
if (timeInput[currentDigit] > 3)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 3;
} else {
if (timeInput[currentDigit] > 9)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 9;
}
break;
case 2: // Tens digit of minutes
if (timeInput[currentDigit] > 5)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 5;
break;
case 3: // Ones digit of minutes
if (timeInput[currentDigit] > 9)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 9;
break;
case 4: // Tens digit of seconds
if (timeInput[currentDigit] > 5)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 5;
break;
case 5: // Ones digit of seconds
if (timeInput[currentDigit] > 9)
timeInput[currentDigit] = 0;
else if (timeInput[currentDigit] < 0)
timeInput[currentDigit] = 9;
break;
}
}
void switch_digit()
{
// Move to the next digit
currentDigit++;
// Wrap around if reached the last digit
if (currentDigit > 5)
{
currentDigit = 0;
}
}
void set_time()
{
// Set the RTC time based on the entered values
rtc.setTime(timeInput[0] * 10 + timeInput[1], timeInput[2] * 10 + timeInput[3], timeInput[4] * 10 + timeInput[5]);
// Print message indicating time has been set
lcd.clear(); // Clear the display
lcd.setCursor(0, 0);
lcd.print("Time Set!");
delay(2000); // Display the message for 2 seconds
}