PIR controlled relay - timing issues

Hi Folks.

I'm sure that more questions will come up along the line but for now i could do with one answer in particular.

I'm making a controlled relay that when movement it detected the relay opens for a certain amount of time. The problem is if movement is always detected the relay stays open where as i need it to close after the aloted time no matter if there is motion or not.

Any suggestions?

My code -

const int pirPin = 2;     
const int relayPin =  13;      

int pirState = 0;         

void setup() {
  
  pinMode(relayPin, OUTPUT);      
  
  pinMode(pirPin, INPUT);     
}

void loop(){
  
  pirState = digitalRead(buttonPin);

  
  if (pirState == HIGH) {     
    digitalWrite(relayPin, LOW);
    delay(30000);
    digitalWrite(relayPin, HIGH);

  } 
  else {
    digitalWrite(relayPin, HIGH); 
  }
}

The way you coded it it will close after the allotted time....

But it will open again if there's still movement after the delay. Do you mean that in that case you don't want it to open again? I'd use a boolean flag, say "opened". Set opened to false on initialisation. Then change your if to test not just pirstate but also opened for true. Inside the if where you set the relay low, also set opened to true.

So next time round, after the relay opened once and closed, it wont open again since opened is true and the if won't fire.

EDIT.... but you might like to look at a "blink without delay" approach.

So the problem is that if the pir detects movement again within the 30000ms that the relay is told to be open it seems to reset the count to the last time that the movement was detected.

No.... NOTHING happens in the delay, except, well, the delay. It's not looking at any pins....

But as soon as the relay goes high at the end of the delay, loop() takes you back to the top and if the PIR still says there is still motion, well the relay will go low immediately. That might be so fast, that you wouldn't even know that the relay had tried to switch.

I see. it must be then a fast trigger after the delay time.

Thank you.

But you ought in any case to look at a BWD approach. Right now, that 30 second delay is preventing your processor from doing anything else.

EDIT … my previous suggestion will only allow the relay to cycle once, since once you’ve set opened to true, you’re screwed. If you went with my approach, you would need to reset opened somehow, say on time or a manual switch, else it’ll only work once. You really need a BWD and state machine approach here.

Thanks everyone.

If i use the BWD approach… would it be possible to then have more that one pir controling more that one relay that can be activated even if one or more relay is open?

Hope that makes sense.

FTL

fasterthanlight: If i use the BWD approach... would it be possible to then have more that one pir controling more that one relay that can be activated even if one or more relay is open?

NOW you're talking 8)

Have a look at this video and this thread and all will be revealed....