Slow LED Fade on Motion Detection

After several searches and frustrating results, I came across some pretty simple & brilliant code by a user here that hasn't posted in 7 years, so I'm hoping some of you might provide some assistance. The design is to detect motion, fade LED(s) up, hold while motion is active, then fade LED(s) down with no motion. His code worked great, but I'd like to slow the fade and I'm not comprehending how to do that within his code. The coder was philicibine and I want to ensure he gets the credit here.

Any help in explaining what I'm missing for how to fade at lower increments is appreciated.

#define LEDPIN 0
#define PIRPIN 1

int ledState = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
boolean alreadyfadedup  = false;
boolean alreadyfadeddown  = false;
const long interval = 5000;  //amount of time the leds will stay up in millis

void setup() {
  pinMode(PIRPIN, INPUT);
}


void loop() {

  currentMillis = millis();

  if (digitalRead(PIRPIN) == 1) {
    ledState = 1;
    previousMillis = currentMillis;
    if (alreadyfadedup == false) {
      for (int i = 0; i < 256; i++) {
        analogWrite(LEDPIN, i);
        delay(2);
      }
      alreadyfadedup  = true;
      alreadyfadeddown = false;
    }
  }
  if (currentMillis - previousMillis >= interval) {
    if (ledState == 1) {
      if (alreadyfadeddown == false) {
        for (int i = 255; i >= 0; i--) {
          analogWrite(LEDPIN, i);
          delay(2);
        }
        alreadyfadeddown  = true;
        alreadyfadedup = false;
      }
      ledState = 0;
    }
  }
}

Start with giving the username a call: @philicibine

Then, format your code in your favorite IDE.

Then, paste the formatted code into your original post (click < CODE > button and paste)

#define LEDPIN 0
#define PIRPIN 1

int ledState = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
boolean alreadyfadedup = false;
boolean alreadyfadeddown = false;
const long interval = 5000; //amount of time the leds will stay up in millis

void setup() {
  pinMode(PIRPIN, INPUT);
}

void loop() {

  currentMillis = millis();

  if (digitalRead(PIRPIN) == 1) {
    ledState = 1;
    previousMillis = currentMillis;
    if (alreadyfadedup == false) {
      for (int i = 0; i < 256; i++) {
        analogWrite(LEDPIN, i);
        delay(2);
      }
      alreadyfadedup = true;
      alreadyfadeddown = false;
    }
  }
  if (currentMillis - previousMillis >= interval) {
    if (ledState == 1) {
      if (alreadyfadeddown == false) {
        for (int i = 255; i >= 0; i--) {
          analogWrite(LEDPIN, i);
          delay(2);
        }
        alreadyfadeddown = true;
        alreadyfadedup = false;
      }
      ledState = 0;
    }
  }
}

First things I noticed are the pins being used, 0 and 1... maybe it will work, but generally, pins 0 and 1 on an Uno is not good. Maybe the board was an ESP?

The code seems to latch the LED on for five seconds.

To "slow the fade" you would increase this delay value... This increases the time to "forever"...

This keep the LED on longer... 5000 = 5 seconds, 15000 = 15 seconds... et c.

Thanks for your attempt to use code tags; it did not quite work out and I have fixed them.

``` (each on their own line) before and after the code is sufficient.

If you prefer to type it out, it's [code] and [/code], not <code> and <code/>.

1 Like

Thanks. I did change those pins to 2 for the motion sensor and I used 9 for the LEDs to use the PWM.

I’m satisfied with the 5000ms delay (keeping the LED on before fading out), but I’m wanting to alter the LED intensity slower, making it fade in and out slower.

I expected to see it at the end of this code line but I’m not sure how to make “i—“ or “i++” into something I can alter to change that fade in/out speed.

 for (int i = 0; i < 256; i++)
 for (int i = 255; i >= 0; i--)

Maybe if you (++) and (--) at a decimal step, rather than full integer steps:

i++

try

i += .1

Because you can't PWM in decimal, this will make each full integer step take ten loops to get from "i" to "i+1"

Ah, I see. It’s the delay. I changed it to 15 and it’s close enough to what I wanted. Thanks for the help!

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