Arduino Uno, Obstacle Avoiding Robot.

Hi, I have created a new 4 wheeled chassis with an ultrasonic sensor and created a program to have it avoid objects. But, all it does is go forward for a few seconds and start turning infinitely in a tight circle when nothing is near it whatsoever. I have the program so if it does see something within 5 cm it turns (left motors backwards, right motors forward) until the object is out of sight, then repeats.

This is the program here if any could help me I would be very much appreciated.

const int rightmotor = 12;
const int leftmotor = 13;
const int rightbrake = 9;
const int leftbrake = 8;
const int rightspeed = 3;
const int leftspeed = 11;
int trigPin = 7;
int echoPin = 8;
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(rightmotor, OUTPUT);
  pinMode(leftmotor, OUTPUT);
  pinMode(rightbrake, OUTPUT);
  pinMode(leftbrake, OUTPUT);
  analogWrite(rightspeed, 255);
  analogWrite(leftspeed, 255);
  digitalWrite(rightbrake, LOW);
  digitalWrite(leftbrake, LOW);
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  
   delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 5) {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
digitalWrite(rightmotor,LOW);
digitalWrite(leftmotor,HIGH);

  }
else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   digitalWrite(rightmotor,LOW);
   digitalWrite(leftmotor,HIGH);
  }  
  
}

Hi,
Welcome to the forum.

What do you get displayed on your monitor?

Did you write your code in stages?

First code the sonar and make sure it is stable and evaluated how it works, range, consistency of reading.

Second code the motor drives and make sure it is stable.

Third combine the two and make sure it is stable.

Tom.. :slight_smile:

It's not a great idea to act based on just one reading. Take "n" readings, throw out the high and low, and use the average of the remaining values (or use the median). The ping_median method in the NewPing library will do that for you. (This might not fix your current problem, but it should help prevent future problems.)

Thankyou for your help, I try your suggestions and come back to you

Patch.

else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   digitalWrite(rightmotor,LOW);
   digitalWrite(leftmotor,HIGH);
  }

Shouldn't both motors go the same direction now?

Please always do a Tools > Auto Format on your sketch before posting it to the forum. The automatic indentation can be very helpful in finding bugs and it will make it much easier for us to read.

outsider:

else {

Serial.println ("No obstacle detected. going forward");
  delay (15);
  digitalWrite(rightmotor,LOW);
  digitalWrite(leftmotor,HIGH);
 }



Shouldn't both motors go the same direction now?

Thank YOU SOOOO MUCH I should have realized that

But, the promblem continues, anymore help would be great.

pert:
Please always do a Tools > Auto Format on your sketch before posting it to the forum. The automatic indentation can be very helpful in finding bugs and it will make it much easier for us to read.

This is the auto formatted code:

const int rightmotor = 12;
const int leftmotor = 13;
const int rightbrake = 9;
const int leftbrake = 8;
const int rightspeed = 3;
const int leftspeed = 11;
int trigPin = 7;
int echoPin = 8;
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(rightmotor, OUTPUT);
  pinMode(leftmotor, OUTPUT);
  pinMode(rightbrake, OUTPUT);
  pinMode(leftbrake, OUTPUT);
  analogWrite(rightspeed, 255);
  analogWrite(leftspeed, 255);
  digitalWrite(rightbrake, LOW);
  digitalWrite(leftbrake, LOW);
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 5) {
    Serial.println ("Close Obstacle detected!" );
    Serial.println ("Obstacle Details:");
    Serial.print ("Distance From Robot is " );
    Serial.print ( distance);
    Serial.print ( " CM!");
    Serial.println (" The obstacle is declared a threat due to close distance. ");
    Serial.println (" Turning !");
    digitalWrite(rightmotor, LOW);
    digitalWrite(leftmotor, HIGH);

  }
  else {
    Serial.println ("No obstacle detected. going forward");
    delay (15);
    digitalWrite(rightmotor, LOW);
    digitalWrite(leftmotor, HIGH);
  }

}

Hi,

In your program you have the following instructions:

else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   digitalWrite(rightmotor,LOW);
   digitalWrite(leftmotor,HIGH);

  }

It says ("No obstacle detected. going forward");

and you put the instructions "rightmotor,LOW" ??, and "leftmotor,HIGH"......

I think both should be HIGH!!!

Hi,
I posted questions in post#1.

What do you get displayed on your monitor?

Did you write your code in stages?

First code the sonar and make sure it is stable and evaluated how it works, range, consistency of reading.

Second code the motor drives and make sure it is stable.

Third combine the two and make sure it is stable.

Please answer the first two and did you do the other three points?

Also how are you powering the arduino and motors?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a picture of your project please?
Have you run your machine with the wheels off the ground, freewheeling and free area in front of the sonar, then place an object in the field getting closer and closer?

Tom... :slight_smile:

I don't really get all that you are saying, espically stable and stages. My moniter says it has no problems.

Patch04:
I don't really get all that you are saying, espically stable and stages.

I think it's pretty clear. If you try to slap together a complex project in one go then it's highly likely that there will be a problem in the software or hardware and it will be extremely difficult to find the cause of the problem. If you break the project down into small parts and get each part working perfectly before moving to the next one then you will be able to identify the exact cause of any problems you encounter very easily. By stable TomGeorge means that you have thoroughly tested that part/stage of project and found that it works as expected with no bugs. This approach is very important for a beginner but even someone with lots of experience will still do things this way because it's the most efficient way to do it. Even though it might seem at first to be slower you will find it saves you much time and frustration in the end. I recommend saving the test sketches you write at each stage to use later if you run into a problem with that specific part of the project later.