This is the ladder logic portion of the state machine.
I chose not to use a ladder-logic COUNTER because of your requirement to have the loop count adjustable. I chose to use custom functions to manage a software counter that sets a RELAY, LoopCntrZero, to indicate when the loop count has expired.
I would have used a ladder logic COUNTER if the loop count is always the same.
Gary D