I have PIR motion sensor connected to pin 6.
When motion is detected, I switch a solenoid for XXXX miliseconds.
My problem is that I need to stop sensing the PIR once motion has been detected.
Wait for a period, say 5 seconds, then start detecting PIR for motion.
The problem I have is that it seems to loop continuously.
//*****HANDLE PIR MOTION SENSING**************
if (PIRarmed == 1){
PIRval = digitalRead(PIRinputPin); // read input value
if (PIRval == HIGH) { // check if the input is HIGH
// we have just detected motion
Serial.println("Motion detected!");
digitalWrite(armPIR, LOW); // Disarm PIR - switch PIR OFF
//PIRarmed = 0;
Serial.println("Disarm PIR");
digitalWrite(SolenoidPin, HIGH); // Turn Solenoid ON
Serial.println("Solenoid ON");
delay(2000); // Keep Solenoid ON for 2 seconds
digitalWrite(SolenoidPin, 0); // Turn Solenoid OFF
Serial.println("Solenoid OFF");
delay(5000); // wait 5 seconds before switching PIR ON
digitalWrite(armPIR, HIGH); // Arm PIR - switch PIR ON
//PIRarmed = 1;
Serial.println("Arm PIR");
}
}
digitalWrite(armPIR, LOW); // Disarm PIR - switch PIR OFF
are you actually powering the PIR from a pin or driving its power from that pin?
a PIR needs about 30 seconds to a minute to calibrate after being turned on... that might be your issue. Also most PIR have a potentiometer used to keep the output HIGH (or LOW depends on the PIR) after a detection for a certain amount of time, regardless of the status of the detection
Thanks for the replies;
I am driving the PIR from the pin - switching the PIR ON using a transistor.
I need to stop sensing the PIR pin for about 5 seconds
The problem I have is that it seems to loop continuously.
No, the problem is that you only posted a snippet of your code.
You need to record when you turn the solenoid on. On each pass through loop(), see if the solenoid on time is non-zero, and, it is it, is now - then greater than the "ignore the PIR sensor value" interval? If it is, set the solenoid on time back to 0.
If the solenoid on time is 0, read the PIR sensor. If not, don't.
Here is my entire code.
I use a wireless remote to switch/enable the PIR.
/*
Pepper Spray Controller
*/
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int armPIR = 4; // the pin that the PIR is attached to
const int piezoPin = 3; // D3 PIEZO
const int PIRinputPin = 6; // D6 PIR INPUT
const int SolenoidPin = 5; // D5 SOLENOID
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
//int sensePIRState = LOW; // we start, assuming no PIR motion detected
int PIRval = 0; // variable for reading the PIR status
int PIRarmed = 0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
pinMode(armPIR, OUTPUT);
pinMode(PIRinputPin, INPUT);
pinMode(SolenoidPin, OUTPUT);
digitalWrite(SolenoidPin, 0);
// initialize serial communication:
Serial.begin(9600);
PIRarmed = 0;
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is LOW then the button
// went from off to on:
buttonPushCounter++;
Serial.println("Button Press");
Serial.print("Number of button Presses: ");
Serial.println(buttonPushCounter);
// Sound Piezo on each button press
digitalWrite(piezoPin, HIGH);
delay(100);
digitalWrite(piezoPin, LOW);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
//*****HANDLE WIRELESS REMOTE SWITCHING**************
// Arm PIR on two button presses
if (PIRarmed == 0 && buttonPushCounter == 2 ){
PIRarmed = 1;
// Sound PIEZO alert
delay(500);
digitalWrite(piezoPin, HIGH);
delay(300);
digitalWrite(piezoPin, LOW);
delay(50);
digitalWrite(piezoPin, HIGH);
delay(300);
digitalWrite(piezoPin, LOW);
delay(50);
digitalWrite(piezoPin, HIGH);
delay(300);
digitalWrite(piezoPin, LOW);
delay(5000); // Delay PIR Armed for xxxx seconds
digitalWrite(armPIR, HIGH); // Arm PIR
digitalWrite(piezoPin, HIGH);
delay(300);
digitalWrite(piezoPin, LOW);
Serial.println("PIR Armed");
// Reset Button Counter to 0
buttonPushCounter = 0;
}
// Disarm PIR on two button presses
if (PIRarmed == 1 && buttonPushCounter == 2 ){
PIRarmed = 0;
digitalWrite(armPIR, LOW); // Disarm PIR
// Sound PIEZO alert
delay(500);
digitalWrite(piezoPin, HIGH);
delay(1000);
digitalWrite(piezoPin, LOW);
Serial.println("PIR Disarmed");
// Reset Button Counter to 0
buttonPushCounter = 0;
digitalWrite(SolenoidPin, 0);
}
//***************************************************
//*****HANDLE PIR MOTION SENSING**************
if (PIRarmed == 1){
PIRval = digitalRead(PIRinputPin); // read input value
if (PIRval == HIGH) { // check if the input is HIGH
// we have just detected motion
Serial.println("Motion detected!");
digitalWrite(armPIR, LOW); // Disarm PIR - switch PIR OFF
//PIRarmed = 0;
Serial.println("Disarm PIR");
digitalWrite(SolenoidPin, HIGH); // Turn Solenoid ON
Serial.println("Solenoid ON");
delay(2000); // Keep Solenoid ON for 2 seconds
digitalWrite(SolenoidPin, 0); // Turn Solenoid OFF
Serial.println("Solenoid OFF");
delay(5000); // wait 5 seconds before switching PIR ON
digitalWrite(armPIR, HIGH); // Arm PIR - switch PIR ON
//PIRarmed = 1;
Serial.println("Arm PIR");
}
}
}