arduino sensor pir

hello I would like to add a PIR sensor to my code but I do not know ? thank you

here is my code

unsigned long time;
bool isSirenON;
int irmotionPin = 4; // Pin of IR Motion Sensor
int siren = 13; // Pin Relay

void setup()
{
delay(30000);
isSirenON = false;
Serial.begin(9600);
pinMode(siren, OUTPUT); // Set Pin connected to Relay as an OUTPUT
digitalWrite(siren, LOW); // Relay OFF
}

void loop()
{
if (digitalRead(irmotionPin) == HIGH)
{ // If Motion detected
digitalWrite(siren, HIGH); // Turn Relay ON
isSirenON = true;
time = millis();
Serial.println("Relay is ON");
}
if(isSirenON)
if(millis()- time >= 10000)
{
digitalWrite(siren, LOW); // Turn Relay OFF
isSirenON = false;
Serial.println("Relay is OFF");
}
// Je peux faire autre chose en attendant.
delay(500);
}

This should work, what exactly is the problem?

Get rid of the delay() at the end of loop() , it is not doing anything useful and will just interfere
with reading the PIR.

EDIT: It's tradition here to give you a hard time for not using code tags ( </> )
See #7 How to use this forum

Good to see you carrying on the tradition! :slight_smile:

Weedpharma

Hutkikz:
This should work, what exactly is the problem?

Get rid of the delay() at the end of loop() , it is not doing anything useful and will just interfere
with reading the PIR.

EDIT: It's tradition here to give you a hard time for not using code tags ( </> )
See #7 How to use this forum

I just want to change the program for 2 sensor pir ?

frankblou:
I just want to change the program for 2 sensor pir ?

I can not change the program for 2 sensor pir

