Skip to main content

Digital clock state diagram

Short answer

The clock has three states: Normal (display time/date), Set Time (adjust seconds), Set Date (adjust days). Mode button cycles Normal→Set Time→Set Date→Normal. Forward button increments by one unit; held >2s enters fast mode incrementing every 0.2s. Substates: Idle, Pressed, Fast with transitions on press, release, and after(2s).

A new digital clock, recently available in the market, simultaneously displays the time and date. The time and date displays can be adjusted by the buttons available within the unit. The clock has two buttons, "mode" and "forward". If you wish to change the time you should first press the mode button, after which the time may be changed by the forward-button. If the mode button is pressed again, you can change the date (by the forward-button). If the mode button is pressed once again, you will go back to normal state. When the forward-button is pressed, the display will go a single unit (seconds or days) ahead. If the button is held down for more than two seconds, the display will change rapidly (once in every 0.2 second) ahead so long as the button is pressed. Draw a state diagram for this clock control unit.

Assumptions: the clock has three modes: Normal (displays time and date, running), Set Time and Set Date. Events: mode and forward button presses, forward held for more than 2 s, and forward released. In Set Time one unit is one second; in Set Date one unit is one day.

State diagram

  (*)
   |
   v
 +----------------------+
 |        Normal        |<--------------+
 | do/ run clock,       |               |
 |  display time, date  |               |
 +----------------------+               |
   |                                    |
  mode                                  |
   v                                    |
 +----------------------+               |
 |       Set Time       |               |
 | entry/ blink time    |               |
 | forward: +1 second   |               |
 +----------------------+               |
   |                                    |
  mode                                  |
   v                                    |
 +----------------------+     mode      |
 |       Set Date       |---------------+
 | entry/ blink date    |
 | forward: +1 day      |
 +----------------------+

The mode button cycles the states: Normal --mode--> Set Time --mode--> Set Date --mode--> Normal.

Substates for the forward button (inside Set Time and Set Date)

 +----------------------------------------------------+
 |  (*)                                               |
 |   |                                                |
 |   v     forward pressed / +1 unit                  |
 |  Idle ----------------------------> Pressed        |
 |   ^  ^                                |            |
 |   |  +------- forward released -------+            |
 |   |                                   | after(2 s) |
 |   |                                   v            |
 |   +-------- forward released ------- Fast          |
 |                         do/ +1 unit every 0.2 s    |
 +----------------------------------------------------+

Transition table

FromEventToAction
NormalmodeSet Timeblink the time display
Set Time: Idleforward pressedSet Time: Pressedtime + 1 second
Pressedafter(2 s) while still pressedFastnone
Fastevery 0.2 sFasttime + 1 second
Pressed or Fastforward releasedIdlestop incrementing
Set TimemodeSet Dateblink the date display
Set Dateforward pressed / heldSet Date (same substates)date + 1 day
Set DatemodeNormalresume the normal display

The held-down behaviour is triggered by the time event after(2 s), and the repeated increment is timed with every(0.2 s).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Analysis and Design chapter 2 questions →