Ultrasonic sensor not not working with servos

Hi, I'm having trouble with my sensor triggering my servos so they can move to a different position What do I need to do with my code to get this to work?

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

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





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

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

}

void loop ()
{
  
int duration, distance;

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);
  myservo2.write(pos1);
  
  delay(400);

  myservo1.write(pos1 - pos2);
  
  myservo2.write(pos1 + pos2);
  delay(400);
  myservo1.write(pos1 - pos2);
  
  myservo2.write(pos1 + pos2);
  delay(400);
  if (distance < 10) {
myservo1.write(pos1);
  myservo2.write(pos1);
   delay(400); 
    
    
myservo1.write(pos1 - pos3);
  
  myservo2.write(pos1 + pos3);
  delay(400);
  }
  }
                                                
         
  
  
  

What does your code do now?

1 Like

Hi!

There are some logic problems within your instructions. Maybe it would be more fruitful if you tell us what do you expect the code to do (EDIT: @Paul_KD7HB asked first...)

For example:

 myservo1.write(pos1 - pos2);
 myservo2.write(pos1 + pos2);
 delay(400);
 myservo1.write(pos1 - pos2);
 myservo2.write(pos1 + pos2);

This is uneffective, since you´re writing the same position the servos were already in.

myservo1.write(pos1 - pos3); // This is returning a negative angle
myservo2.write(pos1 + pos3); //This is returning an angle that is beyond 180°

This doesn´t appear to be correct...

Before anything else, however, I can tell you that HC-SR04 (if this is your us sensor) can behave strangely in too close distances and it will return zero (so, < 10) whenever the object is out of range.

1 Like

Without your hardware set up it is hard to tell what it is supposed to do and what is not not working.

What does the Serial output look like?

I'd add a Serial.print("<10"); inside the conditional to report if that section gets triggered as expected.

pos1 - pos3 = -10 and pos1+pos3 = 190. Do those work with your servos?

Not that it makes any functional difference, but your code will be easier for others (and you maybe) to follow if you use meaningful names like servoLeftLeg rather than myservo1 and legUp (or whatever it is) rather than pos1.

I also try to remember to put comments after closing braces so it's easy to see at a glance where sections end, like } //end of loop.

I'm trying to make an obstacle avoiding robot and pos1 - pos2 makes the robot walk forward and pos1 - pos3 should make it go backward. The robot goes forward great it just won't go backward. Should I try

myservo1.write(pos1);
myservo1.write(pos2);
myservo2.write(pos1);
myservo2.write(pos2);

No. The code you wrote will run very quickly, and result both servos being in position 2

Great so what should I do about my sensor not triggering my servos to go backward?

You could add some more debug prints to your code to get it to tell you what it is doing.

Cool how do I do that and how does it work?

First of all, if you could determine the upper and lower limits of your servos, that would be useful to know. The suggestion in post #9 is a useful tool that will help. P.S. there's lots of servo examples for reference.

How do I find that out?

post #11 was edited (slow typing) ...

Are your servos standard 0-180 servos? Or continuous rotation servos?

1 Like

0 - 180 micro servos

is a size.

You were asked for the operationmode.
does this mean that the servo horn move 180 degrees clockwise and then back 180 degrees counter-clockwise?

As a general advice: how many postings do you want to wait for the final solution?
As more details, datasheets, pictures of your project you are posting the less
"asking back for this and that details"-posting are nescessary.

best regards Stefan

1 Like

I answered the question I said 0 - 180 degrees i just forgot to verify.

Those are debug prints.
They tell you something about how your code is behaving.

To get a bigger, better picture, add more.

(and pulseIn returns "unsigned long" (aka "uint32_t"), not "int)

1 Like

How is "pos1-pos3" supposed to make it go backward?

yeah 90 - 80 goes forward for me and 90 - 100 should go backward