1

I am currently working on my first Arduino 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 on the Arduino Mega 2560 board 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.

I have already tried replacing the front PIR sensor twice and the sensor itself does not seem to be the issue.

The PIR sensors I am using are the 'HC-SR501' model. For all PIR sensors, the delay time is set to the lowest possible time (5 seconds) and the sensitivity is set to full.

I welcome any input or suggestions whatsoever. If you would like me to provide any additional information about this project please ask and I will do so.

-1

Try:

  • turn sensor OFF when robot is moving
  • turn sensor ON while robot is standing still
  • look for some prey
  • repeat from 1 one

Same schema goes with rotating :

  • rotate to some angle,
  • stop rotation
  • detect

xD I was wondering bout it too, but only thinking, not implemented yet!

I think that simple is best, but something advanced with faster reaction would be better!

  • 3
    Could you please edit this answer so that it uses correct grammar? Currently, it is hard to follow what you are trying to say. – The Guy with The Hat May 27 '15 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.