erratic servo movement

My servos are moving in very weird ways, I’m building a robot that rolls forward with leftServo and rightServo and when it is 10 inches away from an object, the myservo, which has the Ping sitting on top turns right and left and determines which length is the furthest from it, it then turns that direction and moves that way. There are couple problems with these continuous rotation servos:

  1. In the main loop, when the Ping doesn’t see anything, they move one way for no certain amount of seconds an then move the other way. They also go at various speeds.
    2)When they are supposed to stop when the Ping sees an object, they don’t. This is very frustrating.

I slowed the loop way down to see if the drive servos were even listening to their commands and they still went at full speed. They should almost not be moving at the speed I have them going at. If anyone sees anything wrong with my code, please say something!.

PS. The servos ground and power are all looped together if that has anything to do with it.
Thanks!

#include <MegaServo.h>
#define NBR_SERVOS 3
#define FIRST_SERVO_PIN 8
MegaServo myservo;
MegaServo leftServo;
MegaServo rightServo;
int pos = 90;
int pos1 = 0;
int pos2 = 0;
int pingPin = 7;
const int ledPin = 13;
int rightDist;
int leftDist;

int launchPing()
{  
    pinMode(pingPin, OUTPUT);   //|||
    digitalWrite(pingPin, LOW); //|||
    delayMicroseconds(2);       //giving short burst telling Ping to Send distance
    digitalWrite(pingPin, HIGH);//|||
    delayMicroseconds(5);       //|||
    digitalWrite(pingPin, LOW); //|||
    pinMode(pingPin, INPUT);    // make Ping an input
    return pulseIn(pingPin,HIGH);
}

void setup()
{
  Serial.begin(9600);
  pinMode (ledPin, OUTPUT);
  myservo.attach(9);
  leftServo.attach(8);
  rightServo.attach(10);
  leftServo.write(0);
  rightServo.write(0);
  delay(1000);
}

void loop()
{
  pos1+=1;
  pos2-=1;
  leftServo.write(pos1);
  rightServo.write(pos2);
  long duration, inches, cm;
  duration = launchPing();
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
   if (inches >= 10) {
    digitalWrite (ledPin, LOW);
    pos=94;
    myservo.write(pos);
  }
  else {
    digitalWrite (ledPin, HIGH);
    delay(250);
    
    //start of left scan
    pos = 180;
    myservo.write(pos);
    delay(1000);
    duration = launchPing();
    inches = microsecondsToInches(duration); //converting time into distance
    cm = microsecondsToCentimeters(duration);//converting time into distance
    rightDist = inches;
    Serial.print("right duration = ");//|||
    Serial.print(duration);//sends serial data to computer
    Serial.println();    //|||
    // end of leftt scan
    
    //beginning of right scan 
    pos = 15;
    myservo.write(pos);
    delay(1000);
    duration = launchPing();
    inches = microsecondsToInches(duration); //converting time into distance
    cm = microsecondsToCentimeters(duration);//converting time into distance
    leftDist = inches;
    Serial.print("left duration = ");//|||
    Serial.print(duration);//sends serial data to computer
    Serial.println();    //|||
    //end of right scan
    
    //beginning of mathematics to determine which direction gives robot 
    //the optimal distance to travel before another object
    if (leftDist < rightDist) {
      Serial.print("right side is longer");
      pos1-180;
      leftServo.write(pos1);
      pos=94;
      myservo.write(pos);
      delay(2000);//wait for servo
    }
    else {  
      Serial.print("left side is longer");
      pos2+180;
      rightServo.write(pos2);
      pos=94;
      myservo.write(pos);
      delay(2000);//wait for servo
  }

 }
 delay(1000); 
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74/ 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

It looks like you are writing 0 to the servos when you start and if they are continuous rotation servos they will be running at full speed.

I suggest you start off with a simple test sketch to get the servos under control. If you have a potentiometer you can modify the knob example sketch that comes with arduino so it uses the MegaServo library and drives your three servos. You should be able to adjust the pot to control the motor speed.
Once you have that working you can modify the code to drive the scan using the pot.

When that is working you could replace the pot with the ping code.

By building your application in stages it will be easier for you to get your code working as you want.

thanks!, will do

Erratic servo behavior can, very often, be attributed to power supply de-coupling. This is what happened with me when working with a single continuous rotation servo; i imagine your situation may be even worse with two.

Take a look at De-coupling this has a very thorough explanation of what needs to be done to ensure your servos act proerly via the use of capacitors.

I also agree with mem that you should decompose the problem into smaller parts and make sure that each one works on its own.

good luck!

pos1-180;
pos2+180;

That don't look right.
I mean, it'll compile, but it won't do anything useful.

pos1+=1;
pos2-=1;
leftServo.write(pos1);
rightServo.write(pos2);

If pos2 is initially zero, then it'll immediately go negative - is that valid?

Why are you calculating both inches and cm?