PIR Sensors Issues

Hello guys, I am currently working on my first project and I am having some issues that I am seeking help with. The robot I am building must have human following capabilities and to do so I am using 3 PIR sensors (One on the front, one on the left and one on the right) and an Ultrasonic Sensor so the robot won’t run into a wall or a person it is following. To handle the motors I am using the Adafruit Motor Shield (V.1)

The way I am trying to make this work is as follows.

The robot’s main PIR sensor is the one attached to the front. If this sensor reads HIGH then the robot will move forward while checking its distance from any objects in front of it in which case it stops and reverses until there is at least 30cm of space between itself and the object in front. The radius of this sensor had to be reduced so that it only reads HIGH if the person is exactly in front of the robot. This was done using carton paper and black duct tape. Two images of this have been provided below in case this has something to do with my issue.

The other 2 PIR sensors attached to the sides will tell the robot if it should turn left or right. If the left PIR is triggered then the robot will start turning left and keep doing so until the front PIR finally reads HIGH, and then it moves forward. The same thing is done with the right PIR except that it moves right. These PIR sensors are handled using interrupts as can be seen in the provided code.

The code I am using can be found below:

#include <AFMotor.h>
#include <NewPing.h>

#define trigPin 34
#define echoPin 32

NewPing sonar(trigPin, echoPin);
int minimumDistance = 30;
int currentDistance = 0;

int calibrationTime = 60;
int redLED = 22;
int greenLED = 24;

int PIR = 26;
volatile int PIR_val = 0;
int PIR_left = 19; // (interrupt 4)
int PIR_right = 18; // (interrupt 5)

AF_DCMotor motorL(1, MOTOR12_64KHZ);
AF_DCMotor motorR(2, MOTOR12_64KHZ);

void setup()
{
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

  pinMode(PIR, INPUT);
  pinMode(PIR_left, INPUT);
  pinMode(PIR_right, INPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  digitalWrite(redLED, HIGH);
  // Calibration for PIR sensors
  for (int i = 0; i < calibrationTime; i++)
  {
    delay(1000);
  }
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);

  attachInterrupt(4, turnLeft, RISING);
  attachInterrupt(5, turnRight, RISING);
}

void loop()
{
  delay(50);
  currentDistance = sonar.ping_cm();

  if (currentDistance < minimumDistance && currentDistance > 0)
  {
    moveBackward();
  }
  else
  {
    PIR_val = digitalRead(PIR);

    if (PIR_val == HIGH)
    {
      moveForward();
    }
  }
}

void moveForward()
{
  motorL.run(FORWARD);
  motorL.setSpeed(180);
  motorR.run(FORWARD);
  motorR.setSpeed(200);
}

void moveBackward()
{
  motorL.run(RELEASE);
  motorR.run(RELEASE);
  motorL.run(BACKWARD);
  motorL.setSpeed(190);
  motorR.run(BACKWARD);
  motorR.setSpeed(225);

  while (currentDistance < 30 && currentDistance > 0)
  {
    currentDistance = sonar.ping_cm();
  }

  motorL.run(RELEASE);
  motorR.run(RELEASE);
}

void turnLeft()
{
  motorL.run(BACKWARD);
  motorL.setSpeed(150);
  motorR.run(FORWARD);
  motorR.setSpeed(150);

  // While front PIR is reading LOW
  while (PIR_val == LOW)
  {
    PIR_val = digitalRead(PIR);
  }
  // After front PIR reads HIGH
  moveForward();
}

void turnRight()
{
  motorR.run(BACKWARD);
  motorR.setSpeed(150);
  motorL.run(FORWARD);
  motorL.setSpeed(150);

  // While front PIR is reading low
  while (PIR_val == LOW)
  {
    PIR_val = digitalRead(PIR);
  }
  // After front PIR reads HIGH
  moveForward();
}

The first issue I am having is that as soon as the PIR sensors are done calibrating, the robot starts moving forward by itself. Even if I leave the robot alone inside a room with the door closed, after the calibration time passes the robot automatically moves forward on its own.

The other issue I am having is also with the front PIR sensor. While the robot is turning and looking for a human as seen in the ‘turnLeft()’ and ‘turnRight()’ methods, the robot will sometimes move forward toward a random direction instead of towards me.

The PIR sensors I am using are the ‘HC-SR501’ model and I bought them from here: http://www.ebay.co.uk/itm/5x-New-HC-SR501-Infrared-PIR-Motion-Sensor-Module-for-Arduino-Raspberry-Blue-New-/261667599022?pt=LH_DefaultDomain_3&hash=item3cec9ab2ae
For all PIR sensors, the delay time is set to the lowest possible time (5 seconds) and the sensitivity is set to full.

Thank you for reading, I welcome any input or suggestions whatsoever. If you would like me to provide any additional information please ask and I will do so.

The front PIR will sense a HIGH until it is within 30cm of an object and while not within 30cm of an object, you move the robot.

So of course the robot moves, even if you lock it up in another room. You have programmed it to do so, it will only stop 30cm from the door waiting for ever for you to come and open the door, then backing off the door to keep a distance of 30cm from the door.

I dont think there is a problem with your PIR or your program. Its your logic (or method) that is teasing you.

As usual the biggest opponent in programming problems is - yourself ;)

I like him robot you have built, reminds me of E.T. :P

Anders53: The front PIR will sense a HIGH until it is within 30cm of an object and while not within 30cm of an object, you move the robot.

So of course the robot moves, even if you lock it up in another room. You have programmed it to do so, it will only stop 30cm from the door waiting for ever for you to come and open the door, then backing off the door to keep a distance of 30cm from the door.

I dont think there is a problem with your PIR or your program. Its your logic (or method) that is teasing you.

As usual the biggest opponent in programming problems is - yourself ;)

I like him robot you have built, reminds me of E.T. :P

Thanks for your reply Anders53. I have changed my code around and some issues have been solved. I will keep on working with the code so that maybe it will perform as I want it to. :D

I am glad you like the robot :P