Led Still Turned on When Going Away from the Motion Sensor

i am working on a motion sensor with Blynk notification, when someone is near the sensor a led turns on and it sends me a notification to the Blynk app once every time someone is near the sensor,.
i made the code below:

#include <SPI.h>;
#include <SimpleTimer.h>;
#define BLYNK_PRINT Serial    
#include <BlynkSimpleEsp8266.h>
char auth[] = "****************************************************";
char ssid[] = "**********************";
char pass[] = "*******************";
SimpleTimer timer;
int lastState2 = 0;
int lastStateLow2 = 0;
#define PIRLedPin D8
#define pirPin D1 // Input for HC-S501
int pirValue; // Place to store read PIR Value

void setup()
{
  Serial.begin(115200);
  delay(10);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, getPirValue);
  pinMode(PIRLedPin, OUTPUT);
  pinMode(pirPin, INPUT);
}

void loop()
{
  timer.run();
  Blynk.run();
}

/***************************************************
 * Get PIR data
 **************************************************/
void getPirValue(void)
{
  pirValue = digitalRead(pirPin);
  if (pirValue == 1 && lastState2 == 0) 
  { 
    lastState2 = 1;
    lastStateLow2 = 0;
    Serial.println("==> Motion detected");
    Blynk.notify("T==> Motion detected");  
    digitalWrite(PIRLedPin, pirValue);
  }
 else if (pirValue == 0 && lastState2 == 0) {
  lastState2 = 0;
    lastStateLow2 = 1;
    digitalWrite(PIRLedPin, pirValue); 
 }
}

i made this, i received the notification, and the led turned on, but the led is still on even if i am not near it.
the thing is that i need the led to be turned on and a notification is sent only one time whenever someone comes near the motion sensor, else the led will be turned off.
Need help with this, thanks!

else if (pirValue == 0 && lastState2 == 0)

This may be the problem.

It sohould probably be like this

else if (pirValue == 0 && lastState2 == 1)
1 Like

ohh it worked, but how, with other sensors, it worked for me with laststate = 0 in the else if

for example this worked just fine:

void gassensor(){
gas_avalue = analogRead(A0);
Blynk.virtualWrite(A0, gas_avalue);
if (gas_avalue >350 && lastState == 0)            
  {
    lastState = 1;                         
    lastStateLow = 0;                  
    Serial.println(gas_avalue);
    Serial.println("DANGER!!!!");    
    Blynk.notify("DANGER!!!,Gas Leakage");
    digitalWrite(GasLedPin, HIGH);  
  }
  else if (gas_avalue <348 && lastState == 0) 
  {
    lastState = 0;
    lastStateLow = 1;
    Serial.println(gas_avalue);
    Serial.println("NO LEAKAGE");
    digitalWrite(GasLedPin, LOW);  
  }  
}

ohhh i was mistaken, it must be like that:

void gassensor(){
gas_avalue = analogRead(A0);
Blynk.virtualWrite(A0, gas_avalue);
if (gas_avalue >350 && lastState == 0)             
  {
    lastState = 1;                        
    lastStateLow = 0;                  
    Serial.println(gas_avalue);
    Serial.println("DANGER!!!!");    
    Blynk.notify("DANGER!!!,Gas Leakage");
    digitalWrite(GasLedPin, HIGH);  
  }
  else if (gas_avalue <348 && lastStateLow == 0) 
  {
    lastState = 0;
    lastStateLow = 1;
    Serial.println(gas_avalue);
    Serial.println("NO LEAKAGE");
    digitalWrite(GasLedPin, LOW);  
  }  
}

although it worked before, but this is the best way

You don’t need two “flags”. Just work through the logic.

If: gas_avalve is greater than 350 AND lastState is equal to 0
THEN: set lastState to 1, print the gas Sensor Value to the serial monitor, print DANGER!!! to the serial monitor, send BLYNK notification DANGER!!!,Gas Leakage, and make GasLedPin HIGH.

Else If: gas_avalue is less than 348 AND lastState is equal to 1
THEN: set lastState to 0, print Sensor Value to serial monitor, print NO LEAKAGE to serial monitor, and make GasLedPin LOW.


void gassensor(){
gas_avalue = analogRead(A0);
Blynk.virtualWrite(A0, gas_avalue);
if (gas_avalue >350 && lastState == 0)             
  {
    lastState = 1;                                     
    Serial.println(gas_avalue);
    Serial.println("DANGER!!!!");    
    Blynk.notify("DANGER!!!,Gas Leakage");
    digitalWrite(GasLedPin, HIGH);  
  }
  else if (gas_avalue <348 && lastState == 1) 
  {
    lastState = 0;
    Serial.println(gas_avalue);
    Serial.println("NO LEAKAGE");
    digitalWrite(GasLedPin, LOW);  
  }  
}
1 Like

yeahh, that’s also nice, with less variables and same result, thanks @Toro_Blanco :blush: