Hello, I am working on a project where a PIR motion sensor triggers an 8 channel relay. The relays turn on at different times. I could not find a code online for this so I took it upon myself to attempt to write it myself. However, when I set it up, the relays go off by themselves even with no motion. it seems that they are in a loop. Could someone please review the code and see if there is anything wrong with it.
Thank You!!!
Code-
int irmotionPin = 4;
int relayPin1 = 5;
int relayPin2 = 6;
int relayPin3 = 7;
int relayPin4 = 8;
int D = 2000;
void setup(){
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode (relayPin2, OUTPUT);
pinMode (relayPin3, OUTPUT);
pinMode (relayPin4, OUTPUT);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
}
void loop(){
while (digitalRead(irmotionPin) == HIGH) {
digitalWrite(relayPin1, HIGH);
delay(D);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
Serial.println("Triggered");
delay(5000);
}
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
delay(4000);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin4, LOW);
Serial.println("Waiting");
delay(1);
}