Arduino obstacle avoider only going left

I am posting my code below.
The problem is: The robot goes forward perfectly, but whenever it detects an obstacle, it only turns left and goes forward. No matter what I change, it turns left, whenever an obstacle is detected.

#include <Servo.h> 
#define trigPin 3
#define echoPin 2
#define left_front  4
#define left_back  5
#define right_front  6
#define right_back  7
int pos = 88;
int leftDistance, rightDistance;
Servo servo;
const int dangerThresh = 10;
long duration;

void setup ()
{ 
  Serial.begin (9600); 
  pinMode(left_front,OUTPUT);
  pinMode(left_back,OUTPUT);
  pinMode(right_front,OUTPUT);
  pinMode(right_back,OUTPUT);
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,LOW);
  servo.attach(9);
  servo.write(pos);
}
int durationF,distanceF,durationL,distanceL,durationR,distanceR;
void loop ()
{
int distanceFwd = ping(); 
if (distanceFwd>dangerThresh)
{
  GoFront(); 
}

else  
{
  Stop();
  LookLeft ();
  leftDistance = ping();
  delay (1000);
  LookRight ();
  rightDistance = ping();
  delay (1000);
  LookFront ();
  if (leftDistance>rightDistance)  
  { 
     GoLeft ();
     delay (500);
  }
  else if (rightDistance>leftDistance)
  {
     GoRight ();
     delay (500);
  }
  
  else 
  {
    GoLeft ();
    delay (1000);    
  }
  
  
}
}

void GoFront()
{
 digitalWrite(left_front,HIGH);
 digitalWrite(right_front,HIGH);
 digitalWrite(right_back,LOW);
 digitalWrite(left_back,LOW);
}

void GoBack()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,HIGH);
  digitalWrite(left_back,HIGH);	
}

void Stop()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,LOW);
}

void GoLeft()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,HIGH);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,HIGH);	
}

void GoRight()
{	
  digitalWrite(left_front,HIGH);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,HIGH);
  digitalWrite(left_back,LOW);	
}

long ping()
{
pinMode (trigPin,OUTPUT);
pinMode (echoPin,INPUT);
// Send out PING))) signal pulse
digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echoPin, HIGH);

//Convert duration into distance

return duration / 29 / 2;

}

void LookLeft ()
{                             
    servo.write(170);
    delay (500);
   
}

void LookRight ()
{
    servo.write(10);
    delay (1200);  
}

void LookFront ()
{
    servo.write(90);
    delay (500);  
}

Arduino_Obs_Avoider.ino (2.4 KB)

Hi, How many wheels does this bot have?? Looks like 4, but you seem to have only definded 4 control pins!

Also do you need:

else { GoLeft (); delay (1000); }

After doing the Left/Right bit? We need more details regarding the motors and the driver, etc. If you care to take a look here, you can see some of my early bots.www.melsaunders.co.uk under Electronics. I have code if needed.

Hope it helps, Regards.

Mel.

Hi, Can you please post a copy of your sketch, using code tags? Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf? This is so we can see how you are electronically controlling your motors and how you are powering them.

Tom...... :)

Perhaps if you set leftDistance and rightDistance in lookLeft() and lookRight() your
code would be able to tell the difference?

Hi, As TomG says we need more info in order to help you, we're only guessing and that's a waste of time and not much use to you either. Taking another look at your code this could be a 2 wheeled bot but things like:

define left_front 4

define left_back 5

define right_front 6

define right_back 7

Are a bit confusing, it could be what you mean is #define left_front 4 = left motor forwards, or #define right_back 7 = right motor backwards.... What driver are you using an H bridge, L293, etc.

What is this code supossed to DO!

void LookLeft() { servo.write(170); delay (500);

}

void LookRight () { servo.write(10); delay (1200); }

As it is it does NOTHNG but point your servo and sensor Left or Right, it does not take a reading.. Perhaps you should be looking left/right then call PING to take a reading.... Yes that is it, it always returns the same value, so always turns in the same direction!!!

See some of my bots here:www.melsaunders.co.uk under Electronics..

Hope it helps, Regards.

Mel.

