Kata // Spec

The 'washing machine' needs you to implement specific behaviour to control the hardware. Wash cycles, multiple programs, valves, powder, softener, countdown timers and sensor input. Read the spec in full here.

Kata home page

Wishy Washy

Run it!

One of the best ways to understand the intended behaviour of the washing machine is just to run a solution. You can download an ihex-format release here , and flash it to the ST Nucleo target using your debugger of choice.

High level description

At the highest level, the washing machine’s operation can be divided into two main modes, or states. During wash program selection, the operator is in control, pressing buttons, and operating switches.

After the operator initiates the wash program operation, the washing machine is in the driving seat until the wash program ends. The state transitions and operation within a wash program depend on the selected program, and different wash cycles.

During a wash program, the human has to operate the fill and temperature dials, but this is just to simulate the sensor operation.

An image of the washing machine's high level state model

We’ve omitted a lot of the detail in the RUNNING state - there’s a lot going on in there, and it depends on the selected wash program. We’ll break this down a little later.

Functional walkthrough

A small hint on notation here: this description uses CODE style formatting to denote physical elements of the board, such as user buttons and LEDs. We use BOLD UPPER CASE to denote states, and these correspond to the states named in the state machine diagrams.

Startup state

On starting the machine should always enter the SELECTING state, and display P1 as the selected program in the PROGRAM DIGITS display. On startup, the door LED indicator to the bottom right of the board will always honour the initial state of the door switch. Closed means the LED is OFF, open means the LED should be illuminated. So, more specifically, it doesn’t just enter the SELECTING state, but either one of the READY or NOT READY states, depending on the switch position.

All other LEDs are off. Of the control LEDs (the bottom right-hand bank), only the OPEN indicator plays any role in this state.

The motor should be stopped, and none of the circular MOTOR SPEED display LEDs illuminated. You can choose to implement a “health blinky” for the LD2 indicator on the Nucleo. This is optional, but sometimes helpful (hint: and sometimes a good, simple starting point).

Program selection

During wash program selection in the SELECTING state, the timer digits to the right hand side should remain blanked.

The user can select the program they want to run by pressing the UP and DOWN buttons. When the UP button is pressed, the selected program will be incremented and the new program displayed. So, from startup, pressing the UP button a single time will display P2, a second time P3 in the PROGRAM DIGITS display.

The machine should ignore an UP button press that would increment beyond P3, and a DOWN button press that would decrement lower than P1. No error or warning needs to be given, it’s sufficient to ignore the user action.

The user can operate the door switch in the SELECTING state: opening the door will cause the machine to switch to a NOT READY sub-state, closing it to a READY substate. In the NOT READY state, the OPEN indicator is illuminated, in the READY state, it is off.

Technical note: when we we say disable we mean “subsequently ignore until the state machine says it’s enabled again”. So, don’t make the literal assumption that this must mean reconfiguring peripheral hardware…although you could if you wanted to…it’s up to you.

Starting the selected wash program

The user can press the START button at any time. During program selection, if the door is open and the system in the NOT READY state, a START button press will be ignored.

After closing the DOOR to enter the READY state, pressing the START button will have the effect of starting the selected wash program.

Transitioning to running a wash program

This is a significant state transition, and causes a number of changes in the posture of the machine.

After initiating a wash, all user input is ignored. Pressing the UP and DOWN buttons have no effect. Pressing the START button has no effect, and for clarity, does not have the effect of restarting the program.

The DOOR switch becomes impotent. It’s a physical switch, of course, so there’s nothing to stop the user sliding the switch back and forth, but the OPEN indicator LED remains off during the wash.

The machine will leave the selected wash program illuminated during the whole wash, but as soon as the wash program is started, the time remaining for the program is displayed (which will be different for each of the three wash programs).

Washing operations

The Washing Machine supports four different cycles, which are then combined into three different wash programs. One program runs all of the cycles, the other two use a subset.


There are four cycles. Some aspects of a cycle are fixed, but other things can vary from one wash program to another. Taking the wash cycle as an example, the wash temperature for this cycle might be hot in one wash, but only warm in another.

The variables are:

  • The duration for a cycle can vary between one wash
  • The target water temperature (if water is used) can vary between programs
  • The target water fill level (if water is used) can vary between programs

Here’s a summary of the cycles and the variable and constant factors for each. Where something is variable, the value is defined in the next section covering the wash programs.

Cycle Duration Water Level Treatment Temperature Motor
Wash Varies Varies Detergent Varies Alternating
Soften Varies Varies Softener Varies Alternating
Rinse Varies Varies N/A N/A Alternating
Spin 10s N/A N/A N/A Ramps to max

It’s worth stressing the fixed aspects of the system behaviour as well as the variable:

  • The wash cycle always fills the drum with detergent as well as water.
  • The soften cycle always fills the drum with softener as well as water.
  • Although the temperature target varies, the wash and soften cycles always have one, and the rinse and spin cycles never do
  • The things that cause transitions in the cycle never change, regardless of duration, target temperature or fill level.
  • Although there are two different motor programs, the same one is always used for a given cycle - alternating, or ramp-up.
  • The “wet” cycles - wash, soften and rinse - have essentially the same three-stage transitions of fill, operate, then drain. For all three of these, the timer countdown only commences on fill, and then pauses at the end of the operate phase.

