/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
// code showing the time passing by on an I2C LCD
// allowing time editing through a rotary encode (double click = enter/exit edit mode, single click = select field to modify)
// modifying days, hour, minute, second will impact the full date (ie add 1 day at the end of the month goes to next month or add one second at 59 and you go to the next minute)
// month and year are direct editing (1-12 or 0-99)
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
#include <RTClib.h> // https://github.com/adafruit/RTClib
#include <OneButton.h> // https://github.com/mathertel/OneButton
const uint8_t colCnt = 20;
const uint8_t lineCnt = 4;
hd44780_I2Cexp lcd;
byte locked[] = {0b01110, 0b10001, 0b10001, 0b11111, 0b11011, 0b11011, 0b11111, 0b00000};
byte unlocked[] = {0b00110, 0b00001, 0b00001, 0b11111, 0b11011, 0b11011, 0b11111, 0b00000};
enum {LOCKED, UNLOCKED};
const byte encoderCLKPin = 2;
const byte encoderDTPin = 3;
Encoder encoder(encoderDTPin, encoderCLKPin);
long encoderPosition;
const byte encoderSWPin = 4;
OneButton encoderSwitch(encoderSWPin);
// pick the right class for your RTC
RTC_DS1307 rtc; // in wokwi only the DS1307 is available at the time of this writing but it's not as good as a DS3231
// RTC_DS3231 rtc;
DateTime currentTime; // Current time
enum : byte {NORMAL_MODE, CONFIG_MODE} mode, previousMode;
enum {EDIT_DAY, EDIT_MONTH, EDIT_YEAR, EDIT_HOUR, EDIT_MINUTE, EDIT_SECOND} editingTarget, previousEditingTarget;
bool encoderChanged() {
long newPosition = encoder.read() >> 2; // divide by 4 as the rotary sends 4 ticks per click
if (newPosition != encoderPosition) {
encoderPosition = newPosition;
return true;
}
return false;
}
void resetEncoder() {
encoder.write(encoder.read() & 0b11); // keep the sub ticks
encoderPosition = 0;
}
void printWithTwoDigits(uint32_t number) {
if (number < 10) lcd.write('0');
lcd.print(number);
}
void showTime() {
static DateTime previousTime(0ul);
static bool previousInvisibility = true;
bool invisibility = (millis() & 0x100ul) ; // ~2Hz change (every 256 ms)
if (( currentTime != previousTime) || (editingTarget != previousEditingTarget) || (mode != previousMode) || (mode == CONFIG_MODE && (previousInvisibility != invisibility || encoderChanged()))) {
// something has changed, repaint the time
byte month, year;
if (mode == CONFIG_MODE) {
switch (editingTarget) {
case EDIT_DAY: currentTime = currentTime + TimeSpan(encoderPosition, 0, 0, 0); break;
case EDIT_MONTH:
month = byte(currentTime.month() + encoderPosition) % 13;
if (month == 0) month = 1;
currentTime = DateTime(currentTime.year(), month, currentTime.day(), currentTime.hour(), currentTime.minute(), currentTime.second());
break;
case EDIT_YEAR:
year = byte(currentTime.year() - 2000 + encoderPosition);
year = constrain(year, 0, 99);
currentTime = DateTime(year, currentTime.month(), currentTime.day(), currentTime.hour(), currentTime.minute(), currentTime.second());
break;
case EDIT_HOUR: currentTime = currentTime + TimeSpan(0, encoderPosition, 0, 0); break;
case EDIT_MINUTE: currentTime = currentTime + TimeSpan(0, 0, encoderPosition, 0); break;
case EDIT_SECOND: currentTime = currentTime + TimeSpan(0, 0, 0, encoderPosition); break;
}
resetEncoder();
}
lcd.setCursor(0, 0);
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.day());
else if (editingTarget == EDIT_DAY && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.day());
lcd.write('/');
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.month());
else if (editingTarget == EDIT_MONTH && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.month());
lcd.write('/');
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.year() - 2000);
else if (editingTarget == EDIT_YEAR && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.year() - 2000);
lcd.write(' ');
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.hour());
else if (editingTarget == EDIT_HOUR && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.hour());
lcd.write(':');
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.minute());
else if (editingTarget == EDIT_MINUTE && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.minute());
lcd.write(':');
if (mode == NORMAL_MODE) printWithTwoDigits(currentTime.second());
else if (editingTarget == EDIT_SECOND && invisibility) lcd.print(".."); else printWithTwoDigits(currentTime.second());
lcd.setCursor(colCnt - 1, 0);
lcd.write(mode == NORMAL_MODE ? LOCKED : UNLOCKED);
previousTime = currentTime;
previousEditingTarget = editingTarget;
previousMode = mode;
previousInvisibility = invisibility;
}
}
void simpleClick() {
if (mode == CONFIG_MODE) {
// go to the next editing target
encoder.write(0); encoderPosition = 0;
switch (editingTarget) {
case EDIT_DAY: editingTarget = EDIT_MONTH; break;
case EDIT_MONTH: editingTarget = EDIT_YEAR; break;
case EDIT_YEAR: editingTarget = EDIT_HOUR; break;
case EDIT_HOUR: editingTarget = EDIT_MINUTE; break;
case EDIT_MINUTE: editingTarget = EDIT_SECOND; break;
case EDIT_SECOND: editingTarget = EDIT_DAY; break;
}
}
showTime();
}
void doubleClick() {
switch (mode) {
case NORMAL_MODE: mode = CONFIG_MODE; editingTarget = EDIT_DAY; encoder.write(0); break;
case CONFIG_MODE: rtc.adjust(currentTime); mode = NORMAL_MODE; break;
}
showTime();
}
void testSwitch() {
encoderSwitch.tick();
}
void setup() {
pinMode(encoderSWPin, INPUT_PULLUP);
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
int result = lcd.begin(colCnt, lineCnt);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
lcd.createChar(LOCKED, locked);
lcd.createChar(UNLOCKED, unlocked);
encoderSwitch.attachClick(simpleClick);
encoderSwitch.attachDoubleClick(doubleClick);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Do this ONCE to set the RTC to the current time if your RTC is not already set, then comment it out.
currentTime = rtc.now();
}
void loop() {
testSwitch();
if (mode == NORMAL_MODE) currentTime = rtc.now();
showTime();
// do other stuff here as long as it's non blocking and fast
}