I got tired of stirring my espresso grounds so I made a machine do it for me

Finite state machine (FSM) diagram:

With a flushed out FSM, the code becomes much simpler to write. All routines within each state can be its own function.