Increment a count only when PIR is active for 5seconds

Hi, Been fiddling around with this for a while and can’t get it to do what i want. Basically I want the “counter” to ignore any activity the PIR detects if its there for less than 5seconds any more and it will increment/decrement the count.
void loop()
{
countup = digitalRead(pir1); // read input value
countdown = digitalRead(pir2);

if (countup == HIGH && countdown == LOW)
{
if (pirState == LOW)
{
Serial.println(“Motion detected”);
buttonPushCounter–;
Serial.print("Number of spaces available: ");
Serial.println(buttonPushCounter);
pirState = HIGH;
matrix.print (buttonPushCounter);
matrix.writeDisplay();
//delay(10);
}
}

PIR_count_v3.ino (3.15 KB)

The variable pirState does not contain the state of any PIR. You REALLY need a better name.

The variables countup and countdown do not make sense to contain the state of any PIR sensors.

The PIR sensor does not know about time. The Arduino does. So, YOU need to record when you see a change (from no motion to motion or from motion to no motion). You need to determine, then, on any given pass through loop() whether there is (still) motion, and that the motion has been going on for some period of time.

The state change detection example and the blink without delay example bear studying.

I see that your PIR sensor goes LOW when activity is detected.
It basically behaves like a pushbutton.

That means you could use the built-in debounce function of Atm_button to discard any activity that lasts less than 5 seconds (5000 milliseconds)

#include <Automaton.h>
#include <Atm_button.h>

Atm_button button;
Factory factory;

const int pinPir = 8;

void btn_change( int press ) 
{
  if ( press ) {
    // Do what needs to be done
  }
}

void setup() 
{
  button.begin( pinPir ).onPress( btn_change );
  button.debounce( 5000 ); // Debounce 5000 milliseconds
  factory.add( button );    
}

void loop() 
{
  factory.cycle();
}

The Automaton framework can be found at: Home · tinkerspy/Automaton Wiki · GitHub

If you want to test that some situation exists continuously for a period of time this sort of code should do it

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

Just replace the button with your PIR output.

...R