Some time ago, I made a two-button clock project:
https://forum.arduino.cc/index.php?topic=408565.0
I used a coding style (numbers in BCD, lots of bit-masks) which "feels" right to me, but is very beginner-unfriendly. So, now I wish to build up essentially the same project, in smaller steps, and with a simpler, more beginner-friendly coding style.
This first stage just counts time since the Arduino was most recently powered on (or reset).
// robs_clk_pt1.ino
// by odometer 2018-12-06
#include <LiquidCrystal.h>
// declare and initialize variables
uint16_t dd = 0; // number of days
uint8_t hh = 0; // number of hours
uint8_t mi = 0; // number of minutes
uint8_t ss = 0; // number of seconds
uint8_t ticks = 0; // number of ticks
uint32_t lastTickMicros = 0UL; // value of micros() at most recent tick
uint8_t updateFlag = 1; // whether to update the display (0 means no, 1 means yes)
char buf[30]; // buffer for storing a string
// a pretty important constant
const uint32_t ONE_TICK = 100000UL; // this number controls the speed of our clock
// here we specify what pins our LCD is on
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// start the LCD going
lcd.begin(16, 2);
}
void loop() {
// check if it is time for the clock to tick
if ((micros() - lastTickMicros) >= ONE_TICK) {
// make the clock tick forward
ticks++;
// space the clock ticks exactly 1/10 second apart
lastTickMicros += ONE_TICK;
// take care of all of the timekeeping math
if (ticks >= 10) {
// trade 10 ticks for 1 second
ticks -= 10;
ss++;
}
if (ss >= 60) {
// trade 60 seconds for 1 minute
ss -= 60;
mi++;
}
if (mi >= 60) {
// trade 60 minutes for 1 hour
mi -= 60;
hh++;
}
if (hh >= 24) {
// trade 24 hours for 1 day
hh -= 24;
dd++;
}
// remind ourselves that it is time to update the display
updateFlag = 1;
}
// check if it is time to update the display
if (updateFlag == 1) {
// update the display
//
// Position: 01234567890123456
// Line 0: robs_clk_pt1.ino
// or, Line 0: So far it's been
// Line 1: 00000d00h00m00s0
lcd.setCursor(0,0);
// top line of display: alternate messages every 5 seconds
if ((ss % 10) < 5) {
lcd.print("robs_clk_pt1.ino");
}
else {
lcd.print("So far it\'s been");
}
lcd.setCursor(0,1);
// bottom line of display: show elapsed time
sprintf(buf, "%5dd%02dh%02dm%02ds%d", dd, hh, mi, ss, ticks);
lcd.print(buf);
updateFlag = 0;
}
}
I am not 100% sure about my explanatory comments, though. I want them to be short enough to hold the reader's attention, but not so short as to leave her scratching her head.
The main thing I am having trouble with is that big ol' sprintf() statement. I am not sure how to comment it, or even whether to try.
I am aware that, instead of the sprintf() statement, I could use lcd.print() to print one number at a time. But, let's face it: lcd.print() leaves something to be desired. More details on that "something" here: Support for leading zeros in Serial.print, etc. - Suggestions for the Arduino Project - Arduino Forum
What should I do?