Meridian Works
ENTR
Open the simulator
Waking the Machines

Lesson 8

State Machines in Ladder Logic: Sequences with One State Register

One thing at a time, the full pattern, drawing it before you open the editor, timers inside a state, illegal states, requests, and faults as states.

From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 8, “Machines with Moods: State”. A machine does one thing at a time. Write which one in a single register, and the rest follows from it.

One thing at a time

Stand at the end of the racking and watch the yard for ten minutes.

A forklift comes round the corner of the store. The empty crates on its forks are stacked to eye height, and the driver cannot see ahead. A man walks out of the canteen reading a sheet of paper. For eleven years they missed each other by two metres. One morning they did not. The bent handrail by the weighbridge dates from that morning.

That corner now has a lamp post. There are three lamps for the vehicles and two for people on foot. In the yellow box on the post sits a controller that has not seen power since the plant shut.

Ask the machine one question

Point at the box and ask: what is this machine doing right now?

I am not asking which lamp is lit. I am asking what it is doing. There are three honest answers. It is letting the vehicles through. It is warning the vehicles to stop. It is holding the vehicles while somebody crosses. There are three answers, and two of them are never true at once.

This is called a state. A state is not a lamp, a flag or a condition. It is the answer to what is the machine doing right now. The whole chapter comes from one fact: there is only ever one answer.

What you have written so far

Until this chapter your rungs asked about the present moment. Is the guard shut? Is the level above forty? Is the jar at station eight? When you needed memory you latched a bit. You sealed in a start button, held a fault until a reset, carried a decision down a belt. For those jobs that was the right tool.

But as the machine grows, the method jams. The reason is arithmetic, not taste. Every latch is one more bit, and bits multiply. Three bits give eight combinations. Eight bits give two hundred and fifty-six. Your design covers five or six of them. The machine can reach the rest too: at three in the morning, in front of a customer, in an order nobody drew on paper.

One register, one value

So do not spread the answer over eight bits. Write it in one place.

State is an integer (INT). Every state is a number, and the number is the answer. Not started yet is an answer too, so there is a fourth number:

State What the crossing is doing
0 not started: the isolator is open, the post is dark
1 vehicles passing
2 vehicles being warned
3 vehicles held while somebody crosses

An INT holds one value at a time. Your interlocks do not guarantee that; the register itself does. The "only one at a time" rule you used to promise with rungs is now a property of the memory. No rung can break it.

What is left is small:

  • To decide an output, ask which state you are in. State == 2 lights the amber. There is nothing to latch or reset, because the answer is worked out again every scan.
  • To change behaviour, write a new number into State. There is no other kind of event.
  • To read somebody else's program, find the places that write State. There will be four or five. They are the whole behaviour of the machine.

You can see the number

Every panel in this yard has a numeric display showing State, next to the lamps. It is the cheapest fault-finding tool in this book. A crossing that has hung shows 2 for ever. You know the amber never ends before you put your bag down.

On the Sorting Line you watched a shift register as a row of lamps instead of a number. Here we do the opposite, for the same reason: show what you designed, not what happens to be energised.

The yard has three jobs. The crossing at the blind corner only looks at the clock. The same crossing with a button has to remember a press. The dock door has limit switches, a light curtain, a timeout and a fault. By the end of this chapter you will understand why everybody avoids that door.

A machine that does two things at once is a machine whose design is not finished.

Two hundred and fifty-six moods

The controller in that box was not built wrong. It was built piece by piece over the years.

Open the program and you find eleven rungs and eight latched bits. Each rung has a reason on its own. Green_On seals in when the sequence starts and drops when Amber_On picks up. Amber_On seals in from the green timer and drops when Red_On picks up. Ped_Clear is set by the crossing timer and cleared from two different places. One of those places was added later, in a different pen.

Nobody designed this. It piled up. Somebody wanted a warning before the lights changed, so a bit was added. The night shift found the crossing time too short, so a timer and another bit were added. A driver ran the red, so an interlock was added. That interlock needed to know whether the crossing phase had ended, so one more bit was added.

Eight bits, eight small decisions. Each was reasonable on its day and was tested. None was tested together with the other seven.

eight latched bits designed, drawn, tested reachable — and never once tested the ninth bit does not add one square. It doubles them. one INT that holds 0, 1, 2 or 3 has four.

