Obstacle Avoiding Robot with L293D

Hi,

I want to make a Obstacle Avoiding Robot with Motor Shield L293D. I have 2 DC motors,one Ultrasonic sensor and one servo motor.

I have completed circuit but i have not found code for ardiuno.Please,can you help me? Actually,i found a code but it not working exactly.

The code:

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

#define TRIG_PIN A4 // Pin A4 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN A5 // Pin A5 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 200 // sets maximum useable sensor measuring distance to 200cm
#define MAX_SPEED 80 // sets speed of DC traction motors to 180/256 or about 70% of full speed - to get power drain down.
#define MAX_SPEED_OFFSET 40 // this sets offset to allow for differences between the two DC traction motors ****** from 20
#define COLL_DIST 10 // sets distance at which robot stops and reverses to 10cm
#define TURN_DIST COLL_DIST+10 // sets distance at which robot veers away from object (not reverse) to 20cm (10+10)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to measure distance.

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(4, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency
Servo myservo;  // create servo object to control a servo 

int leftDistance, rightDistance; //distances on either side
int curDist = 0;
String motorSet = "";
int speedSet = 0;

//-------------------------------------------- SETUP LOOP ----------------------------------------------------------------------------
void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object 
 myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
 delay(1000); // delay for one seconds
}
//------------------------------------------------------------------------------------------------------------------------------------

//---------------------------------------------MAIN LOOP ------------------------------------------------------------------------------
void loop() {
 myservo.write(90);  // move eyes forward
 delay(90);
 curDist = readPing();   // read distance
 if (curDist < COLL_DIST) {changePath();}  // if forward is blocked change direction
 moveForward();  // move forward for 1/2 second
 delay(500);
}
//-------------------------------------------------------------------------------------------------------------------------------------

void changePath() {
 moveStop();   // stop forward movement
 myservo.write(36);  // check distance to the right
   delay(500);
   rightDistance = readPing(); //set right distance
   delay(500);
   myservo.write(144);  // check distace to the left
   delay(700);
   leftDistance = readPing(); //set left distance
   delay(500);
   myservo.write(90); //return to center
   delay(100);
   compareDistance();
 }

 
void compareDistance()   // find the longest distance
{
 if (leftDistance>rightDistance) //if left is less obstructed 
 {
   turnLeft();
 }
 else if (rightDistance>leftDistance) //if right is less obstructed
 {
   turnRight();
 }
  else //if they are equally obstructed
 {
   turnAround();
 }
}


//-------------------------------------------------------------------------------------------------------------------------------------

