I'm a newbie myself, I understand it is frustrating coming to the forum for assistance and being told that you need to read a tutorial and figure it out. If you understood the tutorial you wouldn't have asked the question (a reasonable assumption.) Instead of telling you what you already know that you don't understand, I thought I'd learn with you and maybe help you get to the final solution a little quicker.
Eventually you'll probably want to get rid of the delay between the servo positions and use millis, but the code below should get you started. I've added the reset button to pin 8, but it can be any digital pin, it should probably be debounced which I prefer to do with hardware instead of software because it makes the sketch so much simpler and my wheelhouse is hardware not software. If you're more intimidated by hardware than software or you want to learn more about programming just for the sake of learning there's a tutorial on debouncing, I believe there's a debounce library that is supposed to make software debouncing more managable as well.
#include <Servo.h>
Servo door; //calling servo the door
int sensor = 7; //sensor is on pin7
int relay = 6; //relay on pin6
int resetButton = 8; //reset button on pin8
int detect = 0; //setting sensor to open
int previousDetect;
boolean blockRelay = false;
void setup()
{
pinMode(sensor, INPUT);
pinMode(relay, OUTPUT);
pinMode(resetButton, INPUT);
door.attach(5); // servo on pin5
previousDetect = HIGH; //set previous detect to high for first cycle
}
void loop()
{
detect = digitalRead(sensor);
//turn on relay only if relay has not been blocked by null detection time out
if (blockRelay = false){
digitalWrite(relay, HIGH);
}
if (detect == HIGH) {
door.write(90);
delay(2000);
door.write(45);
}
unsigned long nothingToDetectTime;
//if the sensor doesn't detect anything and previously had detected something
//set nothing to detect time to millis
if (detect == LOW && previousDetect == HIGH){
nothingToDetectTime = millis();
}
//if 5000 milliseconds have passed since nothing to detect time was set
//and detect is not currently high turn off relay and block it from coming back on
//until reset button is pressed
if (((nothingToDetectTime + 5000) < millis()) && detect == LOW){
digitalWrite(relay, LOW);
blockRelay = true; //block relay so relay won't be turned on in next cycle
}
//if reset button is pressed allow relay to turn on in next cycle
int resetButtonState = digitalRead(resetButton);
if (resetButtonState == HIGH){
blockRelay = false;
}
previousDetect = detect;
}