Cycles and State Machines

2026-12-03 @matyo91

Period and phase

A period tells you when a sequence repeats. Phase tells you where you are within one repeat. A greenhouse controller with four phases revisits phase 0 every four minutes.

01 | Darkwood Code Calendar @matyo91

Modulo gives a phase

With zero-based minute n, n modulo 4 gives 0, 1, 2 or 3. Minute 10 is phase 2. Carry a timestamp-derived index when a sensor can miss a reading.

02 | Darkwood Code Calendar @matyo91

An action and a state

A timer can request ventilation. A controller in maintenance can defer that request. Store the mode explicitly whenever the same input can require different actions.

03 | Darkwood Code Calendar @matyo91

Simulate before connecting hardware

Give the simulation a delay, a missing reading and a manual override. Compare expected transitions with actual transitions at each tick.

04 | Darkwood Code Calendar @matyo91

From signal to observations

In the Flow example, twelve samples form three cycles. Group by phase and count the states. Phase 3 has two idle readings and one vent reading.

05 | Darkwood Code Calendar @matyo91

When two loops meet

Independent loops of length four and six return to their joint starting phase after twelve ticks. A shared resource needs an explicit rule for simultaneous requests.

06 | Darkwood Code Calendar @matyo91