Keep the LED on as long as the IR sensor detects something

Hello friends,
I'm asking this to help a friend of mine who does not have internet in his area,
He has an IR sensor that turns an LED on for 5 seconds when something is detected in front of the sensor.

Is there a way to only turn off the LED if there is nothing in front of the sensor? More precisely, to keep the LED on as long as there is something in front of the sensor even after the 5 seconds elapsed. Because now after the 5 seconds elapsed, the LED turns off and quickly back on.
I tried to ask chat GPT but it could not solve this.

Thank you so much!

//==============================================================
// SETTINGS | CHANGE AS PER YOUR REQUIREMENT
//==============================================================

  int Fade_In_Time = 2;  // Default fade-in duration in milliseconds
  int Fade_Out_Time = 40; // Default fade-out duration in milliseconds
  int LED_On_Time = 5000; // How long the LED should stay on in milliseconds
  int LED_Brightness = 255; // Adjust from 0 (LED off) to 255 (maximum brightness)

//==============================================================
 void fadeIn() {
    fadingInProgress = true;
    for (int i = 0; i <= LED_Brightness; i++) {
      analogWrite(ledPin, i);
      delay(Fade_In_Time);
    }
    fadingInProgress = false;
  }

  void fadeOut() {
    fadingInProgress = true;
    for (int i = LED_Brightness; i >= 0; i--) {
      analogWrite(ledPin, i);
      delay(Fade_Out_Time);
    }
    fadingInProgress = false;
  }

  void setFadeDurations(int Fade_In_Time, int Fade_Out_Time) {
    this->Fade_In_Time = Fade_In_Time;
    this->Fade_Out_Time = Fade_Out_Time;
  }

  void setIrDetected(bool detected) {
    if (userControlled || fadingInProgress) return;  // don't start a new fade if one is already in progress

    irDetected = detected;
    if (detected) {
      timerStart = millis();
      fadeIn();
      power->setVal(true);
      update();
    }
  }

  boolean update() {
    if (power->getNewVal() != power->getVal()) {
      userControlled = power->getNewVal();
      irDetected = false;
      power->setVal(power->getNewVal());

      // If the LED is being turned on
      if (power->getVal() && !fadingInProgress) {
        fadeIn();
      }
      // If the LED is being turned off
      else if (!power->getVal() && !fadingInProgress) {
        fadeOut();
      }
    }
    return true;
  }

  void checkTimeout() {
    if (irDetected && (millis() - timerStart > LED_On_Time) && !fadingInProgress) {
      irDetected = false;
      fadeOut();
      power->setVal(false);
      update();
    }
    if (!power->getVal() && !fadingInProgress) {
      userControlled = false;
    }
  }
};

the IR sensor is sensitive to "heat movement" and comes usually with 2 potentiometers, one that sets the duration of the TRIGGERED state and one that is used to set the "sensitivity" of the sensor.

When the sensor detects heat movement, it goes to the TRIGGERED state and stays there for the duration you set with the potentiometer, even if the detection is no longer true.

After the set time, the trigger is released and it will trigger again if it detects again some heat movement.

So the duration of the trigger is not linked to the actual presence of the heat movement. Upon detection it just goes on for some time and then off and does it again.


To your case, if you want the led to stay on even when the sensor goes off at the end of the Trigger duration, you need to code for that. The code coud wait "a bit" after the trigger is canceled to turn off the LED, this way if there is a new trigger the LED will not have blinked.

a state machine is well suited to that kind of coding, it would look like this

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.