     doorState = DOORSTATE_CLOSING;
   }
 
-  doorTimeLastOperated = 0;
   operateDoor(pin, cycles);
+  doorTimeLastOperated = millis();
 }
 
 void setLightState(boolean state) {

  sensorOpen = !(bool)sensorOpenDebouncer.read();
  sensorClosed = !(bool)sensorClosedDebouncer.read();

  if (sensorOpen && !sensorClosed) {
    doorState = DOORSTATE_OPEN;
  } else if (!sensorOpen && sensorClosed) {
    doorState = DOORSTATE_CLOSED;
  } else if (sensorOpen && sensorClosed) {
    doorState = DOORSTATE_UNKNOWN;
  } else if (!sensorOpen && !sensorClosed) {
    if ((doorState != DOORSTATE_OPENING) && (doorState != DOORSTATE_CLOSING) && (doorState != DOORSTATE_STOPPED_OPENING) && (doorState != DOORSTATE_STOPPED_CLOSING)) {
      if (doorStateLastKnown == DOORSTATE_CLOSED) {
        doorState = DOORSTATE_OPENING;
      } else if (doorStateLastKnown == DOORSTATE_OPEN) {
        doorState = DOORSTATE_CLOSING;
      } else {
        doorState = DOORSTATE_UNKNOWN;
      }
    }
  }

    doorTimeLastOperated = 0;
    doorStateLastKnown = doorState;
  } else if ((doorState == DOORSTATE_OPENING) || (doorState == DOORSTATE_CLOSING)) {
    if ((timeNow - doorTimeLastOperated) >= DOOR_MAX_OPEN_CLOSE_TIME) {
      // Door was opening/closing, but has now exceeded max open/close time
      doorTimeLastOperated = 0;

      if (doorState == DOORSTATE_OPENING) {
