Fixing Motion Sensored Lights Program

Hey guys I ran into a problem where my motion sensing light’s pir sensor goes low even when I am moving (and/or within sensor’s range). The shop where I bought my sensor said that the sonsor sometimes does that even when there is motion. My intention for the project is for the lights to stay on whenever there is a person in range (cos ima use it for a public comfort room) and u know turn off only when people are out of range. I’m grateful that u stayed this long reading a random dude’s problem, I really need help on how to achieve my desired outcome thank u guys!^-^

Here is my program:

int sensor_pin = 8; // Initialized the pin for motion sensor
int relay_pin = 9; // Initialized the pin for relay module
int output; // Variable to store the output state of motion sensor
void setup(){
Serial.begin(9600);
pinMode(sensor_pin, INPUT); // Declared motion sensor pin as Input
pinMode(relay_pin, OUTPUT); // Declared relay module pin as Output pin
// Make the relay module initial state as low, Relay works opposite
digitalWrite(relay_pin, HIGH);
}
void loop(){
// Read the output state of motion sensor
output = digitalRead(sensor_pin);

// If output is High
if(output == 1){
digitalWrite(relay_pin, LOW);
}

// If output is LOW
else if(output == 0){
digitalWrite(relay_pin, HIGH);
}
Serial.println(output);
delay(100);
}

Is there anyway to tweak this so it stays on when im in range of sensor and it only turns off when nothin is present? THANK U VERY MUCH >.<

Whenever the sensor detects movement, capture the time using millis. Only turn the light off when you have not sensed movement for some time - a minute perhaps?.

How do i put that in the code? I have read some articles about using millis but i’m not really knowledgeable about it. Do i put the millis down my if condition then make it a minute then put it in voidsetup part?

A PIR is not a presence detector. It only sees movement. If you set your time to 5 minutes then sit still for 5 minutes the light will still go off. Is that acceptable for your project?

Additionally, many PIR sensors have their own delay built-in because this is what they are mostly used for. But they do it in a stupid way: they don't look for movement while the light is on. Instead the light turns off after the programmed period and might turn on again immediately if you are currently moving.

void loop(){
  const unsigned long lightOnPeriod = 1000; //milliseconds. Light will stay on for this time after the last movement is detected
  static unsigned long lastMovement;

  // Read the output state of motion sensor
  output = digitalRead(sensor_pin);
  
  // If output is High it means it detected something
  if(output == 1){
    digitalWrite(relay_pin, LOW);
    lastMovement = millis();
  }
  
  // If output is LOW and the time expired
  else if(output == 0 && millis()-lastMovement>lightOnPeriod){
    digitalWrite(relay_pin, HIGH);
  }
  Serial.println(output);
  delay(100);
}

This code does have one small bug: if there is no movement for 49 days, the light will go on for a minute.