Self Driving Car

I am having trouble making a self driving car with two servos as wheels and one servo moving a ping sensor.

1.) The ping sensor servo just spazzes out and vibrates

2.) The continous servos move at different speeds (they are calibrated).

Here is my code

//Neutral is going to be 90

#include <Servo.h>
Servo myservo;
Servo left;
Servo right;

#define trigPin 4
#define echoPin 3

void setup(){
  myservo.attach(9);
  left.attach(10);
  right.attach(5);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  left.write(180);
  right.write(180);
  Serial.begin(9600);
}

void loop(){
  
  int safeDistance = 100;
  
  myservo.write(90); 
  long duration, distanceFront;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceFront = (duration/2) / 29.1;
  
  Serial.println(distanceFront);

  
if(distanceFront < safeDistance){
    
   left.write(90);
   right.write(90);
    
   myservo.write(0); //Looking Left
  long duration, distanceLeft;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceLeft = (duration/2) / 29.1;
   delay(2000);

   
   myservo.write(180); //Looking Right
  long distanceRight;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distanceRight = (duration/2) / 29.1;
   delay(2000);

  myservo.write(90);
  delay(500);
   
   if(distanceLeft > distanceRight){
    //insert command here to turn Left 
    left.write(0);
    right.write(180);
    delay(1000);
    left.write(180);
   }
   
   if(distanceRight > distanceLeft){
     //insert command here to turn Right)
     right.write(0);
     left.write(180);
     delay(1000);
     right.write(180);
   }
  }
}

:

By the way, the arduino could not supply the power, so I used an external 9v and voltage regulator. I connected all the grounds, so the only thing connected to the arduino is the ping sensor, all servos's signal, and common ground connection.

Hi,

Can you please post a copy of your sketch, using code tags. Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Thanks ..Tom....... :)

Most likely the servo power supply is inadequate. When starting or under load, servos require 1 ampere or more each, so your power supply should be capable of supplying 5-6 V at 2-3 amperes.

Why did you copy and paste the code for reading the sensor instead of using your head and creating a function?

electricviolin: The ping sensor servo just spazzes

Mind you don't go too technical.....

jremington: Most likely the servo power supply is inadequate. When starting or under load, servos require 1 ampere or more each, so your power supply should be capable of supplying 5-6 V at 2-3 amperes.

electricviolin: so I used an external 9v and voltage regulator

That a PP3 style so-called "transistor battery"? Maybe it can't handle the load. You don't say what you voltage reg is, but if it's a linear one like a 7805 or 7806 don't forget that you have the output current on the input side so you might be expecting too much from a PP3.

(But then, you didn't say what the power source and voltage reg are: you should do that ;) )