Help with timing for a motion-controlled nightlight


I’m having trouble with a nightlight that I’ve built.

This is how it currently works:

A light sensor monitors the amount of light in the room.
A potentiometer is used to set a threshold for the light sensor.
A motion sensor checks for any motion in the room.

If the sensor detects motion in the room, and if the amount of light in the room is less than the threshold set by the potentiometer, the nightlight turns on for 2 minutes, then turns off.

What I want it to do:

I want the light to turn on when it detects motion and it’s dark enough (like it already does), but the 2 minute countdown should only start when there is no longer any motion. If there is any motion while the 2 minute countdown is ongoing, the timer should be reset to 2 minutes.

Example: If I start moving, the light turns on and stays on. If I stop moving 20 seconds later, that’s when the 2 minute countdown starts. If i start moving again halfway through this countdown, the timer resets back to 2 minutes until I stop moving.

I’m not sure how to go about programming this. Below is my code for how it currently works.
Any help would be appreciated.

int lightSensor = 0;
int potMeter = 1;
int motionSensor = 2;
int red = 11;
int grn = 10;
int blu = 9;
int calibrationTime = 30;  //seconds to calibrate (set to 30 unless testing)
int timeON = 120;          //how many seconds the light stays on after detecting motion

int lightSensorSetting = 0;
int lightSensorValue = 0;

void setup()
 pinMode (red, OUTPUT);
 pinMode (grn, OUTPUT);
 pinMode (blu, OUTPUT);
 pinMode (motionSensor, INPUT);
 digitalWrite (motionSensor, LOW);
//sensor calibration
    for(int i = 0; i < calibrationTime; i++)
    Serial.println("SENSOR ACTIVE");

void loop()

  lightSensorValue = analogRead(lightSensor);
  lightSensorSetting = analogRead(potMeter);

  Serial.print("brightness setting: ");
  Serial.print("amount of light: ");
  if ((digitalRead(motionSensor)==HIGH) && (lightSensorValue < lightSensorSetting))
    analogWrite (red, 7);
    analogWrite (grn, 5);
    analogWrite (blu, 3);
    //Serial.println("Motion Detected");
    //Serial.println("Light ON for ");
    for(int j = 0; j < timeON; j++)
    int countdown = timeON - j;
    analogWrite (red, 0);
    analogWrite (grn, 0);
    analogWrite (blu, 0);
    //Serial.println("Light OFF");
unsigned long timer = 0;
bool turnOn = false;

if (triggerCondition){
timer = millis();
turnOn = true;
else if (millis() - timer > 120000) {
turnOn = false;

if (turnOn){

A crude hack, fitting a crude program.

    for(int j = 0; j < timeON; j++)
      if ((digitalRead(motionSensor)==HIGH) {
        j = 0;