Two hundred and fifty-six

That is how many combinations eight bits have. The design uses six of them. The other two hundred and fifty do not stay on paper; the machine can reach them. Nothing in a ladder program forbids a combination. A rung only says when a bit goes up and when it comes down.

The way it grows is worse than the number. Every bit you add doubles the combinations and adds one state to the design. Six out of eight, seven out of sixteen, eight out of thirty-two. The gap between what you meant and what the machine can do grows with every small change. It grows fastest on the machines people ask to change most. That means the important ones.

How it shows itself

You hear this problem before you see it. It always comes with the same sentences:

  • "It only does it first thing in the morning." A cold start enters a combination the warm plant never reaches.
  • "If you press it twice quickly it goes strange." Two events land in one scan. The rungs read them in an order nobody thought about.
  • "We switch it off and on again and it is fine." There is a combination the machine cannot leave on its own.
  • "He fixed that last year and the hopper broke." A condition added to a latch to close one hole opens two more somewhere else.

The last one is what happened here. To fix a complaint, somebody made Ped_Clear clear from the vehicle-green rung as well. The complaint stopped. But in one particular order, when the vehicles were released early because nobody was crossing, the WALK lamp and the vehicle green were lit together for a few scans.

Nobody saw it. It lasted a tenth of a second and only happened when nobody was at the crossing. It went on for two years. In the end a driver saw the green light and the walking man at the same moment. He stopped and came in to tell somebody.

Why one INT ends it

Because the illegal combinations no longer exist.

State holds one number: 0, 1, 2 or 3. No value means green and walk together. A person cannot be in Ankara and İzmir at the same time. This is the same. No interlock prevents it. The register simply cannot express it. You moved the "only one at a time" rule out of the rungs and into the data. In the rungs it was a promise; in the data it is a fact.

Eight latches One state
every rung promises "only one at a time", none guarantees it "only one at a time" is a property of the register
a new request doubles the combinations a new request adds one number and its arrows
"what is it doing?" is guessed from eight lamps "what is it doing?" is read off a display
the untested combinations are invisible there are no untested combinations; every state fits on one line

The last row matters most. The state machine being tidier is not the real gain. The real gain is that it is countable. Four states, four arrows. You can walk through every one of them over a cup of tea.

The objection you will hear

"My interlocks do not allow those combinations."

An interlock is a rung too. It is a promise about a bit written somewhere else. The promise holds while both writes land in the same scan. The day one of them has to wait for an input, a timer or an edge, they stop landing in the same scan. Then the combination you called impossible goes out to the outputs for one scan. Page six shows what that costs.

An interlock patches a design that allows the fault. A state is a design that does not contain the fault.

Nobody sets out to build a machine with two hundred and fifty-six moods. You build one with two, and then for eleven years you give everybody what they ask for.

The pattern, in full

Here is the whole pattern. It is shorter than you expect.

One register. State is an INT in memory. Every state of the machine is a number. You choose the numbers.

Two kinds of rung, and no others.

  • An output rung asks which state you are in and drives a coil. It reads State and never writes it.
  • A transition rung asks which state you are in and adds the condition for leaving it. Then it MOVEs the next number into State. It drives nothing else.

One rule. Nothing but the transition rungs writes State. Not a Set coil. Not an ADD. Not a MOVE tacked onto the end of an output rung because it was convenient. If two different places can write it, you have built latches again.

The same instructions on both platforms

This pattern needs no new instruction. Both platforms have the same instructions under different names:

Job Siemens Rockwell
equal? CMP == EQU
not equal? CMP <> NEQ
write a number MOVE MOV
coil --( )-- OTE
set / reset coil --(S)-- / --(R)-- OTL / OTU

Here is a whole machine written in the pattern: the crossing on the main road outside the school gate. It has the same shape as the one in the yard, but different timings.

OUTPUT RUNGS — read State, drive one coil TRANSITION RUNGS — read State, write the next one State == 1State == 2State == 3State == 0State == 1State == 2State == 3 Veh_GreenVeh_AmberVeh_Red MOV 1 → StateMOV 2 → StateMOV 3 → StateMOV 1 → State TON T_TrafficPT 20 sTON T_WarnPT 4 sTON T_WalkPT 10 s Enable no rung does both, and nothing else in the program writes State four states, four arrows, seven rungs, one register

Reading the drawing

