Delay on relay when power on & instantaneous contact on power off

Hello, guys. I`m newbie with arduino as well as c programming :confused: .

I would like to have a time relay which could delay on power on and contact ASAP when power off.

I tried this simple code and I have both delays, on power on/off. When I remove delay function I have instantaneous contact on power on/off.

int const outPin = 13;               
int const inPin = 2;

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

void loop()
{
  
  if (digitalRead(inPin) == HIGH)
  {
    delay(2000);
    digitalWrite(outPin, LOW);   
  }
  else
  {
    digitalWrite(outPin, HIGH);
  }
}

The problem is that it is going to take the program up to 2 seconds to recognise that the switch has been released because it can't read the switch pin during the delay.

Consider using millis() for timing so that the switch can be read each time through loop()

See the BlinkWithoutDelay example in the IDE which shows the principle.