OK, thank you both of all the help on Finite State Machines, my plan is to start rewriting the software for my trail counters using this approach. My next project is to rewrite the software to enable solar power. I am using some of the ideas provided by @RWB and I am sure I will end up making a lot of mistakes (I mean learning a lot) in the process. I will start by creating a state diagram for what I want to get accomplished. The words that go with this diagram are:
The device is initialized and connects to the Particle service
If It fails in initialization or connection, it will reset
Once connected it goes to an idle state and waits for an event
It will take a “nap” if nothing happens for a timeout period waking once there is a hardware interrupt from the sensor
It will also, “sleep” while the park is closed waking only when the park reopens
When it wakes from either a nap or sleep, it will report using a Webhook
The webhook will need to be successfully received for it to go back to the idle state.
Now, I can start to rewrite and test my code for each state and build a main loop to handle the transitions. I will post progress as this seems like a general use case for many Electron projects. Comments and suggestions always welcome.