First time programming anything here.

Hi guys,

This is my first post. Im just getting into the arduino world and decided to try to build a robot. I am using an arduino uno, a ping ultrasonic sensor, and two springrc continuous rotation servos for my wheels.

I have used a program to control the servos successfully. I have used a program to use the ping successfully. The problem develops when I integrate the two. When the servos and ping are both in the program, the ping stops reading and shows 0's in the serial monitor. If i disconnect the servos' signal wires from the arduino then the ping works again fine, but as soon as one is plugged in, it no longer functions properly. I am unsure if the problem is something electrical or a problem with my program, so I am turning to you guys for help! Here is my code, its not done but it should be enough to identify a problem in the code if one exists

const int pingPin = 11;
const int servoR = 9;
const int servoL = 10;

long duration, inches, cm;




void setup()
{
   // initialize serial communication:
  Serial.begin(9600);
  pinMode (servoR,OUTPUT);
  pinMode (servoL,OUTPUT);
}


void go() {
  if (inches > 2){
  analogWrite(servoR, 170);
  analogWrite(servoL, 170);
  }
  else
  {
  
    // BLAH BLAH havent worked out the turning routine yet but it will go here
    
    
  }
}

void ping() {
  {
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(1000);
  }
}


void loop() {
  ping();
  go();
  
}

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


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

How are you powering all this?

analogWrite(servoR, 170);

Don't do this - use the servo library.

originally I used a 9v batter to power the arduino. The 9 volts also went through a +5v low dropout voltage regulator. the +5v was used to power the servos. I ran into a problem when the servos wouldnt respond to the arduino signal. I realized that to properly work it seemed that to read the arduino signal properly the servo needed to be grounded to the on board ground, and not the ground in the voltage regulator circuit. So as it currently stands, the servo and ping are both being powered by the arduino boards onboard supply. How can I use an alternate power supply for the servos when they have different commons?

I will give the servo library a shot tomorrow, its gettin late and my head hurts from trying to pick up on all this just today haha. I will give it a shot and let you know how it goes. I feel it is unlikely this will solve my problem though.

originally I used a 9v batter to power the arduino.

A common PPs 9V battery simply doesn't have enough poke to drive two servos, and you shouldn't be driving the servos off the Arduino's regulator.
Four AAs with a common ground will do a much better job.
I normally budget at least 500mA per servo, more if the servo is heavily loaded or making a lot of rapid movements.

I am new to arduino but I'm planning to build a small cart also. I recently purchased a number of Spring RC servos, including two of the SM-S4315R high torque continuous rotation servos. I calibrated each of the servos manually using the writeMicroseconds command from the servo library.

Plug random numbers between ~540 to ~2400 into writeMicroseconds() and then count the number of revolutions in 60 seconds for each number you plug in. If you graph this data you will see that you can correlate writeMicroseconds() values to specific values of RPM.

#include <Servo.h> 
 
Servo myServo;  // create servo object to control a servo 
 
void setup() 
{ 
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
} 
 
 
void loop() 
{ 
  myServo.writeMicroseconds(1288);
}

Note that if you change the voltage to the servos, this may change the curve slightly.

At ~5.3 V, here is the curve I got for my two servos:

Notice from the graph that there is a 'dead band' in the center where I will get 0 RPM over a range of writeMicroseconds() values. I will probably want to write software that collapses this zone. Otherwise, I could end up writing a program that expects the cart to move very slowly, yet the cart doesn't move at all.

Also notice in the graph that there is a range in which the Microseconds and the RPM are pretty much linear, but at high and low RPMs the graph starts to curve and levels off at the max/min RPM.

Have you done the same graph for a Futaba 3003?

originally I used a 9v batter to power the arduino. The 9 volts also went through a +5v low dropout voltage regulator. the +5v was used to power the servos. I ran into a problem when the servos wouldnt respond to the arduino signal. I realized that to properly work it seemed that to read the arduino signal properly the servo needed to be grounded to the on board ground, and not the ground in the voltage regulator circuit. So as it currently stands, the servo and ping are both being powered by the arduino boards onboard supply. How can I use an alternate power supply for the servos when they have different commons?

All the grounds must be in common. Running the servo's from the on-board regulator is a bad idea, a separate regulator is a much better proposition.

Also you main problem is that pulseIn is 'blocking' - that is it doesn't return until it sees a whole pulse go by - none of the rest of you code can run during this wait.