On the approaches we talk a little more about how to break down the problem into more sensible chunks, and these fixed aspects of the system behaviour are a help in doing that.

The wash cycle

The wash cycle operates between three states, and operates on the fill and drain pumps, the powder valve, the timer, and the heater.

An image of the wash cycle state model

As with all of the “wet cycles”, the user has to intervene to simulate the sensor levels using the rotary potentiometers, “filling” the water level in the machine and setting the water temperature.

The soften cycle

Another “wet cycle”, the soften cycle has the same basic operation as the wash cycle. The primary difference is that the SOFTEN control is opened as the water is filling the washing machine drum.

As with all of the “wet cycles”, the user has to intervene to simulate the sensor levels using the rotary potentiometers, “filling” the water level in the machine and setting the water temperature.

An image of the soften cycle state model

The rinse cycle

Another “wet cycle”, but with a couple of significant differences from the wash and soften cycles. The rinse cycle doesn’t add anything as the drum is filled, only water, so no additional valves are opened.

The other key difference is that the rinse cycle always uses cold water, so the thermostat is never activated. If the operator increases the temperature using the dial, this is ignored and the HEAT control is never illuminated.

An image of the rinse cycle state model

The spin cycle

The spin cycle is fundamentally different from the preceding three. The spin cycle never fills the drum with water, or any additive. It always has a fixed duration of 10 seconds, and runs to a fixed plan, ramping up the motor power successively at 2-second intervals.

See the specification below under “Notes on motor operation” for details of the power settings.

Wash programs

There are three different wash programs that have to be implemented for our Washing Machine. For clarity, we include the spin cycle, although it never varies between wash programs.

In effect, a wash program is nothng more than one or more cycles run back-to-back, with the ability to vary the parameters of temperature, fill level, and duration. For this reason, we don’t show a state machine for each.

The entry and exit events shown on the high level FSM are the same for all variants of a wash program. The parameters vary, covered in the tables below.

Full Hot Wash

This is the only wash program that runs all four cycles, so it’s also the longest at 45 seconds, excluding filling and draining times for the “wet” cycles.

Cycle Duration Water Heating
Wash 15s 75% 75%
Soften 10s 50% 50%
Rinse 10s 75% N/A
Spin 10s N/A N/A

Eco Wash Rinse and Spin

The cool wash skips the soften cycle, and has both cooler temperature targets and lower water fill targets. It’s shorter at 30 seconds, excluding filling and draining times for the “wet” cycles.

Cycle Duration Water Heating
Wash 12s 25% 50%
Rinse 8s 50% N/A
Spin 10s N/A N/A

Spin Only

This wash program only runs the spin cycle, with its fixed duration of 10 seconds, and fixed pattern of ramped-up motor operation.

Cycle Duration Water Heating
Spin 10s N/A N/A

Notes on motor operation

The motor works in only two different modes, and the mode is fixed for a particular cycle. In wash and soften, the motor alternates off and on, at 2 second intervals. “On” is always low power, which should light 4 of the circular display LEDs, so approximately 40% duty cycle. This mode is used for wash, soften, and rinse cycles.

The other mode is ramp-up, which is only ever used in the spin cycle. This never varies. In this mode, the motor power is increased in 2-second steps up to max power, before dropping to zero at the end of the spin cycle:

Timing Power Notes
0 seconds min approximately 20% duty cycle, or 2 x LEDs
2 seconds low approximately 40% duty cycle, or 4 x LEDs
4 seconds medium approximately 60% duty cycle, or 6 x LEDs
6 seconds high approximately 80% duty cycle, or 8 x LEDs
8 seconds max 100% duty cycle, all LEDs

Notes on fill level and temperature

The analog sensors operate as 12-bit ADC conversions, so you have a range of values 0-4095 for each channel. The spec is expressed in percentage terms, so we’ll leave the arithmetic to you. Enumerations (scoped enums in C++ are more typesafe…) are a good way to express these in your code.

Notes on the progress indicator

Whenever a cycle becomes active, the indicator LED for that cycle has to be illuminated, and any other previous illuminated indicator switched off.

Notes on the countdown timer

The countdown cycle should display the total wash program time in the TIMER DIGITS as soon as the selected wash program is started. But this is only an approximation - it’s the sum of the OPERATING phase durations of the cycles that make up the program. During the FILLING and DRAINING phases, the countdown timer is paused.

Program completion

When the program ends - always when the timer reaches zero for the last cycle - the display should be set to all dashes -- --. A value of zero should never be seen on the two timer digits: when zero is reached, the display is shown as all dashes -- -- to signal program completion.

In this state, the only action the user can take is to open the door. All other inputs are ignored.

Dealing with electromechanical controls

Some of elements of the design have physical positions - the sensor potentiometers and the door switch. Our MCU doesn’t have telekinetic powers, so the position of these have to be respected by the code. Specifically:

  • When the system reaches the unloading state, and the user has set the door to open, the system has to transition without delay to the programming state.
  • During a cycle that needs to fill the drum with water, the current state of the water sensor potentiometer is respected, and the drum is taken to be at that fill level. If the sensor reads higher than the target fill level, the wash cycle is started without delay.
  • If the systems is reset with the door set to closed, the system should be in a ready state, as if the door was closed.