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!
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.