If condition does not work, I don't know what's wrong.

Hi everyone,

So below is my coding. I’m using NodeMCU ESP826. As you can see I’ve set the threshold to 100. And I also make an if condition to the pir sensor. if the light value is lower than the threshold, then the pir sense motion and led would turn on. But, even when the light value is higher than the threshold the pir still sense the motion. There is no error while compiling. Any idea on why that happens?

#include <math.h>
#include <Wire.h>
#include <BH1750FVI.h>

#define timeSeconds 10
#define threshold 100

static uint16_t LightValue;
//int LightValue;
int CHECK_INTERVAL = 10000;

// Set GPIOs for LED and PIR Motion Sensor
const int led = 0;
const int motionSensor = 14;
const int light = 4;

// Settings for BH1750
uint8_t ADDRESSPIN = 13;
BH1750FVI::eDeviceAddress_t DEVICEADDRESS = BH1750FVI::k_DevAddress_H;
BH1750FVI::eDeviceMode_t DEVICEMODE = BH1750FVI::k_DevModeContHighRes;

// Create the Lightsensor instance
BH1750FVI LightSensor(ADDRESSPIN, DEVICEADDRESS, DEVICEMODE);

// PIR Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

// Checks if motion was detected, sets LED HIGH and starts a timer
void ICACHE_FLASH_ATTR detectsMovement() {
Serial.println(“MOTION DETECTED!!!”);
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}

void setupPIR (){

if (LightValue < threshold )
{
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
}

void setupLightSensor(){
pinMode(light, INPUT);
LightSensor.begin();
Serial.println(“Running…”);
}

void setup() {
// Serial port for debugging purposes
Serial.begin(115200);

setupLightSensor();

setupPIR();

}

void loop() {

// Current time
// now = millis();

// Current time
now = millis();
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000)))
Serial.println(“Motion stopped…”);
digitalWrite(led, LOW);
startTimer = false;

//Get light intensity value
LightValue = LightSensor.GetLightIntensity();
Serial.print("Light: ");
Serial.println(LightValue);
//delay(1000);

delay(1000);
}

You do know that pins 0 and 1 are used by Serial?

  if(startTimer && (now - lastTrigger > (timeSeconds*1000))) 
    Serial.println("Motion stopped...");
    digitalWrite(led, LOW);
    startTimer = false;

seems to be missing a pair of { } IF you want that if () to only do the next few lines when true.

GoForSmoke:
You do know that pins 0 and 1 are used by Serial?

I used GPIO which is D1. Should i not use that?

  if(startTimer && (now - lastTrigger > (timeSeconds*1000))) 

Serial.println("Motion stopped...");
   digitalWrite(led, LOW);
   startTimer = false;




seems to be missing a pair of { } IF you want that if () to only do the next few lines when true.

thank you for this.
I was actually talking about the IF condition in void setupPIR

LOL…

You have this that runs in setup() before loop() runs,

void setupPIR (){

  if (LightValue < threshold )
  {

and the only place I see LightValue set to other than default 0 is in void loop().

  LightValue = LightSensor.GetLightIntensity();

Just me saying this, I would always have PIR set up and only read PIR when the light level is low. That way if the light starts bright but drops, the PIR will be -able- to work.