Wednesday, May 10, 2017

Programming the MeinEnigma - Alarm Clock Example



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:

  1. 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.
  2. 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.
  3. Chime: When enabled, will beep on the hour using the buzzer.
  4. 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.

Keyboard keys:

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:
  1. Get the current time from the RTC.
  2. Display current time, alarm time, or date depending on mode in effect.
  3. Display time in binary on the discrete LEDs.
  4. Check if it is time to play the chime (hour rolled over).
  5. Check if it is time to play the alarm. Toggle alarm beep if it is active.
  6. If no keys pressed for 5 seconds, exit alarm or date set modes and revert to time mode.
  7. Check if key pressed.
  8. If alarm is active, pressing any key will turn it off.
  9. Update how long since a key was pressed.
  10. Handle time/alarm/date set keys 1-4.
  11. Handle date key.
  12. Handle toggle chime key.
  13. Handle 12/24 hour mode key.
  14. Toggle alarm on/off key.
  15. Handle alarm mode key.
  16. Handle set date keys.
  17. Delay 1 second, unless a key was pressed.
  18. Toggle seconds decimal point.
The program was written for clarity and not intended as a polished application. Several things could could be made more efficient and there are some obvious enhancements, some of which are listed under "to do" at the top of the file.

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.

No comments:

Post a Comment