The top block describes the present: in state 1 the green is lit. It does not say "the green comes on when". The green is worked out again every scan for as long as the machine runs. An output rung has no event in it and nothing to remember.

That is why these are ordinary coils and not Set coils. A Set coil is a second memory standing next to State. A second memory is a second opinion. When State changes, an ordinary coil follows it in the same scan. A Set coil stays lit until somebody writes the reset rung. One day somebody forgets to write it.

The bottom block describes events: in state 1, once the vehicles have had their twenty seconds, go to state 2. Each rung says which state it may fire from. That is not decoration. Remove the State == 1 compare at the front and the rung fires in every state. It fires while somebody is crossing too, and you release the traffic onto a man with a walking stick.

The rung people forget most is the first one. Something has to take the machine out of 0. It is an ordinary transition rung like the others. Leave it out and the crossing sits in a state where no lamp is lit and there is no way out. From the yard it looks like a blown fuse.

What is State at power-up?

Here the two platforms differ, and you need to know it.

  • On Siemens, M memory and data blocks are not retentive by default. When power comes back, State is 0.
  • On Rockwell, tags keep their values by default. If the machine was in state 2 when the power went, it comes back up in state 2. If you do not want that, add a rung. It uses the first-scan bit S:FS to write 0 into State.

In this simulator, downloading the program is a cold start and State starts at 0. Keep the same rule on both platforms: 0 is the state it is safe to wake up in.

Choosing the numbers

You choose the numbers. Choose well.

  • 0 is where you wake up. Design it and make it safe. Nothing turns. No lamp is lit that anybody could read as "go". On a conveyor it means "stopped", on a door "shut", on the crossing above "not started yet".
  • On a big machine, count in tens. 10, 20, 30 instead of 1, 2, 3. If you ever need a state in between, it becomes 15. You do not renumber every rung, every drawing and the operator's card.
  • Put faults at the end, side by side. 90, 91, 92. Then State >= 90 means "faulted" in a single compare. It drives the beacon and stops the sequence too.
  • Do not give one number two meanings. A state 7 that means "closing, and the curtain was broken earlier" is really two facts. When a third one comes along, you are back to the arithmetic of page two.

If the work order gives you the numbers, use them. The drawing on the wall, the display on the panel and the documents must agree with each other. They do not have to agree with me.

How much does rung order matter?

Transition rungs run top to bottom within one scan. They read State as it is at that moment, not as it was at the start of the scan. So a state entered by rung 4 can be left by rung 6 in the same scan. No time passes. The output rungs of that state never run even once.

You cannot see this happen. You see a state the display never shows and a lamp that never lights.

Two habits protect you:

  1. Every transition rung asks about its own state. The rung that leaves state 2 starts with State == 2. It looks at no other state.
  2. Where you can, pick a condition that cannot be true on the scan you arrive. A timer behind State == n starts from zero the moment you enter the state. Twenty seconds do not pass in one scan. The next page explains why that reset happens by itself.

That is why the drawing above works even when written in ascending order. The rung below the one that fires is always a timer that has just started. Replace one timer with a limit switch or a button and only the first habit is left. Then read two neighbouring rungs and ask: can both be true in the same scan? If they can, decide which one wins. Put it first and write the reason in the rung comment.

What the program looks like in the end

Rung Kind Asks Does
1 output State == 1 Veh_Green
2 output State == 2 Veh_Amber
3 output State == 3 Veh_Red
4 transition State == 0 and the isolator closed MOV 1 → State
5 transition State == 1 and twenty seconds MOV 2 → State
6 transition State == 2 and four seconds MOV 3 → State
7 transition State == 3 and ten seconds MOV 1 → State

Seven rungs, and each fits on one line of a table. With the two pedestrian lamps it is nine. With a request button, its lamp and its memory it is eleven. But it never gets tangled, because no rung depends on another rung. Each depends only on the number.

The first work order in the yard is this machine with its own timings and numbers. Do not copy the table. Draw the machine first; the next page explains how. The rungs come out of the drawing by themselves.

Seven rungs and a number. Everything else is a way of avoiding the decision about what the machine is doing.

Draw it before you open the editor

A state machine is drawn first and written second. The rungs are a fair copy of the drawing. If you write the rungs first, you have designed the machine with a mouse. There is no worse tool for the job.

