Thanks! This is what I was looking for. I was sure there had to be simpler way. Although I don't really understand what is going on with what you wrote, I will definitely try to figure it out.
Here's what it's doing:
Your system basically has to worry about three things: what are we doing, how long will we be doing it and what length of pulse we need to send to the servo to achieve it.
The state variable keeps track of what we’re doing. It will be START when the arduino is powered up, then it needs to change to POWER for a while, then alternate between CENTER and SHUTTER with appropriate delays.
NextAction is the time in millis when the next state change will occur. All your state changes are time based, so we just check on every iteration of loop to see whether that time has been passed. If so, we call ChangeState to set up what to do next.
Your state changes are simple, there are no choices to be made, if we are in one state and it’s time to change, the next state is always the same. The switch statement implements this and changes START to POWER, POWER to CENTRE, CENTRE to SHUTTER and SHUTTER to CENTRE. It also increases NextAction to the due time of the next state change so that we can stay in our new state for the desired duration. Finally, it sets the PulseWidth to whatever is appropriate for the new state and a pulse of that size will be sent to the server on each iteration of loop.
In this example, there are few states and only one event (elapsed time) to trigger state change, so the advantage of the method is somewhat limited. With more inputs & outputs and more states with a more complicated flow between them, it can be a nice way to model your system and encapsulate your change logic.