How to implement a State Machine for your Embedded project

How to implement a State Machine for your Embedded project

When you are working on an embedded device, you usually start simple. A main loop that does something. 
As you start to add more features, the complexity of the code grows. You add more ifs, more loops, more conditions, and even, god forbid, switch statements. 
This is the point when you should to pause and ask yourself: "Am I doing the right thing?"

There must be a better way... 
Better way to make these kind of systems, which is not tangled, hard to debug and hardly testable.