I would continuously measure every cycle and put each measurement into a circular buffer of 100 or 256 or 1024 measurements, so I could get an idea of mean and standard deviation. But that's me.
You want one measurement, presumably on demand (e.g. press a button to make a new measurement). There will be three states:
State 1) on-demand button was pressed,
- Assign zero to counter (any integer)
- Waiting for next trigger: rising edge on piston 1 prox AND in state 1
- On that trigger, Assign 2 to state
State 2) piston 1 prox occurred while in state 1,
- Counter increments by one (++ instruction) on each 50Hz rising edge
- Waiting for next trigger: rising edge of piston 1 prox AND in state 2 AND counter greater than zero
- On that trigger, assign 3 to state
State 3) piston 1 prox occurred while in state 2
- Accumulated counter pulses are put on display
- Waiting for next trigger: rising edge of on-demand button press
- On that trigger, assign 1 to state
This is far from the best approach, but it is simple and robust: three states, with a clear trigger for each and transition to the next.
The blue AND clause is important or else it could move from state 1 to state 3 in one cycle, although that can be avoided by reversing the order of the trigger tests.
Also, the button press rising edge trigger should probably not care what state the measurement is in, but that trigger should always initiate state 1.