Stopping the program after 1 second
You need to use read the time (millis()) when you start the motor and read it frequently while the motor is running to know when to stop it. You also need to keep track of the sensor, and don't start the motor again until after you've seen the sensor turn off (detect no obstacle). Here is an outline of the logic in psuedo-code; turn this into a detailed loop() function in C++:

if the motor is ON {
  if (millis() - motorStartMillis) &gt; 1000 {
  stop the motor;
}
else if sensor is ON {
  if the sensor was OFF {
    start the motor;
    motorStartMillis = millis();
    sensorWasOn = true;
  }
}
else {      // sensor must be off
  sensorWasOn = false;
}