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