2 PIR Sensor controlling servo movement

Hi all, I hope to get some guidance. I am working on 2 PIR sensor (lets call it PIR Left and PIR right) with a 180 degrees servo and the configuration I need to achieve is:

  1. PIR left detect motion, servo rotates left, LED left “lit” , LED right “Off”
  2. PIR right detect motion, servo rotates right, LED right “lit” , LED left “Off”
  3. Both PIR detects motion, servo stop at the last read position, both LED “lit”
  4. No motion detects on PIR, servo stop at the last read position, both LED “Off”

I am working on the codes and the servo and indications do not corresponds to what I wanted.

These are my codes

#include <Servo.h>
Servo myservo;
//Servo library

int calibrationTime = 10;

int pos = 0;
int pinPIRleft = 2;
int pinLEDleft = 8;
int pinPIRright = 4;
int pinLEDright = 10;
//the digital pin declaration for the output for the various component

//MAIN PROGRAM//

//SETUP
void setup(){
Serial.begin(9600);
pinMode(pinPIRleft, INPUT);
pinMode(pinLEDleft, OUTPUT);
pinMode(pinPIRright, INPUT);
pinMode(pinLEDright, OUTPUT);
myservo.attach(9);

//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);
}

//////////////////////////////LOOP
void loop(){

if ((digitalRead(pinPIRleft) == HIGH) && (digitalRead(pinPIRright) == HIGH)) {
digitalWrite(pinLEDright, HIGH);
digitalWrite(pinLEDleft, HIGH);
// do something here if both sensors detect movement.
myservo.write(myservo.read());
delay(15);

}

if ((digitalRead(pinPIRleft) == LOW) && (digitalRead(pinPIRright) == LOW)) {
digitalWrite(pinLEDleft, LOW);
digitalWrite(pinLEDright, LOW);
// do something here if both sensors detect no movement.
myservo.write(myservo.read());
delay(15);
}

if ((digitalRead(pinPIRleft) == HIGH) && (digitalRead(pinPIRright) == LOW)) {
digitalWrite(pinLEDleft, HIGH);
digitalWrite(pinLEDright,LOW);
if(pos>180){
pos -= 1;
myservo.write(pos);
delay(15);
}

}
if ((digitalRead(pinPIRright) == HIGH)&& (digitalRead(pinPIRleft) == LOW)) {
digitalWrite(pinLEDright, HIGH);
digitalWrite(pinLEDleft, LOW);
if(pos<0){
pos += 1;
myservo.write(pos);
delay(15);
}
}
}

I am still figuring out if the sequence for my main loop is correct. Please help and advise me what have gone wrong.

Many many thanks

I am working on the codes and the servo and indications do not corresponds to what I wanted.

The code does something. You haven't explained what it does or how that differs from what you want.

    // do something here if both sensors detect movement.
     myservo.write(myservo.read());

Move the servo to where it currently is. How useful.

    // do something here if both sensors detect no movement.
    myservo.write(myservo.read());

Move the servo to where it currently is. How useful.

You don't have to do anything is the two sensors report the same value, you know. At least, I assume that you know that.

      if(pos>180){
      pos -= 1;      // if less than 180 degrees and the right sensor is off then move servo                                                   // increment servo degrees by +1
      myservo.write(pos);                                         // write the position to the servo
      delay(15);
    }

No. You need to decrement the value if it is greater than 0, or increment the value if it is less than 180.

the servo and indications do not corresponds to what I wanted.

You will need to tell us what is happening...

Also, have you tried two sketches before this in which you satisfy yourself that the PIRs and the servo are working correctly in isolation of each other, before you bring everything together in this sketch?

Here's a tip which might help btw, especially if you're not sure if the code is navigating the "if" statements correctly: use Serial to report back to the Arduino IDE monitor from time to time, to tell it which "if" you're in and / or what the values of pins and variables are.

Ok let me explain how the codes performed.

After the 10 sec calibration period, the Servo stuck at 90 degrees position and hold there. The two LEDs will "lit" randomly ON and OFF. Although the PIR is right in front of myself, it is not continuous "lit" up.

@PaulS

I am not familiar of how to write the code so that upon two PIR detects how to command the servo to halt at the position that it is travelling. Can teach me?

@JimboZA

I have tested with the PIRSense code and both my PIR are functioning to the detection.

Thanks to PaulS guidance, I test again on the servo rotation by: 1) removing the part of two PIR detecting at the same time 2) change in code increment and decrement value.

it now rotates accordingly but some instances when my left PIR was detected, the left LED "lit" and servo rotates one direction, the right LED will "lit" up randomly and the servo will be rotate counter-direction and create a jerk effect. I am going to test if it works individually, can give me some advise if there is a sequence that I should use to follow?

can give me some advise if there is a sequence that I should use to follow?

Sure. Make some code change(s). Test them. If there are problems, post the changed code and describe what the code actually does and how that differs from what you want.

You can save a lot of iterations by using Serial.print() to print out stuff.