Skip to content

Clock

In this tutorial you will make a simple clock. Doing this you will learn how to replace a tangled set of if's with state machine. If you don't know how state machine works, please read first my Control actions with state machine tutorial.

Table of contents:


Iteration 1

In this iteration you:

  • make initial cicruit consisting of microcontroller, LCD with I2C adapter and one tact switch (button);
  • measure the passage of time using millis() function;
  • reset time after pressing the button.


Iteration 2

In this iteration you:

  • add all buttons required to control your clock;
  • make a diagram showing actions and how they reflect state changes;
  • implement initial code for small subset of actions and states.


General idea of states and actions flow

General idea of states and actions flow


An inital code taking control on the small subset of states and actions

Below you can find inital code taking control on the small subset of states and actions:

This should give you an overwiew how you can continue this approach to make fully functional clock. Just after the code you will find an animation how it works.

An inital code taking control on the small subset of states and actions in action


Iteration 3

In this iteration you:

  • turn a set of ifs into state machine which simplifies code development.


Iteration 4

In this iteration you:

  • add LED to signal alarm activation;
  • implement all required functionality according to description from iteration 2.

Code from this iteration is much longer than from previous. It's not more complicated but is longer simply because it implements all functionality. I also added to the circuit LED and buzzer. Because it's long I divided it into parts:

This code works - see the video (approx. 128MiB).