Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

I need help about obstacle avoiding robot

So… hi there im new for projects like that and i started to make it working (btw im using arduino uno and ultrasonic sensor and a motor shield l293d for this) but i couldn’t manage to make it work with the servo motor in it all works almost fine but i have a code for it in separated
This is the code without the servo motor:

const int trig = 12;
const int echo = 11;
const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;
 
int duration = 0;
int distance = 0;
 
void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
  
  Serial.begin(9600);
 
}
 
void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);
 
 
  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);
  
 
  if ( distance < 20 )
  {
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(100);
  }
  else
  {
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
  }
 
}

And this is the code for only the servo:

#include &lt;Servo.h&gt;

Servo myservo; // create servo object to control a servo

const int trigPin = 2;

const int echoPin = 4;

void setup() {

// initialize serial communication:

Serial.begin(9600);

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm &gt; 7 &amp;&amp; cm &lt; 14)

{

myservo.write(140); // sets the servo position according to the scaled value

delay(4000);

}

else if ( cm &lt; 8)

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

else

{

myservo.write(40); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

The problem is that i can’t find a way to marge it and make it work together.
Thanks in advance.

What is the servo supposed to do?

In general, a good approach to merging programs is to first make sure that you understand exactly what each line in each separate program is doing, and why it is necessary.

With that clear understanding, it should not be difficult to write a new program, line by line, that combines parts of others and does exactly what you want.

Hi Jim thanks for replying I’m really new at it
The servo motor suppose to be working as shown in the video:


The problem is i can’t find a matching code for the l293d chip

I don’t know what you mean by “matching code”. The L293 is a brushed DC motor driver, and is not useful for hobby servos.

The Arduino programming console has a “servo sweep” example that might help with the servo action shown in the video.