Motion Sensor Keep Relay On For Certain Time

I have a motion sensor that turns a relay on when motion is detected. The relay is hooked up to a light.

I want to add a timer that will control how long the relay stays on before turning off again. I don't want to use delay because that will then cause the code to stop. I'd like the light to stay on for a period of time while the code still runs.

How can I accomplish this?

int motionSensor = 2;
int timer = 100; // set the global delay

#define RELAY_1  3                        
#define RELAY_2  4                        
#define RELAY_3  5                        
#define RELAY_4  6

void setup() {

  Serial.begin(9600);

  pinMode(motionSensor, INPUT);

  pinMode(RELAY_1, OUTPUT);       
  pinMode(RELAY_2, OUTPUT);
  pinMode(RELAY_3, OUTPUT);
  pinMode(RELAY_4, OUTPUT);
  
}

void loop() {
   
  digitalWrite(RELAY_1,HIGH);
  digitalWrite(RELAY_2,HIGH);
  digitalWrite(RELAY_3,HIGH);
  digitalWrite(RELAY_4,HIGH);
  
  int motionSensorVal = digitalRead(motionSensor);

  if (motionSensorVal == HIGH) {
  
    Serial.print("Motion detected!");
    Serial.print("\n\n");
    digitalWrite(RELAY_1, LOW);
    delay(2000);
    
  } else {
    
    Serial.print("No motion detected.");
    Serial.print("\n\n");
    
  }

  delay(400);

}

Take a look.:
Timing starts when sensor in not trigged

int motionSensor = 2;
int timer = 100; // set the global delay
#define RELAY_1  3                        
#define RELAY_2  4                        
#define RELAY_3  5                        
#define RELAY_4  6

boolean r1_on; // relay1 on
long timeOff; // timestamp when r1 is to be turned off
const int secs_on=30; // seconds with light on
void setup() 
{
  Serial.begin(9600);
  pinMode(motionSensor, INPUT);
  pinMode(RELAY_1, OUTPUT);       
  pinMode(RELAY_2, OUTPUT);
  pinMode(RELAY_3, OUTPUT);
  pinMode(RELAY_4, OUTPUT);
  digitalWrite(RELAY_1,HIGH);
  digitalWrite(RELAY_2,HIGH);
  digitalWrite(RELAY_3,HIGH);
  digitalWrite(RELAY_4,HIGH);
}

void loop() 
{ 
  int motionSensorVal = digitalRead(motionSensor);
  if (motionSensorVal == HIGH) 
  {
    Serial.print("Motion detected!");
    Serial.print("\n\n");
    digitalWrite(RELAY_1, LOW);
    r1_on=true; 
    timeOff=millis()+1000*secs_on;
  } else 
  {
    Serial.print("No motion detected.");
    Serial.print("\n\n");
  }
  if (r1_on & millis()>=timeOff)
  { 
    digitalWrite(RELAY_1,HIGH);
    r1_on=false;
  }
}

Thank you for your reply.

The timer is not working. When I Serial.print time0ff the value changes for some reason? Wouldn't that be constant?

After the loop finishes the relay shuts off.

EDIT: It started working suddenly. Nevermind ;).

One thing I noticed is when you set the secs_on number to a value a little higher than 30, the timeOff value becomes negative and the timer does not work.

I tried using absolute value to make it positive but that did not work. It does not appear to be calculating it correctly. Any suggestions?