Wemos with motion sensor keep LEDs on for duration

I am hooking up a Wemos d1 mini with a RCWL-0516 microwave sensor and an LED strip. If motion is detected I want the LEDs to light up and stay on for 15 minutes and then turn off if no motion has been detected. I've set the sensor to check motion every 2000ms. Is there something better I can do here? Thx in advance.

const unsigned long radar_check = 2000;
const unsigned long LEDS_on = 900000;
unsigned long previousTime = 0;

#define Motion_Sensor D6
#define LED_pin D4
int radarVal = 0;

void setup() {
pinMode(Motion_Sensor, INPUT);
pinMode(LED_pin, INPUT);
Serial.begin(115200);
Serial.println("RCWL-0516 motion test");
}

void loop() {

  /* Updates frequently */
  unsigned long currentTime = millis();

  /* This is the event */
  if (currentTime - previousTime >= radar_check) {
    
    /* Event code */
    radarVal = digitalRead(Motion_Sensor);
    if (radarVal == HIGH) {
      Serial.println("Someone is here, keep/switch on leds");
      if (currentTime - previousTime >= LEDS_on){
      digitalWrite(LED_pin, LOW); // set the LEDs off
      }
      }
      else {
        Serial.println("Nobody is here, turn off leds");
      digitalWrite(LED_pin, LOW); // set the LEDs off
        }
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  }
}

And, tell us if it works or not. If not, what part does work and what part does not work? What do you see from the Serial.print() ?

The LEDs just stay on throughout. As for the serial monitor, it’s displaying correctly, so the sensor is working properly. I want the LEDs to go off when no motion is detected.

If you are NOT seeing this, then your "if" logic is NOT correct. Something to look at!

I am seeing this, that’s working nicely. The issue is that the LEDs aren’t turning off, so it’s something to do with digitalWrite(LED_pin, LOW); . I was hoping for a solution to my incorrect coding here, but if you can’t figure this out either then hey ho. I appreciate you taking time out, so thanks anyway.

Autoformat your code using the tool in the IDE.

Then put your finger on it and play dumb computer.

I’m at the beach so I can’t, but it looks like you have a flaw in the basic structure which is hard to spot.

The idea looks like it is in there, I think rearranging where the timer is used and reset is wrong yet.

a7

The you likely have the LEDs wired backward! A schematic will show that right away.

Thanks very much for your feedback, I’ll give that a go.

Thanks Paul, I’ll look into that.

The strip is pointing the right way, and double checked the wiring and that all looks correct, I.e. 5V and Data one end to GND at the other end. I just can’t figure out why the LED doesn’t go LOW when radarVal doesn’t equal HIGH. I’ve even changed the if statement to:

if (radarVal == LOW) {

```  but doesn’t make any difference.

You never write LED_pin HIGH.

Oh, okay. What is the correct way?

I did not mean that to be code. I thought you would know what I meant.

digitalWrite( LED_pin, HIGH )

I don't know how your LED is wired so I don't know where it belongs (the "if" or the "else")

void loop() 
{
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= radar_check) 
  {
    radarVal = digitalRead(Motion_Sensor);
    if (radarVal == HIGH) 
    {
      Serial.println("Someone is here, keep/switch on leds");
      if (currentTime - previousTime >= LEDS_on)
      {
        digitalWrite(LED_pin, HIGH); // set the LEDs off
      }
    }
    else 
    {
      Serial.println("Nobody is here, turn off leds");
      digitalWrite(LED_pin, LOW); // set the LEDs off
    }
    previousTime = currentTime;
  }
}

If you don't like that then try this --

void loop() 
{
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= radar_check) 
  {
    radarVal = digitalRead(Motion_Sensor);
    if (radarVal == HIGH) 
    {
      Serial.println("Someone is here, keep/switch on leds");
      if (currentTime - previousTime >= LEDS_on)
      {
        digitalWrite(LED_pin, LOW); // set the LEDs off
      }
    }
    else 
    {
      Serial.println("Nobody is here, turn off leds");
      digitalWrite(LED_pin, HIGH); // set the LEDs off
    }
    previousTime = currentTime;
  }
}

No worries, thank you for responding in the first instance, truly appreciate it! Okay, I have it working now at least, just dropped in a pull-down 10k resistor, and also had the LED_pin set to INPUT. But I'm still doing something wrong because it's doing the opposite of what I'm expecting it to do. The led is coming on when it's meant to be going off and visa-versa. Here's my code:

/* Radar */
    radarVal = digitalRead(Motion_Sensor);
    if (radarVal == HIGH) {
      Serial.println("Someone is here, keep/switch on leds");
      digitalWrite(LED_pin, HIGH);
      }
      else if (radarVal == LOW) {
        Serial.println("Nobody is here, turn off leds");
        digitalWrite(LED_pin, LOW);
        }

  /* Update the timing for the next time around */
    previousTime = currentTime;
  }

Ah thanks so much, I'll give this a go too :slight_smile:

Strip… is not an LED.

Post a schematic diagram.

Strip, what kinda strip?

a7

Thanks for these variations. The second one works for me but I don't understand why LOW and HIGH work in reverse? If I want the lights off then surely I put it LOW, but in the second example above, LOW turns them on and HIGH turns them off.

How about it, @jaseography ?