The notation is just circles and arrows:

  • A circle is a state. Give it its number and a name a person would say out loud.
  • An arrow is a transition. It is drawn from the state it leaves to the state it enters.
  • The condition is written on the arrow. Not next to it. On it.
0 1 2 3 OFF TRAFFIC WARN WALK Enable T ≥ 20 s T ≥ 4 s T ≥ 10 s power-up every arrow carries the condition that fires it a circle with no arrow out is a machine that hangs

That is the machine from the previous page, drawn the way it should have been drawn first. Seven rungs became four circles and four arrows. It took the back of a delivery note and forty seconds.

Four checks you can only do on paper

Do these before you open the editor. Each takes a few seconds. Each saves you a day of fault-finding.

Every circle needs an arrow out. A state with no way out is a machine that hangs. From the yard it does not look like a program fault. People say "the door just stopped", and everybody measures the limit switch first. Go round each circle and point at its exits. If a state has none, either you forgot a transition or you added a state that should not exist.

Every circle needs an arrow in. A state no arrow reaches can never be tested. It sits in the program for years looking as if it works. Usually it is a state that was split in two, with only one half wired up.

No two arrows out of one circle may be true at once. On a real machine this happens often, because a button and a limit switch do not consult each other. When it happens, rung order decides the outcome. Rung order is not a decision anybody made. Either narrow one of the conditions or write the priority down: if both, the STOP arrow wins. Then put that rung first.

Two arrows with the same condition on them are the same fault. People draw that when they have not decided something. Make the decision.

Which circle do you start in?

The short arrow at the top of the drawing shows the state the machine wakes up in when power comes on. On Siemens and in this simulator, State is 0 at power-up. On Rockwell the tag keeps its last value, so there you write 0 on the first scan with S:FS. On both platforms the arrow points at 0.

So draw the 0. A designed 0 makes a cold start safe. An undrawn 0 means the first check fails before anything happens. The states are 1, 2 and 3, none of them is true, and there is nowhere to go. The post stays dark for two weeks. Nobody thinks of looking at the program instead of the fuse.

Then look at circle 0. It has one arrow in, the power-up arrow. It has one arrow out. No state goes back to 0. That is a decision, not an oversight. On this crossing the isolator is a starting handle, not a stop button. If yours is a stop button, draw one more arrow from every circle to 0. Write on it what happens to the lamps.

What the drawing gives you and the program cannot

Count the circles. That is how many times you must answer what is lit and what is turning here. "Nothing" is an answer too, given on purpose. Count the arrows. That is how many transition rungs you will write. You know the size of the job before you type anything.

People who will never open the editor can read the drawing. Derya can read circles and arrows. So can the electrician who comes out at two in the morning. He holds the drawing next to the number on the panel and sees in five seconds which arrow did not fire. Imagine handing the same man eleven rungs.

Keep the drawing with the machine. Put it in the box on the post, where the wiring diagram lives. A drawing in a folder in an office does not exist.

When you get to the dock door

The third work order in the yard has a door, two limit switches, a light curtain, a timeout and a fault. It gives you the state numbers too. What it does not give you is this: which of the things that can happen to the door deserves its own circle? That question is the whole design.

One question answers all of them: once the machine is in there, is it doing anything different from a state I have already drawn? A door that turns round on the curtain and climbs does the same as a door somebody sent up. The same contactor is in, the same limit is awaited, the same travel is timed. That is not a new circle. It is a second arrow into a circle you already have, and the condition on it is the curtain. A door standing halfway with nothing energised is none of the states you drew. If the work order gives you a way to leave the door there, that state earns its own circle.

Get this division right on paper and the program is short. Get it wrong and you draw a state you cannot leave or an arrow you cannot label. Both are cheap to find on paper. Both are expensive to find with a truck under the door.

Circles and arrows on the back of a delivery note. Nobody has found a better method yet.

Time inside a state

Most arrows in a plant are drawn by a clock. Four seconds of amber. Two seconds of warning before moving. Twenty seconds before stopping the vehicles again. All of them ask the same question: how long have I been in this state?

The timer that resets itself

Put the timer behind the state's own compare:

[ State == 2 ]---[ TON  T_Warn  PT 4 s ]---[ MOV 3 → State ]

