I really can't seem to get my head around the programming of a state machine for some reason??
When it clicks how simple it is you will kick yourself.
Look at my example. It has 3 possible states namely red light on, yellow light on or green light on and will be in one of those states during program execution. I gave the states names using the enum to make the program easier to understand, but all it does is to allocate a number to each state.
The program start in state roadRed with the red LED turned on and the start time of the state in the stateStart variable. The switch/case executes only the code for the current state and will execute it each time through loop(). Once the red period has elapsed the program turns the LEDs on/off, saves the current time as the start of the next state and changes the state number.
Next time through loop() the code for the current state, which has now changed, is executed and the program and keeps going round loop() until the yellow period has elapsed then it moves to the green state, turns the LEDs on/off, saves the current time as the start of the next state and changes the state number.
The program would carry on like this unless something happens to alter the state such as a user input. As the execution program is not blocked by delay()s the user input could be read each time through loop() and could change to another state, perhaps pedGreen, change the LEDs appropriately and execute a new case to time the pedestrian phase of the program before setting the LEDs appropriately, saving the state start time, and changing the state number which will cause the program to execute the commands for the new state.
Note that the program as it stands does not read user input. That code needs adding once you have understood the principle of the state machine.