I want to activate a 6 relays in pairs when the PIR detects movement. I also wanted to have the PIR to be in an off state until the relays re-activate. I can't seem to get the PIR to work properly. Sidenote, I connected the PIR to an external 12v psu.
CODE
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
#define Relay_6 7
/-----( Declare Variables )-----/
int waittime; // Delay between changes
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 15000;
int pirPin = 9; //the digital pin connected to the PIR sensor's output
void setup() /****** SETUP: RUNS ONCE ******/
{
waittime = 1000;
Serial.begin(9600);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
//-------( Initialize Pins so relays are active at reset)----
digitalWrite(Relay_1, RELAY_ON);
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
digitalWrite(Relay_5, RELAY_ON);
digitalWrite(Relay_6, RELAY_ON);
//---( THEN set pins as outputs )----
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
delay(4000); //Check that all relays are active at Reset
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if(digitalRead(pirPin) == HIGH){
//---( Turn all 8 relays ON in pairs)---
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(waittime); // wait for a second
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
delay(waittime);
digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF
delay(waittime * 15); // wait see all relays ON
if(digitalRead(pirPin) == LOW){
//---( Turn all 8 relays OFF in pairs)---
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(waittime);
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
delay(waittime);
digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
delay(waittime * 10); // wait see all relays OFF
}
}
}