A TON whose rung is false resets its accumulated time. This is true on both platforms. On every scan you are not in state 2, the timer waits at zero. On the scan you enter the state, it starts counting from zero. No reset rung or reset bit is needed. When you leave the state, it resets itself again and is ready for next time.

This is the most useful feature of the pattern. It is the most natural way to say "time spent in this state" with a TON.

It also closes the hole from page three. A rung further down cannot take you out of a state you have just entered in the same scan. That rung's timer holds one scan, and four seconds have not passed. The timer is both the condition and the guard.

Measure and act on the same rung

Remember from Chapter Three: in this editor the timer's done bit is the power flow leaving the box. On real platforms you can also read the timer's fields on other rungs. On Siemens they are T_Warn.Q and T_Warn.ET; on Rockwell, T_Warn.DN and T_Warn.ACC. But in a state machine the clearest shape is still the same: the rung that measures the state and the rung that acts on it are the same rung. State, timer and MOVE, left to right. The rung reads exactly like the arrow it came from.

One timer, one box

Now the trap that costs you an afternoon the first time.

A timer's fields belong to the timer itself, not to the box. On Siemens they are PT, ET and Q, and they live in an instance data block. On Rockwell they are PRE, ACC and DN, and they live in a TIMER tag. Two boxes naming T_State are two boxes writing one accumulated time. Every scan, the box whose rung is true adds ten milliseconds. The box further down has a false rung and resets the same time straight away.

The symptom is this: a timer that never counts past one scan. Put the time in a watch window and it sits at ten milliseconds or at zero. It never climbs. Every rung looks right, but no time ever runs out.

So give every timed state its own timer. T_Traffic, T_Warn, T_Walk: one tag each, as in the drawing two pages back. Timers are cheap. Being wrong because of one is expensive.

If you are given fewer timers than timed states, there is a reason. Either the states share a time, or the preset is meant to live in memory. The preset is an operand too. Each state writes its own time into a variable, and one box uses that variable. In this simulator you write plain milliseconds into it: 4000, not T#4s. On Rockwell the PRE is already a DINT in milliseconds. Write the seconds in the rung comment so the reader can find them. Both ways are correct. Two boxes on one timer is not.

Timeouts that advance and timeouts that fault

The same box and the same four seconds can be two completely different arrows. Decide which one it is before you draw it.

A timeout that advances is the design. Amber lasts four seconds because that is the job of amber. The time running out is not news. It is the plan, and it happens on every cycle.

A timeout that faults is an alarm. A dock door may be given eighteen seconds to travel. A healthy door takes twelve on a cold morning. If eighteen seconds run out, the door has not arrived. The cause is a slipping belt, a seized roller, a broken limit switch or a trailer parked in the opening. None of that is the plan. The arrow goes to a fault state and does not return to the sequence on its own.

To tell them apart, ask one question: what would I do if this time ran out on every cycle? If the answer is "nothing, that is normal", it advances. If the answer is "I would go and look at the door", it faults. Then the arrow must go somewhere that sends somebody to look at the door.

There is a third kind too, and you will need it on page seven: a timer that measures a minimum. It does not say "you must leave by". It says "you may not leave before". It sits in the transition rung next to the real condition. It is what stops a button turning the road into a car park.

Choosing the number

For a timeout that advances, the number is the specification and comes from the work order.

For a timeout that faults, you choose the number. It should sit in the middle of a wide gap: comfortably longer than the slowest healthy cycle, and comfortably shorter than the time it takes to do damage. Too short and the plant faults every cold morning. Within two weeks somebody doubles it to stop the phone ringing. Now it is too long and nobody remembers why. Too long and the motor pulls against a jammed door for half a minute while the winding heats up.

Measure the machine. Ten cycles from cold, with a stopwatch. Take the slowest and add half of it again. Write both numbers in the rung comment: what you measured and what you set. Whoever comes after you will want to know which is which.

A timer always answers one of three questions. How long have I been here? Have I been here too long? Have I been here long enough? Decide which one you are asking before you type a number.

Illegal states and invariants

Before you write a program, write down the things that must never be true. Not what the machine should do, but what it must never be caught doing. These sentences are called invariants. They are worth more than the specification. A specification describes a good day. An invariant holds every day.

They are all short and plain:

  • The raise and lower contactors are never in together.
  • The vehicle green and the WALK lamp are never lit together.
  • The door never moves down while the light curtain is broken.
  • The motor never turns with no state number on the display.

