You are waiting at a crossing in Dhaka, watching the traffic signal. It is red. You count the seconds, a minute goes by, and at last it turns green. Nobody told it to. There was no policeman and no sensor under the road. Somewhere in a small box a circuit kept track of how long the light had been red, and when the time was up it moved on to the next colour. That circuit does not know who you are, and it has no idea what happened at this crossing an hour ago. It knows exactly one thing: which step of its cycle it is on right now.
That one thing, the step a circuit is on, is called its state, and a circuit built around the idea is a finite state machine. Once you see it, you find it everywhere: in a lift that decides whether to go up or wait, in a ticket machine that counts the coins you drop in, and in a keypad lock that opens only when someone presses 1, 0, 1, 1 one after another.
The counters you have just built are state machines in their simplest form. They have only a clock to listen to. In this chapter we give the machine an input to react to and an output to drive, and we learn to turn any such job into a circuit, step by step, from words to gates.