Servo won't respond with ultrasonic sensor

Hi all I want to know is why my servo won't work with the sensor. Please I'm new at this I just want to know what I need to do. I'm using my computer to power up my project.`#include<Servo.h>
#define trigpin A0 // Trigger pin
#define echopin A1 // echo pin

Servo myservo1; // Left leg
Servo myservo2; // Right leg
int pos = 0;
int pos1= 100;
int pos2 = 90;
int pos3 = 80;

void setup() {
Serial.begin(9600);
myservo1.attach(10);
myservo2.attach(9);

pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);

}

void loop ()
{

int duration, distance;
myservo2.write(pos);
myservo1.write(pos);
delay(400);
digitalWrite(trigpin, LOW);
delayMicroseconds(5);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration*0.034/2;
Serial.print(distance);
Serial.println("cm");
delay(50);

myservo1.write(pos3);
delay(100);
myservo1.write(pos2);
delay(100);
myservo2.write(pos3);
delay(100);
myservo2.write(pos2);
delay(100);
if (distance <= 10)
{
Serial.print("distance is less than or equal to");
myservo1.write(pos1);
delay(100);
myservo1.write(pos2);
delay(100);
myservo2.write(pos1);
delay(100);
myservo2.write(pos2);
delay(100);
}
else
{
Serial.print("distance is greater");
myservo1.write(pos3);
delay(100);
myservo1.write(pos2);
delay(100);
myservo2.write(pos3);
delay(100);
myservo2.write(pos2);
delay(100);

}

}



`

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

#include<Servo.h>
#define trigpin A0 // Trigger pin
#define echopin A1 // echo pin

Servo myservo1; // Left leg
Servo myservo2; // Right leg
int pos = 0;
int pos1= 100;
int pos2 = 90;
int pos3 = 80;



void setup() {
Serial.begin(9600);
myservo1.attach(10);
myservo2.attach(9);

pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);

}

void loop ()
{
  
int duration, distance;
myservo2.write(pos);
myservo1.write(pos);
delay(400);
digitalWrite(trigpin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = duration*0.034/2;
  Serial.print(distance);
  Serial.println("cm");
  delay(50);
  
  myservo1.write(pos3);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos3);
  delay(100);
  myservo2.write(pos2);
  delay(100);
  if (distance <= 10)
  {
    Serial.print("distance is less than or equal to");
    myservo1.write(pos1);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos1);
  delay(100);
  myservo2.write(pos2);
  delay(100); 
  }
  else
  {
    Serial.print("distance is greater");
  myservo1.write(pos3);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos3);
  delay(100);
  myservo2.write(pos2);
  delay(100);
  
  
  }
  
  }
1 Like

Are you providing enough power to the motor driver shield?

yes

That is a poor description of the problem. It conveys little useful information. Do the servos move at all? If so what is wrong with the way that they move? If not, are you sure that they getting power? Are you sure that they are getting a signal?

What do your serial prints tell you? Is the rangefinder working?

Are you giving the servos time to move. A typical servo will be specified on the order of 200ms to move 60°.

All servos move fine. They go the direction I want them to go its just they won't respond with my ultrasonic sensor and that gives me measurements on the serial monitor.

Hi,
Does your sensor work without the motor code?

You have three sets of servo positions for two distance conditions.
Why do you need this part on EVERY LOOP of your code?

 delay(50);
  
  myservo1.write(pos3); 
  delay(100);                     
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos3);
  delay(100);
  myservo2.write(pos2);
  delay(100);
  if (distance <= 10)

If you are "less than or equal to" then the servos are being moved from one position then back to this position each time the code loops.
If you are "greater than", then the servos are being move to the other position then back to this position each time the code loops.

What do you want the servos to do?
Move to ONE position and stay if "greater than", then move to the other position and stay while "less than or equal to"

Tom... :smiley: :+1: :coffee: :australia:

I would like my servos to move back when less than or equal to so I would want both servos to go 80 - 90 degrees. When greater than I want my servos to move forward 90 - 100 degrees. But I don't know how to set that up.

Hi,
Try this edit.
I have made 2 distinct if statements and made 10 and 11 your thresholds, this will prevent jitter around 10cm, it is called hysteresis.

#include<Servo.h>
#define trigpin A0 // Trigger pin
#define echopin A1 // echo pin

Servo myservo1; // Left leg
Servo myservo2; // Right leg
int pos = 0;
int pos1 = 100;
int pos2 = 90;
int pos3 = 80;
int duration;
int distance;


void setup()
{
  Serial.begin(9600);
  myservo1.attach(10);
  myservo2.attach(9);
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);
  myservo1.write(pos3);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos3);
  delay(100);
  myservo2.write(pos2);
  delay(100);
}

void loop ()
{
  myservo2.write(pos);
  myservo1.write(pos);
  delay(400);
  digitalWrite(trigpin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW);
  duration = pulseIn(echopin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print(distance);
  Serial.println("cm");
  delay(50);
  if (distance <= 10)
  {
    Serial.print("distance is less than or equal to");
    myservo1.write(pos1);
    delay(100);
    myservo1.write(pos2);
    delay(100);
    myservo2.write(pos1);
    delay(100);
    myservo2.write(pos2);
    delay(100);
  }
  if (distance <= 11)
  {
    Serial.print("distance is greater");
    myservo1.write(pos3);
    delay(100);
    myservo1.write(pos2);
    delay(100);
    myservo2.write(pos3);
    delay(100);
    myservo2.write(pos2);
    delay(100);
  }
}

Tom... :smiley: :+1: :coffee: :australia:
PS, You aren't trying to make your project WALK in one direction if close and WALK in the other direction if further away?

I'm trying to make an obstacle-avoiding robot using servos. But thank you for the code I'm gonna try it!

I tried the code and it twitches when obstacles aren't in front of the sensor

And what have you done about that, then? Have you tried to work out why? Or are you expecting to be spoon-fed without making any effort yourself?

I'm new at this and all I've been doing is making the effort I just want to know what I'm doing wrong and how I can fix it. I'm not being spoon-fed I just want help with my project thats it.

So I'll repeat my question. The servos are twitching. What have you done to investigate why?

I tried different ways of coding my servos both through setup and loop.I don't know what else to do.

Hi,
How are you powering your project?

Tom.. :smiley: :+1: :coffee: :australia:

through my computer

Unless you are actually connecting the servos to a 5 V power cable (like the disk drives) inside the computer itself, the USB port is not going to provide appropriate power for a servo, let alone more than one.

That is why people keep on asking you. :grimacing:

Gotcha so it's not my code it's the power I'm giving it?