Relay timer

Hi everyone i hope this finds you all well. I am making a controller for my jeep rear door as the body control module has failed. The sequence is this. lift the door handle and the window drops out of the seal. Delay 500 then the latch fires and the tailgate can be opened . Now close the door and a switch in the latch activates to close the window again. Simple timed relays. Now here is my problem ... when the tailgate closes and the microswitch is engaged it Stays engaged so the window close relay keeps restarting its timed period. I need to make it a one shot switch. Delay is no good as it would stop the program from opening the door again and anyway it cant be long enough. I have tried "while" but same problem. I think the answer might be state change but i cant figure it out .. any help would be gratefully appreciated. Iv attached my sample sketch for leds read relays and my delays are long just for debugging.Thank you.

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int led_RED =  13;      // the number of the LED pin
const int led_GREEN =  12;
const int led_YELLOW =  11;
// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(led_RED, OUTPUT);
  pinMode(led_GREEN, OUTPUT);
  pinMode(led_YELLOW, OUTPUT);// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(led_RED, HIGH);
    delay(2000);

    digitalWrite(led_RED, LOW);
    digitalWrite(led_GREEN, HIGH);
    delay(2000);
    digitalWrite(led_GREEN, LOW);
    delay(5000);
  }
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {
    // turn LED on:
    digitalWrite(led_YELLOW, HIGH);
    delay(2000);
    digitalWrite(led_YELLOW, LOW);
  }


}

Sounds like a school project
You must learn how to write non-blocking code.

Delay is blocking so you can't use that.

Usually the millis() timer is used for this. e.g.

 if (millis()-startTime >= loopTime)  
{      
        startTime = millis();         // restart timer for
        ....                                // whatever that must be executed when the looptime has expired
}

Thanks for the reply and yes i agree about mills .. however how does that help the problem of my close window switch being permanently high. The door might be closed for months. i can fix this myself using a 555 timer in monostable to provide a one shot high to the arduino but was looking for a one board solution ... ps left school in 1969.

Sorry about the school remark. It's just that I've seen code similar to what you posted many times here.
I can't teach you programming or write the program for you. There is probably much more to the requirements as what you wrote down here.

The idea of non blocking code is that the loop is always polling. Constantly checking if there is an event like the door handle being lifted or a timer expired or a status changed. Switches in the program may control the route the loop takes.

If you want the program to react on the engagement or the release of a mini switch you must have it's previous status. e.g.

if (previousstatus !=currentstatus) //something changed
{
previousstatus =currentstatus;
if (currentstatus) do this..... //engaged
else do that.....//released
}

Again thanks for the replay.. yes i know im looking for a state change however iv opted to go the old school route with a power up monostable for my window shut timer/relay ... at least i understand that ha. And no worries on the school thing ...Cheers