Question of my PIR motion tracking project

Hi guys, I am currently working on a basic PIR motion tracking project, I’m taking references from this website
http://luckylarry.co.uk/arduino-projects/arduino-very-basic-motion-tracking-with-2-pir-sensors/

my code and the pin are similar with the example in this website and put two PIR sensors facing right and left.
this are my codes:

#include <Servo.h>
int pos = 90;
int PIR1 = 11;
int led1 = 7;
int PIR2 = 12;
int led2 = 9;
Servo littleguy;

void setup() {
pinMode(PIR1, INPUT);
pinMode(led1, OUTPUT);
pinMode(PIR2, INPUT);
pinMode(led2, OUTPUT);
littleguy.attach(8);
}
void loop() {
littleguy.write(pos);
if (digitalRead(PIR1) == HIGH) { // when PIR1 detect something, turn left//
littleguy.write(180);
digitalWrite(led1, HIGH);
delay(2000);
}
else {
littleguy.write(pos); // if notthing there, turn back to the central//
digitalWrite(led1, LOW);
}
if (digitalRead(PIR2) == HIGH) { // if PIR 2 detect something, turn right//
littleguy.write(0);
digitalWrite(led2, HIGH);
delay(2000);
}
else {
littleguy.write(pos);
digitalWrite(led2, LOW);
}
}

But the problem is, when both of my PIR sensors goes HIGH in the same time( mean detected something) , the servo just went crazy and keep turning to left and right , can I set a priority for the codes? like if it detects something on the right, stay with it until the process is finish, if the PIR in the left suddenly detect something, ignore it before the last process is finish.

Hi

(tip: be sure to use the CODE tags in the forum so your code is more readable)

The main difference between your code and the one you were inspired by is where the example increments the pos variable by 1 or -1 when the sensor is high, therefore only adjusting the servo slightly each time. Yours in contrast sends the servo from 180 to 0, ie one side to the other. That will be causing your "turning to left and right". Also where your comment states it will be turning back to the central (sic) the pos variable remains unchanged. I expect before writing to the servo you'll want to set pos to 90 in that logic, or no movement will occur.

Certainly you can make the logic do a special case if both sensors read HIGH, and you can also chain together your if statements such that only one option is chosen. That could look like

if(digitalRead(PIR1) == HIGH && digitalRead(PIR2) == HIGH) {
// do stuff for special case where both PiR sensors are firing
} else if(digitalRead(PIR1) == LOW && digitalRead(PIR2) == LOW) {
// do stuff for case where both PiR sensors see nothing
}

Alternatively if you always wanted PIR1 to have priority over PIR2 that would be as simple as chaining the if statements together so PIR2 doesn't get read if PIR1 is active

if(digitalRead(PIR1) == HIGH) {
// do PIR#1 stuff
} else if(digitalRead(PIR2) == HIGH) {
// PIR#2 stuff that will only be done if PIR1 is not active
}

Notice the else there will make sure that the 2nd part of the statement only happens if the first part is NOT true. ie if PIR1 is high, the actions for that will be done and the next if statement will be ignored. Once PIR1 is LOW, the PIR2 value will be tested.

There are always lots of ways to do these things but I've tried not to stray too far from the style you're already using.

Cheers !
Geoff