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;
}
}
};
