Trouble with Ping))) Sensor PLEASE HELP!

Hello,

I am building a robot that uses a Ping))) sensor as the “eyes” of the robot. The problem occurs after I upload the code to the arduino. It seems that the arduino is executing fine, but my Ping))) sensor has some strange behavior. Whenever the arduino starts to execute the code, the Ping))) sensor flashes on then off. It only collects data for the first half-second.

Here is my setup:
Two Continuous Rotation Servo Motors Connected to 5V, ground, and pins 9 and 6.
Ping))) Sensor connected to 5V(Same as servos), pin 7, and Ground.
-There are no resistors, capacitors, transistors, or any other electronic parts. Just motors and the Ping))) sensor.

If this also helps, here is my code:
Start of Code:

#include <Servo.h>
int SensorPin = 7;//ping pin
Servo left_wheel; //right and left
Servo right_wheel;
unsigned long pulseDuration=0;
int distance;
int state=0;

void setup() {
left_wheel.attach(9);
right_wheel.attach(6); //pin that servo is plugged into
}

void loop() {
if (state == 0) {
state=state+1;
} else {
state=state-1;
}
//The State stuff is to give the servos and the Ping))) sensor “turns”
if (state==0){
pinMode(SensorPin, OUTPUT); //sends Ping
digitalWrite(SensorPin, HIGH);
delayMicroseconds(5); //wait…
digitalWrite(SensorPin, LOW); //Sets up for receiving the ping

pinMode(SensorPin, INPUT); //More setup for receiving ping
pulseDuration=pulseIn(SensorPin, HIGH); //recieves ping
pulseDuration=pulseDuration/2;
distance = int(pulseDuration/29); //calculates distance in cm
} else {
if (distance < 13) {
while (distance < 13); {
left_wheel.write(0);
right_wheel.write(180); //turns the robot
pinMode(SensorPin, OUTPUT); //sends new Ping
digitalWrite(SensorPin, HIGH);
delayMicroseconds(5); //wait…
digitalWrite(SensorPin, LOW); //Sets up for receiving the ping

pinMode(SensorPin, INPUT); //More setup for receiving ping
pulseDuration=pulseIn(SensorPin, HIGH); //recieves ping
pulseDuration=pulseDuration/2;
distance = int(pulseDuration/29); //calculates distance in cm
delay(20);
}
} else {
left_wheel.write(180); //FORWARD!
right_wheel.write(180);
delay(20);
}
}
}

End of Code:

Is there anything I could correct in my setup or code so that the Ping))) sensor works again?

Thanks,

I_Got_Pi

If you are powering the servo from the arduino then you should try using direct power. Servos draw too much power from the arduino pins.

Thanks, I’ll try that.