Ultrasonic sensor won't trigger servos

Hello, I'm new at this and I'm trying to make a my ultrasonic sensor trigger my servos but it doesn't do anything. I'm getting measurements in the serial monitor, what am I doing wrong and what do I need to add or get rid of?`#include<Servo.h>
#define trigpin 2 // Trigger pin
#define echopin 3 // 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(7);
myservo2.attach(8);

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);

}

}`

How are your servos powered? Can you get one of them running using the sweep example?

Post an image or 2 of your project.

Use code tags when posting code.

Describe your power supply situation.

Post an image or 2 of your project.

I'm using sweep I attached everything on a prototype shield. My servos are attached to the GND, 5V, and pins 7 and 8. My sensor is attached to GND, 5V, and pins 2 and 3. I'm also using an Arduino Uno.

Forgot to mention both servos run great

What serial output are you seeing?

You know Arduino 5V rail is not for powering motors and servos? Use a separate power supply with suitable current rating (1A per servo for small servos, more for larger). 95% of issues we see with servos is due to them being underpowered

sorry I don't know how to see my serial output and what else should I use for my servos?

Hi,

Did you write this code?
It has provision for the IDE serial monitor programmed into it.

What is your project supposed to do?

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

I'm trying to make an obstacle avoiding robot and the servos are giving me no problems it's just not working with my sensor

What are you getting in your serial monitor as you move an obstacle towards the UltraSonic sensor?
Does the serial monitor say;

"distance is less than or equal to

When the obstacle is to close?

Does the serial monitor say;

distance is greater

When the obstacle is outside the set range?

To make your serial easier to read edit;

Serial.print("distance is less than or equal to");
and
Serial.print("distance is greater");

to;

Serial.println("distance is less than or equal to");
and
Serial.println("distance is greater");

That way you will not get a continuous line across the monitor screen.

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

Ok cool and my serial monitor does show the measurements accurately

Hi,
Okay, now you are asking your servos to move fairly wide angles in 100ms, thats 1/10 second.
Change the delays to 500, to see if your servos respond.
You appear to be wanting them to move 10degrees in 1/10second.

Lengthen the delay so you can do some observable diagnostics.
How are you powering your project,
As asked previously a picture and a circuit diagram will help.
Just an image of a hand drawn circuit will be fine.

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

I'm using a 9V battery to power my project.

Alright did the changes and with 500 delay my servos were having a hard time moving but with 100 as my delay, they work great. I got rid of the else statement and when the sensors were close enough my servos changed speed but didn't go clockwise.

That is a problem if the 9V battery looks like:

Please post an image of your project.

Please put your code in code tags.

#include<Servo.h>
#define trigpin 2 // Trigger pin
#define echopin 3 // 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(7);
myservo2.attach(8);

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(pos1);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos1);
  delay(100);
  myservo2.write(pos2);
  delay(100);
  if (distance <= 10)
  {
    Serial.println("distance is less than or equal to");
    myservo1.write(pos3);
  delay(100);
  myservo1.write(pos2);
  delay(100);
  myservo2.write(pos3);
  delay(100);
  myservo2.write(pos2);
  delay(100); 
  }
}

Why is the 9V battery bad and what should I use?

For reasons of running a motor, a 9V battery just does not have the current capacity, like a set of Li AA's could supply.

For batteries I like using 18650's and LiFePo4's.

For reliable wall warts, I use ones rated to run RPi's (Raspberry Pi's). They give the least amount of issue.

Those plastic geared servos have a stall current of around 800mA. I round that up to get, seeing 2 servos, you'll want at least 2 amps available and then more power will be needed to run the rest of the system.

The 9V battery, like the one pictured, should give you 500ma for an hour; brand new out of the box, maybe. In a few moments those servos could demand 1600ma (I know extreme case) and the battery only has the capacity to supply 500ma for one hour. You might be seeing why to not use the 9V?

Yeah I see why so do you think that could be the reason why my sensor isn't triggering my servos its underpowered?

A rough reply, hopefully red by several other new members, nothing peronal.
If had been a school teacher in physics I had thrown You out of the class room.
That battery is designed for long time use, several years, for very low current devices like remote controls, fire alarms, things that don't consume current, only need some voltage support.
There's not a nuclear reactor inside that housing. Compare with car batteries!
I thought most 10 year old children would know that. This we have to explain more or less every day. Come on!