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