You can use this way

 void loop() {
 if(digitalRead(irmotion1pin)==HIGH && digitalRead(irmotion2pin)==LOW) {
 //Then siren1 is on for a time
 }
 if(digitalRead(irmotion1pin)==LOW && digitalRead(irmotion2pin)==HIGH) {
 //Then siren2 is on for a time
 }

And use

pinMode(irmotion1pin, INPUT);
pinMode(irmotion2pin, INPUT);

digitalWrite(irmotion1pin, LOW);
digitalWrite(irmotion2pin, LOW);

in setup()

this code is for detect one motion at a time

if you want detect motion form 2 sensors

if(digitalRead(irmotion1pin)==HIGH || digitalRead(irmotion2pin)==HIGH)

for 2 sensors and just one siren it could be:

if(digitalRead(irmotion1pin) == HIGH || digitalRead(irmotion2pin) == HIGH) {
 //code to turn on siren goes here
 }

   // this way does the same thing 
if(digitalRead(irmotion1pin) || digitalRead(irmotion2pin) ) {
 //code to turn on siren goes here
 }

The bitwise || symbol means : OR
so if pir1 OR pir2 == HIGH the siren will sound.

EDIT: corrections as pointed out by ias0601 sorry I missed that bit of code in your post

I have already mentioned that

"if you want detect motion form 2 sensors

if(digitalRead(irmotion1pin)==HIGH || digitalRead(irmotion2pin)==HIGH)"

Hutkikz:
for 2 sensors and just one siren it could be:

if(digitalRead(irmotion1pin) == HIGH || digitalRead(irmotion2pin) == HIGH) {

//code to turn on siren goes here
}

// this way does the same thing
if(digitalRead(irmotion1pin) || digitalRead(irmotion2pin) ) {
//code to turn on siren goes here
}



The bitwise || symbol means : OR 
so if pir1 OR pir2 == HIGH the siren will sound.

EDIT: corrections as pointed out by ias0601 sorry I missed that bit of code in your post

Its okay

unsigned long time;


bool isSirenON;
int irmotion1Pin = 4; // Pin of IR Motion Sensor
int siren = 13; // Pin Relay
int irmotion2Pin = 5;// Pin of IR Motion Sensor

void setup()
{
delay(30000);
isSirenON = false;
Serial.begin(9600);
pinMode(siren, OUTPUT); // Set Pin connected to Relay as an OUTPUT
digitalWrite(siren, LOW); // Relay OFF



}





 void loop() {
 if(digitalRead(irmotion1pin)==HIGH && digitalRead(irmotion2pin)==LOW) {
 //Then siren1 is on for a time
 }
 if(digitalRead(irmotion1pin)==LOW && digitalRead(irmotion2pin)==HIGH) {
 //Then siren2 is on for a time
 }
digitalWrite(siren, HIGH); // Turn Relay ON
isSirenON = true;
time = millis();
Serial.println("Relay is ON");
}
if(isSirenON)
if(millis()- time >= 10000)
{ 
digitalWrite(siren, LOW); // Turn Relay OFF
isSirenON = false;
Serial.println("Relay is OFF");
}

I can not find my erreure ?

frankblou:
I can not find my erreure ?

What do you mean?

I cant Understand?

change your setup() to this

void setup() {
 delay(30000);
 isSirenON = false;
 Serial.begin(9600);
 pinMode(siren, OUTPUT); // Set Pin connected to Relay as an OUTPUT
 digitalWrite(siren, LOW); // Relay OFF

 pinMode(irmotion1pin, INPUT);
 pinMode(irmotion2pin, INPUT);

 digitalWrite(irmotion1pin, LOW);
 digitalWrite(irmotion2pin, LOW);
}

my code not working

What is problem?

first get input from your sensor and turn a led on when input is high

then write a code to switch a relay to on with timer(Forget about pir sensor)

then write code to get input from pir sensor and relay

ok thank you

Arduino : 1.6.5 (Mac OS X), Carte : "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

sketch_dec16a.ino: In function 'void setup()':
sketch_dec16a:13: error: 'irmotion1pin' was not declared in this scope
sketch_dec16a:14: error: 'irmotion2pin' was not declared in this scope
sketch_dec16a.ino: In function 'void loop()':
sketch_dec16a:23: error: 'irmotion1pin' was not declared in this scope
sketch_dec16a:23: error: 'irmotion2pin' was not declared in this scope
'irmotion1pin' was not declared in this scope

Ce rapport contiendrait plus d'informations si l'option
"Montrer les informations de sortie pendant la compilation"
était activée dans Fichier > Préférences.

[/codeunsigned long time;
bool isSirenON;
int irmotion1Pin = 4; // Pin of IR Motion Sensor
int siren = 13; // Pin Relay
int irmotion2Pin = 5; // Pin of IR Motion Sensor

void setup() {
 delay(30000);
 isSirenON = false;
 Serial.begin(9600);
 pinMode(siren, OUTPUT); // Set Pin connected to Relay as an OUTPUT
 digitalWrite(siren, LOW); // Relay OFF
pinMode(irmotion1pin, INPUT);
 pinMode(irmotion2pin, INPUT);

 digitalWrite(irmotion1pin, LOW);
 digitalWrite(irmotion2pin, LOW);
} 



 void loop() {
 if(digitalRead(irmotion1pin)==HIGH && digitalRead(irmotion2pin)==LOW) 

  {
 

  
digitalWrite(siren, HIGH); // Turn Relay ON
isSirenON = true;
time = millis();
Serial.println("Relay is ON");
}
if(isSirenON)
if(millis()- time >= 10000)
{ 
digitalWrite(siren, LOW); // Turn Relay OFF
isSirenON = false;
Serial.println("Relay is OFF");
}
// Je peux faire autre chose en attendant. 
delay(500);
}
int irmotion1Pin = 4; // Pin of IR Motion Sensor
pinMode(irmotion1pin, INPUT);

Case matters!

I have to do what ?

irmotion1Pin is not the same as irmotion1pin notice the capital P .

Also

if(digitalRead(irmotion1pin)==HIGH && digitalRead(irmotion2pin)==LOW)
Means irmotion1pin AND irmotion2pin need to be activated to sound the alarm.

if(digitalRead(irmotion1pin)==HIGH || digitalRead(irmotion2pin)==LOW)
Means irmotion1pin OR irmotion2pin need to be activated to sound the alarm.

EDIT: Oops I wrote pushed when I meant activated