Write them at the top of the drawing, before the circles. They will still be true when the machine is twenty years old.

One scan is a violation

This is the sentence people argue with most, and they are wrong: a violation still there at the end of a scan is a violation.

LOWER RAISE scans both contactors closed ten milliseconds of phase-to-phase across the motor terminals the contactor does not know you only meant it for one scan

The words "end of a scan" matter. A Siemens CPU reads the inputs into the process image at the start of the scan. It runs the program against that picture. At the end of the scan it writes the output image to the terminals. This simulator works the same way. A coil that comes on at rung 4 and goes off at rung 8 of the same scan never reaches a contactor. The field only sees the picture the scan finished with.

On Rockwell it is different. On ControlLogix and CompactLogix, inputs and outputs are updated at the module's RPI, independently of the program. An input can change in the middle of a scan. An output can be sent to the module in the middle of a scan too. So on Rockwell a short overlap is even more likely to reach the field. The rule on this page matters even more there.

The end-of-scan write on Siemens is both a help and a trap. An overlap can sit in a program for years and never reach the field. Then one edit lets it out. Once the write that clears the old output and the write that sets the new one land on different scans, both go out to the field together. They stay there for one scan. That is the drawing above.

Ten milliseconds looks like nothing. But these two contactors are wired to the same three phases with their outputs crossed. When both close, two phases are shorted across the motor terminals. The result is not small. Contacts built to break a load start to arc. The contact tips pit. The mechanical interlock hammers. The supply dips so hard that the lights flicker two rooms away.

It does not fail the first time. It fails after eleven thousand cycles, on a Sunday. The contactor is replaced and the fault is never found. The evidence lasts a tenth of a second and happens at a moment nobody records.

That is why grading in this yard reads the lamps after every scan, not at the end of the test. That is the picture the terminals receive. An illegal pair lasting one scan at a changeover is exactly the fault these work orders are built to catch. If that seems harsh, remember the other way is to learn it from a contactor.

Where the overlap comes from

There are three sources. You will meet all three:

  1. Two rungs driving the same output. One drives it from the sequence, the other from a manual jog. The last coil in the program wins. No warning appears, because writing the same coil twice is not forbidden.
  2. A coil that drops one scan late. Motor_Up is sealed in. Motor_Down picks up on a new condition. The rung that breaks the seal is further down the program. If the seal breaks in the same scan, the field never sees the pair. If an input, a timer or an edge makes it break on the next scan, both contactors reach the field together. You cannot tell which one you wrote by looking at the program.
  3. An output driven from a state and from something else too. This is what ruins good state machines. The pattern was correct. Then somebody added a bypass.

How the pattern removes it

State == 2 and State == 3 cannot be true at the same time. If only state 2 drives raise and only state 3 drives lower, you do not have to enforce the invariant. It becomes impossible to write. No value of the register lights both. No rung order can produce it.

This has one condition, and it is absolute: every output is written exactly once in the whole program. One coil, one rung, one state. If Motor_Up appears on a second rung anywhere, you are back to the first source. Then the register cannot help you, because it is no longer the one deciding.

Use ordinary coils, not Set coils. A coil driven from State == 2 goes off by itself when the state changes. "Off in every other state" comes for free. A Set coil (OTL on Rockwell) holds until somebody writes a reset. Then the output has a memory that State knows nothing about.

Write the invariant as a rung

You have written the sentences. Spend one rung on each important one:

[ Motor_Up ]---[ Motor_Down ]---( Both_In )

Put this rung at the bottom of the program, below every rung that writes an output. Then it sees the picture about to go to the terminals. Give it a lamp of its own. If you hang it on an output that already has a rung, you have built the first source with your own hands.

This rung costs nothing. In a correct program it never lights. When somebody adds a bypass on a Friday evening, it turns a two-day mystery into a lamp that lights on the first try. That is how a plant learns over time.

You cannot catch every invariant this way. "The door never moves down while the curtain is broken" looks at the door's movement. By the time a rung can see it, the contactor has already been in for a scan. Do not try to catch invariants like that. Design them out. That is what the circles are for.

The contactor does not care that you only meant it for ten milliseconds. Nothing in a plant that can hurt somebody cares what you meant.

Requests, memory and fairness