Thanks for the replies :) Its a 2 wheeled robot and I used l298d motor driver. Its working fine now :)

Hi anik_2008, Glad to hear the bots now working, BUT please tell us why it's now working fine! What was the problem?? We need feedback just as you do, and without it this thread is of no use to anyone else, that might have a similar problem....

Regards

Mel.

I am posting the code that worked :slight_smile:

#include <Servo.h> 
#define trigPin 3
#define echoPin 2
#define left_front  4
#define left_back  5
#define right_front  6
#define right_back  7
int pos = 88;
int leftDistance, rightDistance;
Servo servo;
const int dangerThresh = 10;
long duration;

void setup ()
{ 
  Serial.begin (9600); 
  pinMode(left_front,OUTPUT);
  pinMode(left_back,OUTPUT);
  pinMode(right_front,OUTPUT);
  pinMode(right_back,OUTPUT);
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,LOW);
  servo.attach(9);
  servo.write(pos);
}
int durationF,distanceF,durationL,distanceL,durationR,distanceR;
void loop ()
{
int distanceFwd = ping();
distanceFwd = ping();
distanceFwd = ping();
if (distanceFwd>dangerThresh)
{
  GoFront(); 
}

else  
{
  Stop();
  LookLeft ();
  leftDistance = ping();
  delay (100);
  leftDistance = ping();
  delay (100);
  leftDistance = ping();
  delay (100);
  LookRight ();
  rightDistance = ping();
  delay (100);
  rightDistance = ping();
  delay (100);
  rightDistance = ping();
  delay (100);
  LookFront ();
  if (leftDistance>rightDistance)  
  { 
     GoRight ();
     delay (500);
  }
  else if (rightDistance>leftDistance)
  {
     GoLeft ();
     delay (500);
  }
  
  else 
  {
    GoLeft ();
    delay (1500);    
  }
  
  
}
}

void GoFront()
{
 digitalWrite(left_front,HIGH);
 digitalWrite(right_front,HIGH);
 digitalWrite(right_back,LOW);
 digitalWrite(left_back,LOW);
}

void GoBack()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,HIGH);
  digitalWrite(left_back,HIGH);	
}

void Stop()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,LOW);
}

void GoLeft()
{
  digitalWrite(left_front,LOW);
  digitalWrite(right_front,HIGH);
  digitalWrite(right_back,LOW);
  digitalWrite(left_back,HIGH);	
}

void GoRight()
{	
  digitalWrite(left_front,HIGH);
  digitalWrite(right_front,LOW);
  digitalWrite(right_back,HIGH);
  digitalWrite(left_back,LOW);	
}

long ping()
{
pinMode (trigPin,OUTPUT);
pinMode (echoPin,INPUT);
// Send out PING))) signal pulse
digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echoPin, HIGH);

//Convert duration into distance

return duration / 29 / 2;

}

void LookLeft ()
{                             
    servo.write(170);
    delay (500);
   
}

void LookRight ()
{
    servo.write(10);
    delay (1200);  
}

void LookFront ()
{
    servo.write(90);
    delay (500);  
}

Hi, Why are you doing this 3 times: leftDistance = ping(); delay (100); leftDistance = ping(); delay (100); leftDistance = ping(); delay (100);

And defining a variable twice?: int distanceFwd = ping(); distanceFwd = ping(); distanceFwd = ping(); if (distanceFwd>dangerThresh)

Bad coding, sure you'll get better, now you can move on and inprove..

Regards

Mel.

For value stability.... I am doing it 3 times And for improvement... u can take the average as well...

Hi, Why! 3 times, only the last ping reading will be used.............

u can take the average as well...

Not like that you can't.

Regards

Mel.

Hi Mel, When I first did the coding and uploaded it on my atmega, it wasn't working properly. So, I thought of taking it multiple times as sometimes the sr 04 sonar gets false positives, so taking it three times helps avoid that, even though the last reading is being taken, the false positive is being avoided. I hope this answers ur question. And btw, how do I set this post as solved? Btw, um gonna start working on a line follower from 4th January. Any sort of resources or links would be highly appreciated..... As it seems to me that you know these things quite well. Cheers