In this installment we'll tie together much of what we looked at in this blog series with a larger application.
I decided to implement a simple digital clock program with alarm. These were the basic requirements and features:
- Time display, 12 hour mode: 100 through 1159 with rightmost decimal point indicating pm. 24 hour mode: 0000 through 2359. The second decimal point will toggle at a one second on/one second off rate whenever the time is displayed. The time is also displayed in binary using the discrete LEDs. The hours are shown on the LEDs in the first row, minutes on the second row, and seconds on the third row.
- Time set function: pressing four keys below display will perform as follows (left to right): go back one hour, advance one hour, go back one minute, advance one minute.
- Chime: When enabled, will beep on the hour using the buzzer.
- Alarm: When enabled, buzzer will sound at one second on/one second off rate until any key is pressed. Leftmost decimal point indicates alarm on.
D - Display date briefly, e.g "JA 1" "2017" or "DE24" "2018". Shows month and day, then year, then goes back to time mode.
C - Toggle chime. Briefly Display "CH Y" or "CH N", then goes back to time mode.
M - Toggle between 12 and 24 hour mode. Briefly display "12HR" or "24HR", then go back to time mode.
A - Toggle alarm on/off. Briefly display "AL N" or AL Y", then go back to time mode. Leftmost decimal point goes on when alarm is enabled.
S - Set alarm. Pressing keys under display will set alarm time, similar to time set. Pressing S again will exit alarm set mode. Leave alarm set mode if no key pressed for more than 5 seconds.
T - Set date. Pressing keys under display will set month and day, similar to time set. Pressing T again will exit date set mode. Leave date mode if no key pressed for more than 5 seconds.
I won't go over the source code line by line. The software can be found here.
It consists of one main loop which performs various functions:
- Get the current time from the RTC.
- Display current time, alarm time, or date depending on mode in effect.
- Display time in binary on the discrete LEDs.
- Check if it is time to play the chime (hour rolled over).
- Check if it is time to play the alarm. Toggle alarm beep if it is active.
- If no keys pressed for 5 seconds, exit alarm or date set modes and revert to time mode.
- Check if key pressed.
- If alarm is active, pressing any key will turn it off.
- Update how long since a key was pressed.
- Handle time/alarm/date set keys 1-4.
- Handle date key.
- Handle toggle chime key.
- Handle 12/24 hour mode key.
- Toggle alarm on/off key.
- Handle alarm mode key.
- Handle set date keys.
- Delay 1 second, unless a key was pressed.
- Toggle seconds decimal point.
It doesn't use the the sound module or the rotors. These could obviously used for sounds and for setting the time or date.
Incidentally, there are Arduino-based clocks on the market. The "new" Heathkit, for example, offers one as a kit which looks like a traditional digital clock and is Arduino based.