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);
}
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.
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?
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!