Working on how to run a if statement inside the loop for the following...
If PIR triggers 3 times in a window of 50secs sends a alert then resets the countPIR to zero and starts again.
If PIR triggers 2 times in a window of 50secs no alert resets the countPIR to zero and starts again.
If PIR triggers 1 times in a window of 50secs no alert resets the countPIR to zero and starts again.
The point is too minimize any false PIR triggers with in a period (50sec) 3 triggers to send a alert or action less than three reset and start again.
//Timer new
const unsigned long eventInterval = 50000; //20 seconds
unsigned long previousTime = 0;
unsigned long currentTime = millis();
unsigned long countDown = 0;
// Countdown Timer
(countDown = (currentTime - previousTime) / 1000.0, 0); //Divide millis to sec / or % , 1 ,2
// PIR Detector
pirState = digitalRead(pirPin);
if (pirState != lastPirState) { // if changed
if (pirState) { // if LOW to HIGH
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin, HIGH);
Serial.print("State: ");
Serial.println("Motion Detected!");
countPIR++; // increment x by one and returns the old value of x
Serial.print("Counter: ");
Serial.println(countPIR);
if (countPIR >= 3) {
countPIR = 0; //reset PIR count to 0 // x is greater than or equal to y
Serial.print("State: ");
Serial.println("Counter reset!");
countDown = 0; //reset countdown timer to 0
Serial.print("Timer: ");
Serial.println("Timer reset from PIR!");
}
}
else { // if HIGH to LOW
digitalWrite(ledPin, LOW);
Serial.print("State: ");
Serial.println("No Motion");
}
// Reset time window
if (currentTime - previousTime >= eventInterval) {
previousTime = currentTime;
countPIR = 0; //reset PIR count to 0
Serial.print("Timer: ");
Serial.println("Timeout Reset from 50s!");
}
lastPirState = pirState; // remember
}
if (countDown >= 50) {
countDown = 0; //reset countDown to 0 // x is greater than or equal to y
}
else { // Display counter
Serial.print("Timer: ");
Serial.println(countDown);
delay(1000);
}
}
Timer: 25
Timer: 26
Timer: 27
Timer: 28
Timer: 29
Timer: 30
Timer: 31
Timer: 32
Timer: 33
Timer: 34
Timer: 35
Timer: 36
Timer: 38
State: Motion Detected!
Counter: 2
Timer: 39
Timer: 40
State: No Motion
Timer: 41
Timer: 42
Timer: 43
Timer: 44
State: Motion Detected!
Counter: 3
State: Counter reset!
Timer: Timer reset from PIR!
Timer: 0<--------- resets it but is due to it being outside the if?
Timer: 46<------ still counts to 50secs?
State: No Motion
Timer: 47
Timer: 48
Timer: 49
State: Motion Detected!
Counter: 1
Timer: Timeout Reset from 50s!
Timer: 0 <--------- resets it but is due to it being outside the if?
Timer: 1
Timer: 2
State: No Motion
Timer: 3
Timer: 4
Timer: 5
countPIR resets to countPIR 0 but doesnt reset the countDown to 0
At this stage after 50 secs countDown resets to 0
This is a reasonably complex scenario, as the start of the time window (50 seconds) needs to continually update as old PIR triggers drop off.
Before coming up with a solution can you confirm the behaviour of your sensor. Some sensors can be configured to "pulse" a signal at the first sign of movement, whereas some will stay triggered while there is still movement.
Thanks John will try and work my way through I issue for me is taken the original code below and working out how ti implement the your example into it which will take me some time.
// PIR Detector
pirState = digitalRead(pirPin);
if (pirState != lastPirState) { // if changed
if (pirState) { // if LOW to HIGH
digitalWrite(ledPin, HIGH);
Serial.println("Motion Detected!");
}
else { // if HIGH to LOW
digitalWrite(ledPin, LOW);
Serial.println("No Motion");
}
lastPirState = pirState; // remember
}
}
void Check_PIR()
{
// PIR Detector
int pirState = digitalRead(pirPin);
if (pirState != lastPirState) // if changed
{
lastPirState = pirState;
if (pirState) // if LOW to HIGH
{
PIR_Triggered();
}
}
}
Thanks John seems to be working. Only question if there is only two triggers with 50 sec period does it reset the count to O once the time has expired?
Is there a way to show the time 1,2,3-50 as it goes and then see it reset to 0? This would allow me to continue the build the code.
//Timer new
int timeInterval = 50000; //20 seconds
if (TriggerCount > 0 &&
millis() - TriggerTimes[0] >= (timeInterval)) //5000
{
for (unsigned tt = 1; tt < TriggerCount; tt++)
{
TriggerTimes[tt-1] = TriggerTimes[tt];
}
TriggerCount--;
}
// PIR Detector
Check_PIR();
}
void PIR_Triggered()
{
TriggerTimes[TriggerCount++] = millis();
if (TriggerCount >= 3)
{
// SOUND THE ALARM HERE
Serial.print("Status: ");
Serial.println("Reset Trigger Count to 0");
TriggerCount = 0;
Serial.print("Status: ");
Serial.println("Alarm 3 Movements detected");
Serial.print("Status: ");
Serial.println(TriggerCount);
digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
delay (500);
}
}
void Check_PIR()
{
// PIR Detector
int pirState = digitalRead(pirPin);
if (pirState != lastPirState) // if changed
{
lastPirState = pirState;
if (pirState) // if LOW to HIGH
digitalWrite(ledPin, HIGH);
delay (100);
digitalWrite(ledPin, LOW);
delay (100);
digitalWrite(ledPin, HIGH);
delay (100);
digitalWrite(ledPin, LOW);
delay (100);
digitalWrite(ledPin, HIGH);
delay (100);
digitalWrite(ledPin, LOW);
delay (100);
Serial.print("Status: ");
Serial.println("Motion Detected!");
Serial.print("TCount: ");
Serial.println(TriggerCount);
{
PIR_Triggered();
}
}
}