Hi there,
my best advice here would be the following:
- read the AN010 about FSMs here.
- use the FSM library instead of building it with code. One of the advantages is that the library has entry/exit state functions that fire off and can help to write your code better (in the sense of more localized/focused on one task and more isolated of other code). This in turn can help to troubleshoot later.
- go crazy with the number of states, don't try to limit the number just because.
Of course here, if you have a variable that counts to 1000 do not create 1000 states for it (there are other mechanisms for that on FSMs - if I find a reference I'll post it here).
Your sensor probably has many more states than the 4 you have drawn above.
For example: the first draft of my garage project had maybe 3 states, but when I started really thinking about it, and wanting to run code for when the garage was open, when the code was triggering a pulse to open the garage, when it was mid way to open, when it finally closed, when it was mid way being closed, etc, it ended up with maybe 15 states.
There's this previous discussion where @chipmc talks about the states of his project.
There are many others as well.
Cheers!