A crossing that stops the traffic every ninety seconds, whether anybody is there or not, is ignored within a week. Drivers learn. They go round behind the racking, past the tank farm. There is no crossing there and nothing to slow them down. Your careful sequence has made the yard more dangerous than before.

So the second crossing gets a button. A button is a different thing, because it always arrives at the wrong moment. Nobody presses it while the lights are already changing for them. They press it in the middle of the traffic phase. They press it just after the amber starts. They press it while the last person is still crossing.

Three wrong answers

Act on it at once. The traffic is moving, somebody presses, the light turns red. A forklift with two tonnes of glass on its forks cannot stop in one metre. A driver who cannot stop goes through. The person on foot now has a green man and a forklift in front of them. The machine you built to protect that person has lied to them.

Ignore it while busy. Then the button "does not work". People press it eleven times and then walk anyway. Within a month nobody looks at the lights. A button that is sometimes ignored is worse than no button, because a button is a promise.

Wire it straight into the transition. State == 1 and Cross_Req means state 2. It works when you test it holding the button down. In the yard a press is a sixty-millisecond jab from somebody who does not even slow down. The transition rung only listens at certain moments. Most presses are lost. The button works one time in three, and nobody can tell which time.

The right answer: remember the press

A press is an event. A state machine only works with conditions that are true right now. So put a memory between them. Let it be the only extra bit in the program:

  • Req_Held is set the moment the button is pressed. In any state, at any time. Nothing else is asked.
  • Req_Held is cleared in one place only. That place is the transition where the machine decides to serve the request. It is not the state where the person crosses. There is an amber between the two. A press during the amber belongs to the next crossing. Clear the bit late and you swallow that press.
  • The lamp in the button is not exactly this bit. It means heard but not yet served. So it stays lit through the amber, because the amber is that person's turn too. It goes out when the crossing opens. This lamp is worth half the program. It is the machine saying I heard you. It is why people stop pressing eleven times.

This bit has two rules, and both are required:

One meaning. Req_Held means somebody asked and has not been served yet. It does not mean "the crossing is busy" or "somebody is there". If a second rung starts reading it slightly differently, the disease from page two comes back. One bit instead of eight, but the same disease.

A request bit is not a state. It is the only latch this chapter allows. It is allowed because it remembers an event, not what the machine is doing. If you want a second one, what you really want is a new state.

About edges. Both platforms have an edge instruction to count a press as one event. On Siemens it is the P contact; on Rockwell it is an ONS placed after the contact. However long the finger stays down, the rising edge is one event. But you do not need an edge to set a request bit. A plain contact and a Set coil (OTL on Rockwell) do the same job. Setting a bit that is already set changes nothing. Use the edge when a second press must count as a second event.

Minimum time: protecting the traffic from the button

Now the part everybody forgets. If a request is served the moment it arrives, on a busy morning the traffic gets four seconds of green between crossings. The yard turns into a car park. The drivers go round the back again.

So the transition out of the traffic phase needs two conditions: the request and an expired minimum green. The forklifts get a fixed run before anybody can stop them again. This is the third kind of timer from the previous page: "you may not leave before".

Where does a request go if it arrives during the minimum? Nowhere. It waits in Req_Held with its lamp lit. It is served the moment the time runs out. That is the point of remembering it. An early press is neither lost nor obeyed at once. It joins the queue, and the person can see that it has.

Maximum wait: protecting the button from the traffic

The same idea has another end. If a request can be put off for ever, on a busy day somebody waits at the button for four minutes. After four minutes people cross anyway. You have not built a safe crossing; you have built a slow one. People get round a slow crossing.

Limit the wait. Once a request is held, it must be served within a set time. That number belongs in the work order, next to the minimum green. A machine that is fair on paper but unlimited in practice is not fair.

The one whose turn never comes

Once you see this, you see it everywhere. There are two requests and a priority. The low-priority one is only served when the plant is quiet. The plant is never quiet at the moment it matters. Two docks and one loading bay. Two hoppers and one screw conveyor. Serve in turn, not by rank. If somebody can explain why one may wait for ever, that is different. Then write the reason down, because the one who waits will ask.

The second work order in the yard is a crossing with a button, a minimum time and a memory. The request is the interesting part. The timing is what makes it fair.

A button is a promise. Keep it late if you must, but keep it. And light the lamp so people know you will.

Faults are a state

