you should take a look at the BlinkWithoutDelay example.
You can find it on your IDE or here:
the idea would be something like this:
Define an interval (you said you want 5s).
You would need to save the millis() into a variable at the moment you want the Arduino so start counting the 5s (ex: when the door is opened).
Then on each loop you can check if the difference between the previous millis() (which you save into a variable) and the current millis() is equal or bigger than you interval. In other words: you can on each loop check if 5 seconds have passed.
It would be something like this:
(i'm skipping parts of code, just writing the relevant for the "interval checking, but nothing for the door,...)
// create variables
long openTime;
int interval = 5000;
// ...
// open the door
// save time at which door was opened
openTime = millis();
// if the door is open
// check for interval
if( millis() - openTime > interval) {
// close the door
}
Good luck!
![]()