int readPing() { // read the ultrasonic sensor distance
 delay(70);   
 unsigned int uS = sonar.ping();
 int cm = uS/US_ROUNDTRIP_CM;
 return cm;
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveStop() {motor1.run(RELEASE); motor2.run(RELEASE);}  // stop the motors.
//-------------------------------------------------------------------------------------------------------------------------------------
void moveForward() {
   motorSet = "FORWARD";
   motor1.run(FORWARD);      // turn it on going forward
   motor2.run(FORWARD);      // turn it on going forward
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
 {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
   delay(5);
 }
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveBackward() {
   motorSet = "BACKWARD";
   motor1.run(BACKWARD);      // turn it on going forward
   motor2.run(BACKWARD);     // turn it on going forward
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
 {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
   delay(5);
 }
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnRight() {
 motorSet = "RIGHT";
 motor1.run(FORWARD);      // turn motor 1 forward
 motor2.run(BACKWARD);     // turn motor 2 backward
 delay(400); // run motors this way for 400ms
 motorSet = "FORWARD";
 motor1.run(FORWARD);      // set both motors back to forward
 motor2.run(FORWARD);      
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnLeft() {
 motorSet = "LEFT";
 motor1.run(BACKWARD);     // turn motor 1 backward
 motor2.run(FORWARD);      // turn motor 2 forward
 delay(400); // run motors this way for 400ms
 motorSet = "FORWARD";
 motor1.run(FORWARD);      // turn it on going forward
 motor2.run(FORWARD);      // turn it on going forward
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnAround() {
 motorSet = "RIGHT";
 motor1.run(FORWARD);      // turn motor 1 forward
 motor2.run(BACKWARD);     // turn motor 2 backward
 delay(800); // run motors this way for 800ms
 motorSet = "FORWARD";
 motor1.run(FORWARD);      // set both motors back to forward
 motor2.run(FORWARD);      
}

,i found a code but it not working exactly.

What does that mean?

Please remember to use code tags when posting code.

Hi,

hasan_uckun, what is your electronics, programming, arduino, hardware experience?

Just using someone else's sketch is looking for trouble as you do not know how it works.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?
And how are you powering the robot?

Thanks..Tom...... :slight_smile:

AWOL:
What does that mean?

Please remember to use code tags when posting code.

I want to write a code with Ardiuno Uno.I had changed the code which i wrote before.There is a problem.The problem is that if robot's motor catch a obstacle,motors always will run because sensor is not see the any obstacles which is behind the sensor.But i did not want this.I want to all motors stop and run backward until 1 second.What do i do?

Now,i have tried to work following code:

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_SPEED 180  
#define COLL_DIST 40 
#define TURN_DIST COLL_DIST +20 
NewPing sonar(TRIG_PIN, ECHO_PIN); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(4, MOTOR12_1KHZ); 
Servo servo; 
const int led = 15;
int leftDistance, rightDistance;
int curDist;
int speedSet = 0;


void setup() {
 Serial.begin(9600);
 pinMode(led,OUTPUT);
 servo.attach(9);  
 servo.write(90);
 delay(500); 

}

void loop() {
 Serial.println(readPing());
 digitalWrite(led,HIGH);
 Serial.println("START FORWARD");
 accelerateForward();
 if (readPing()<COLL_DIST){
   Serial.println("Collision Detected");
   moveStop();
   changePath();
   compareDistance();
   accelerateForward();
 }



}


void changePath() { 
 servo.write(20);  
 delay(600);
 rightDistance = readPing(); 
 delay(600);
 servo.write(90); 
 delay(600);
 servo.write(160);  
 delay(600);
 leftDistance = readPing();
 delay(600);
 servo.write(90);
 delay(600);
}


void compareDistance()  
{
 if (leftDistance>rightDistance) 
 {
   turnLeft();
 }
 else if (rightDistance>leftDistance)
 {
   turnRight();
 }
 else 
 {
   turnAround();
 }
}



int readPing() {
 delay(70);  
 unsigned int uS = sonar.ping();
 int cm = uS/US_ROUNDTRIP_CM;
 Serial.print(cm);
 Serial.println(" cm");
 return cm;
}

void moveStop() {
 Serial.println("Stopped");
 motor1.run(RELEASE); 
 motor2.run(RELEASE);
 delay(800);
}  

void accelerateForward() {
 Serial.println("Start Forward");
 motor1.run(FORWARD);     
 motor2.run(FORWARD);    
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
 {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet);
 }
}


void turnRight() {
 Serial.println("Turn Right");
 motor1.run(FORWARD);     
 motor2.run(BACKWARD);  
 delay(500);      
} 

void turnLeft() {
 Serial.println("Turn Left");
 motor1.run(BACKWARD);     
 motor2.run(FORWARD);     
 delay(500);
} 

void turnAround() {
 Serial.println("Turn Around");
 motor1.run(FORWARD);      
 motor2.run(BACKWARD);     
 delay(1000);
}

I want to write a code with Ardiuno.

it's OK - you don't have to ask permission.

I had changed the code which i wrote before.

And . . ?
What happened?

motors always will run because sensor is not see the any obstacles which is behind the sensor.

I can't think of many directional sensors that will detect objects placed behind them

(what about the code tags?)

TomGeorge:
Hi,

hasan_uckun, what is your electronics, programming, arduino, hardware experience?

Just using someone else's sketch is looking for trouble as you do not know how it works.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?
And how are you powering the robot?

Thanks..Tom...... :slight_smile:

you wanted to picture my circuit,but it not necessary i used ardiuno motor shield,i said before.

I want to write a code with Ardiuno Uno.I had changed the code which i wrote before.There is a problem.The problem is that if robot's motor catch a obstacle,motors always will run because sensor is not see the any obstacles which is behind the sensor.But i did not want this.I want to all motors stop and run backward until 1 second.What do i do?

Now,i have tried to work following code:

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_SPEED 180  
#define COLL_DIST 40 
#define TURN_DIST COLL_DIST +20 
NewPing sonar(TRIG_PIN, ECHO_PIN); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(4, MOTOR12_1KHZ); 
Servo servo; 
const int led = 15;
int leftDistance, rightDistance;
int curDist;
int speedSet = 0;


void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
servo.attach(9);  
servo.write(90);
delay(500); 

}

void loop() {
Serial.println(readPing());
digitalWrite(led,HIGH);
Serial.println("START FORWARD");
accelerateForward();
if (readPing()<COLL_DIST){
  Serial.println("Collision Detected");
  moveStop();
  changePath();
  compareDistance();
  accelerateForward();
}



}


void changePath() { 
servo.write(20);  
delay(600);
rightDistance = readPing(); 
delay(600);
servo.write(90); 
delay(600);
servo.write(160);  
delay(600);
leftDistance = readPing();
delay(600);
servo.write(90);
delay(600);
}


void compareDistance()  
{
if (leftDistance>rightDistance) 
{
  turnLeft();
}
else if (rightDistance>leftDistance)
{
  turnRight();
}
else 
{
  turnAround();
}
}



int readPing() {
delay(70);  
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
Serial.print(cm);
Serial.println(" cm");
return cm;
}

void moveStop() {
Serial.println("Stopped");
motor1.run(RELEASE); 
motor2.run(RELEASE);
delay(800);
}  

void accelerateForward() {
Serial.println("Start Forward");
motor1.run(FORWARD);     
motor2.run(FORWARD);    
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
  motor1.setSpeed(speedSet);
  motor2.setSpeed(speedSet);
}
}


void turnRight() {
Serial.println("Turn Right");
motor1.run(FORWARD);     
motor2.run(BACKWARD);  
delay(500);      
} 

void turnLeft() {
Serial.println("Turn Left");
motor1.run(BACKWARD);     
motor2.run(FORWARD);     
delay(500);
} 

void turnAround() {
Serial.println("Turn Around");
motor1.run(FORWARD);      
motor2.run(BACKWARD);     
delay(1000);
}

AWOL:
it's OK - you don't have to ask permission.
And . . ?
What happened?
I can't think of many directional sensors that will detect objects placed behind them

(what about the code tags?)

while the robot runs forward,sensor can not see the obstacles which is its the rightside and leftside therefore motor or wheels is being catched by the obstacle.

Have you tried mounting the sensors lower down?

AWOL:
Have you tried mounting the sensors lower down?

I had done but it was unsuccessful.Probably,i must solve writing as the code: if sensor's datas(distance) are same,it's time motors stop and run the backward until 1 seconds.I wrote that but it was useless.I must cahange this code but what?

void accelerateBackward ()  {
  if (readPing()==readPing()){
  moveStop();  
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);     
  delay(1000);
}
}