# Traffic light controller

> A Moore machine with three colored states and timed transitions, plus a button that shortens the green. A automata example with its LaTeX source.

A automata example in the TeXeR TikZ library: https://texer.org/library/automata/traffic-light

Package: [automata](https://texer.org/library/automata.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: state machine, Moore machine, controller

## Source

```latex
\usetikzlibrary{automata, arrows.meta}

\begin{tikzpicture}[
  shorten >=1pt, auto, >={Stealth}, every state/.style={minimum size=15mm, font=\small, thick},
]
  \node[state, initial, fill=green!25] (g) at (0,0) {Green};
  \node[state, fill=yellow!40] (y) at (4,0) {Yellow};
  \node[state, fill=red!25] (r) at (2,-3.2) {Red};

  \path[->]
  (g) edge node {30 s or button} (y)
  (y) edge node {5 s} (r)
  (r) edge node {25 s} (g);
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{automata, arrows.meta}

\begin{document}

\begin{tikzpicture}[
  shorten >=1pt, auto, >={Stealth}, every state/.style={minimum size=15mm, font=\small, thick},
]
  \node[state, initial, fill=green!25] (g) at (0,0) {Green};
  \node[state, fill=yellow!40] (y) at (4,0) {Yellow};
  \node[state, fill=red!25] (r) at (2,-3.2) {Red};

  \path[->]
  (g) edge node {30 s or button} (y)
  (y) edge node {5 s} (r)
  (r) edge node {25 s} (g);
\end{tikzpicture}
\end{document}
```
