3 PIR sensor Code

Hi All,

This is my secon post on this endvor and I'm close, so.
This is what I'm doing-- I work in a museum and am fabricating a base for a sculpture that has an electrical component. I want it to turn on when one of the 3 sensors is triggered, stay on for 30 seconds and stay off for 2 minutes before any of the sensors will be able to activate again. The code I came up with, which is just and augmented version of a single sensor code I found, doesn't seem to be consistant. The delay's don't seem to be consistance everytime the sensors are triggered. Any ideas

int irmotionPin1 = 10 ;
int irmotionPin2 = 9; 
int irmotionPin3 = 8;// Pin of IR Motion Sensor 
int relayPin = 7;  // Pin of Relay Module

 
void setup(){
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);  // Set Pin connected to Relay as an OUTPUT
  digitalWrite(relayPin, LOW);  // Set Pin to LOW to turn Relay OFF }
}
void loop(){
  
  while (digitalRead(irmotionPin1) == HIGH  || (digitalRead(irmotionPin2) == HIGH) || (digitalRead(irmotionPin3) == HIGH)) {  // If Motion detected 
       digitalWrite(relayPin, HIGH);  // Turn Relay ON
       Serial.println("Relay is ON");
       delay (30000) ;
       
}
 
       digitalWrite(relayPin, LOW);  // Turn Relay OFF
       Serial.println("Relay is OFF");
     delay (120000);
}

I just ran this code for about 10 minutes. Each time the HIGH and LOW signals had different delays and now it wont run the code at all????

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

JCaps:

  while (digitalRead(irmotionPin1) == HIGH  || (digitalRead(irmotionPin2) == HIGH) || (digitalRead(irmotionPin3) == HIGH)) {

while will continue looping through that bracket as long as one of the PIR sensors is activated. That's not the behavior you want as it can lead to the relay being on for longer than 30 seconds. Use if instead of while.

JCaps:
now it wont run the code at all????

Please explain exactly what you mean by that.

I had this set up in my workspace and left it running to see if the delay issue would get worse or better. I re-entered the room and non of the sensors were responding. It was as if the "code" or more correctly the programing stopped working. Unless the delay was way off, at one point the delay was more than twice as long as it should have been.
here's the auto format

int irmotionPin1 = 10 ;
int irmotionPin2 = 9;
int irmotionPin3 = 8;// Pin of IR Motion Sensor
int relayPin = 7;  // Pin of Relay Module


void setup() {
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);  // Set Pin connected to Relay as an OUTPUT
  digitalWrite(relayPin, LOW);  // Set Pin to LOW to turn Relay OFF }
}
void loop() {

  while (digitalRead(irmotionPin1) == HIGH  || (digitalRead(irmotionPin2) == HIGH) || (digitalRead(irmotionPin3) == HIGH)) {  // If Motion detected
    digitalWrite(relayPin, HIGH);  // Turn Relay ON
    Serial.println("Relay is ON");
    delay (30000) ;

  }

  digitalWrite(relayPin, LOW);  // Turn Relay OFF
  Serial.println("Relay is OFF");
  delay (120000);

JCaps:
I had this set up in my workspace and left it running to see if the delay issue would get worse or better. I re-entered the room and non of the sensors were responding. It was as if the "code" or more correctly the programing stopped working.

If you unplug it and plug it back in does it start working again?

Did you try changing the while to if yet?

You missed the last line of your code.

Some PIRs have an active low output. Yours? - Scotty

Give this a try:

unsigned long timeStart;
const unsigned long timeOn = 30000, lockoutTime = 120000;
const byte relayPin = 7,
           irmotionPin1 = 10,
           irmotionPin2 = 9,
           irmotionPin3 = 8;
bool lockout = false;

void setup() {
  pinMode(irmotionPin1,INPUT);
  pinMode(irmotionPin2,INPUT);
  pinMode(irmotionPin3,INPUT);
  pinMode(relayPin,OUTPUT);
}

void loop() {
  if((digitalRead(irmotionPin1) || digitalRead(irmotionPin2)
     || digitalRead(irmotionPin3)) && (!lockout))
  {
    timeStart = millis();
    digitalWrite(relayPin,HIGH);
    lockout = true;
  }  
  if(millis() - timeStart > timeOn)
    digitalWrite(relayPin, LOW);
        
  if(millis() - timeStart > timeOn + lockoutTime)
    lockout = false;
}

I tried switching the "which" to "if", no luck.

I loaded up the code provided by 765E6C, it seems to be working perfectly, thanks for the input.

Thank you everyone for your assistance! I ran the program all day and it is exactly what I was trying to achieve. Thanks again for the code 765E6C. Cheers!