The dock door is the machine everybody in the yard avoids. They are right to. But the reason is not the door.

The reason is what the old program did when it disagreed with the door. There was a fault lamp. The lamp was a latch, and it sat beside the sequence like a note pinned to a wall. The lamp was lit, but the sequence kept running underneath it. The door was faulted and closing at the same time. In other words, nobody knew what the door was doing. Not even the door.

A fault is not a lamp. It is a state.

That is the whole page. The rest is the reasons.

If the fault is a latch beside the sequence, the sequence keeps running and keeps driving contactors. If the fault is a state, the machine does nothing else, because a machine does one thing at a time. Every output rung asks State == n, and none of them asks for the fault state. So everything goes off by itself. You do not need a rung for that, because you used ordinary coils.

Still, give the fault state its own output rung and put the red fault lamp on it. "Everything off" should be visible in the program. It should not be guessed from the rungs that are missing.

A fault has one way out

On the drawing, many arrows go into the fault circle, but only one comes out. The condition on that arrow is a person: a key switch, or the reset button on the dock wall. Somebody who has walked over and looked at the door.

There is no other arrow. No timer. No retry. No "once the cause has gone".

"It cleared itself"

This is the most dangerous sentence in a plant. It is usually said with relief.

A fault that clears when its cause goes away is a machine destroying the only evidence. The obstruction was moved. The belt freed itself as it cooled. The truck drove off. That is also why most thermal overloads are set to manual reset: even when the motor has cooled, somebody should come and look.

The cause going away does not mean the machine is healthy. It means the machine was wrong once and nobody knows why any more. A machine that hides its faults teaches the people around it that faults are not worth reporting. That is how a plant goes quietly blind.

Reset is not carrying on

When somebody turns the key, do not jump back into the middle of the sequence. Go to the safe state: 0, motor stopped, everything off. Let the operator press OPEN or CLOSE as they did the first time.

Carrying on assumes the world stood still during the fault. It did not. Somebody put a pallet under the door. The trailer left. The man who turned the key is standing in the opening with his hand still on it. If you carry on, the door finishes a decision it made four minutes ago in a yard that has changed.

Number your faults and the diagnosis comes free

Faults are states. So on a machine that can stop for several reasons, give each reason its own number:

State Why the door faulted
90 travel timeout: the door did not reach its limit switch
91 both limit switches made at once: a switch or a wire has failed
92 the curtain turned the door back four times in one close: something is in the opening, or the lens is dirty

Now the display on the panel is the fault code. No extra rungs, no list to keep in step. The fitter reads 91 through the glass and goes straight to the limit switches. He does not watch the door for twenty minutes hoping to see the fault.

A door with one reason to stop has one row in this table. The work order gives its number. The length of the table does not matter. A fault is a state, and a state is a number you can read through the glass of the box.

Keep the first cause. If a second thing goes wrong after the machine has faulted for one reason, the number must stay the first one. The first one explains the second. So the rung that writes a fault number starts with "if not already faulted". Later, in alarm handling, you will capture the first alarm across a whole panel. Here one guarding compare does most of that job.

Some faults are not sequence faults

Both limit switches made at once is not a door position. It is a broken switch, a shorted cable, or a fitter who adjusted one and forgot the other. No sequence can make that normal. But a state machine gives it somewhere to go. A pile of latches never does. That is half the value of the pattern: impossible things now have an address too.

Draw the door before you write it. Ask the question from page four for every event. A fault is a state: nothing turns, a lamp is lit and a person is needed to leave it. A reversal is not a state. A door climbing after the curtain breaks does the same as a door somebody sent up. It is one more arrow into a circle you already have. Get this right on paper and the door is a short program. Get it wrong and you spend the afternoon inside a state you cannot leave.


The chapter comes down to one sentence. The register is only how it is kept:

A machine does one thing at a time. Write down which, in one place. Then every rung in the program becomes a sentence about a single number.

Outputs stop being latches and become descriptions. Transitions stop being interlocks and become arrows. Illegal combinations stop being unlikely and become impossible to write. The electrician who comes out at two in the morning does not interpret eight lamps on a wall. He reads one number off a panel, compares it with the drawing behind the door, and is home by three.

In pencil, in the margin of the last page, pressed hard: "They will tell you a state machine is more work. It is the same work. The difference is that you do it before the plant runs, on paper. There, being wrong is free."