Nothing vital to the project here…you can call it done and put it on display now if you like. Just some extra tidbits and trivia for the curious…
On most Arduino-type boards, the onboard oscillator (from which all timing — such as the delay() function — is derived) is good, but it’s not that good. Over time, it may pick up or lose a few seconds per day. But the DS3231 RTC might drift by just a few seconds per year!
Rather than having our code continually poll the RTC or using delay() or millis() to refresh the display, a special feature of the DS3231 is used: an optional precise 1 Hz square wave output can be enabled. After initially reading the clock on startup (and doing some math with dates and times), the code then watches this signal as a second counter. Any subsequent math is much simpler! Just needs to count 60 seconds, then decrement the number of minutes.
Pin 3 on the Pro Trinket is special. We can “attach an interrupt” to it…when the signal on that pin changes, some code is automatically run. We don’t even need to continually call digitalRead(), it just happens.
That’s how we blink the decimal point at precise 1 second intervals and count minutes, and avoid doing all that time and date math over and over and over…in fact, if we wanted, we could stop the processor completely in a low-power state, having it wake up each time this interrupt occurs. Since this isn’t a battery-powered project, and the always-on LED displays use more current than the microcontroller, there’s no real practical need for that…but I at least wanted to demonstrate the interrupt feature in action, it might come in handy for your own projects later.
void interrupt1hz(void) { if(++seconds >= 60) { seconds = 0; if(minutes) minutes--; } hzFlag = true; } setup() { // This will make the interrupt1hz() function run automatically every // time Pin 3 changes from HIGH to LOW logic state (a "FALLING" signal): attachInterrupt(1, interrupt1hz, FALLING); }
You’ll see this same technique in action in our Talking d20 project, which does use low-power sleep modes to maximize battery life. It watches for an accelerometer in free-fall, waking the microcontroller when detected.
Mind Tricks
When you glance at your clock, you might notice the decimal point “sticks” for a moment before it resumes blinking. How does the clock know when you’re looking at it? Or is this a bug in the code?
Neither! It’s an optical illusion known as chronostasis or the “stopped clock illusion.” Wikipedia has this to say about chronostasis:
Chronostasis is a type of temporal illusion in which the first impression following the introduction of a new event or task demand to the brain appears to be extended in time. This effect can extend apparent durations by up to 500 ms and is consistent with the idea that the visual system models events prior to perception.
Basically, perception — making sense of what you see — runs a little slower than visual sensation — when photons are absorbed by the retina. Certain times (e.g. fast eye movements, as when glancing over to a clock) the mind has to fill in the gaps. Hilarity ensues.
Science!
Text editor powered by tinymce.