To do this I think you need to understand the StateChange example and record the time when the sensor first becomes blocked.
Then, using the state change concepts you could rewrite your code to:
- turn on the motor when it becomes unblocked
- turn off the motor when it becomes blocked (and record the time)
- turn on the motor when it has been blocked for 5000ms
You don't need to re- turn on the motor if it is already running every time you go through loop.