IR Sensor operation

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:

  1. turn on the motor when it becomes unblocked
  2. turn off the motor when it becomes blocked (and record the time)
  3. 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.