LED Button with auto timer off

Hello together,

I have a question,
I use this code to turn on and off a LED.
So first Button Press is On then it stays on , second press is off ...

But how can I add an auto shutoff.
So I press the button and the LED turns on, after 5 min if there is no other Button press for off, the arduino should shut off the LED.

Press - LED ON
AFTER 5 min LED ON - LED OFF
Or if I press between the 5 minutes the LED also should turn off.

int inPin = 5;         // the number of the input pin
int inPin2 =6;
int outPin = 13;       // the number of the output pin

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin
int val = 0;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers


void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(inPin2, INPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;


  val = digitalRead(inPin2);   // read the input pin

 {
      if (digitalRead(val == HIGH))
      {
     digitalWrite(outPin, HIGH);
     delay(1000);
      
      //} else {
    digitalWrite(outPin, LOW);  // turn LED ON
  }
}

}

regards Tim

Do you know how to use millis() ? If not, have a look at Blink without delay or Several things at the same time.

With that, save the time where you turned on the led. And at an extra check to see it the time has passed in the mean time if the led is on.