Parallax ping sensor

Hi!
My problem is that the ping sensor doesn´t sending signals, but there is voltage on it.
For some time it worked properly, but after i changed the code (just added a part to it), it doesn´t work at all and even the ACT LED isn´t blinking.
Could you help me pls?
thanks
the code:

#include "hangok.h"
#include <Servo.h>
int melody[] = {
  NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3, NOTE_F2, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3,NOTE_A3, NOTE_G3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3,NOTE_G2, NOTE_F2 
};
int noteDurations[] = {
  4, 4,1,2,4,1,4, 4,1,2,4,2,1,4, 4,1,2,4,1,1  };
const int motor1Pin1 = 4;
const int motor1Pin2 = 5;
const int motor2Pin1 = 6;
const int motor2Pin2 = 7;
int leftDistance, rightDistance;
const int pingPin = 8;
Servo pingServo;
long duration,cm;
void setup()
{
  motorstop();
  delay(500);
  for (int thisNote = 0; thisNote < 20; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(12, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
  Serial.begin(9600);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pingServo.attach(9);
  pingServo.write(90);
}
long ping()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = duration / 29 / 2;
}
int motorgo()
{
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  analogWrite(motor2Pin1, 255);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor1Pin1, LOW);
  analogWrite(motor2Pin1, 255);
}
int motorstop()
{
  analogWrite(motor1Pin2, 0);
  analogWrite(motor2Pin1, 0);
}
int turnLeft()
{
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  analogWrite(motor2Pin1, 200); 
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor1Pin1, HIGH);
  analogWrite(motor1Pin2, 200);
  delay(500);


}
int turnRight()
{
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  analogWrite(motor1Pin2, 200); 
  digitalWrite(motor2Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  analogWrite(motor2Pin1, 200);
  delay(500);

}
void loop()
{ 
  ping();
  if (cm>=50){
    motorgo();
    delay(250);
  } 
  else
  { 
    motorstop();
    pingServo.write(140); 
    delay(200);
    rightDistance = ping(); //scan to the right
    delay(200);
    pingServo.write(40);
    delay(350);
    leftDistance = ping(); //scan to the left
    delay(200);
    pingServo.write(90); //return to center
    delay(80);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    turnLeft();
  }
  else { 
    turnRight(); //if right is less obstructed
  }
}

What parts did you add to the code? Did you add hardware as well? Please provide the old working code for comparison. We need a lot more detail about your hardware and your circuit.

Oh, ok. Originally that was a tanks base with 2 DC motors, there was a servo and the ping sensor on it.
I added an 8 ohm 0.5 W speaker as hardware and for the code to play a song.
The old code:

#include <Servo.h>
const int motor1Pin1 = 4;
const int motor1Pin2 = 5;
const int motor2Pin1 = 6;
const int motor2Pin2 = 7;
int leftDistance, rightDistance;
const int pingPin = 8;
Servo pingServo;
long duration,cm;

void setup()
{
  Serial.begin(9600);
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pingServo.attach(9);
  pingServo.write(90);
}
long ping()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = duration / 29 / 2;
}
int motorgo()
{
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  analogWrite(motor2Pin1, 255);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor1Pin1, LOW);
  analogWrite(motor2Pin1, 255);
}
int motorstop()
{
  analogWrite(motor1Pin2, 0);
  analogWrite(motor2Pin1, 0);
}
int turnLeft()
{digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  analogWrite(motor2Pin1, 200); 
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor1Pin1, HIGH);
  analogWrite(motor1Pin2, 200);
  delay(500);
 
 
}
int turnRight()
{digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  analogWrite(motor1Pin2, 200); 
  digitalWrite(motor2Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  analogWrite(motor2Pin1, 200);
  delay(500);
  
}
void loop()
{ 
  ping();
  if (cm>=50){
    motorgo();
    delay(500);
  } 
  else
  { 
    motorstop();
    pingServo.write(140); 
    delay(200);
    rightDistance = ping(); //scan to the right
    delay(200);
    pingServo.write(40);
    delay(350);
    leftDistance = ping(); //scan to the left
    delay(200);
    pingServo.write(90); //return to center
    delay(80);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    turnLeft();
  }
  else { 
    turnRight(); //if right is less obstructed
  }
}

What are you using for power? Voltage regulator? Motors? We need a lot more detail.

Also make sure your connections are secure.

Maybe provide a detailed photo.

Does the rest of the circuit work as expected other than the ping sensor?

fba94:
Oh, ok. Originally that was a tanks base with 2 DC motors, there was a servo and the ping sensor on it.
I added an 8 ohm 0.5 W speaker as hardware and for the code to play a song.

Did you just connect the speaker straight to an i/o pin? Is that the pin giving trouble now? Turning that i/o pin on and leaving it on for more than a few hundred mS could damage the pin since it will see the speaker basically as a short circuit.

For power i´m using a 8.4 V 4600mAh Ni-MH accu, i have an AND-TECH motor shield. The rest of the circuit works fine, all what now the whole thing does is plays the music then the servo turns to right then to left and the body turns to right. Same if i disconnect the signal wire from arduino.
And the speaker plays the music fine, i´m also used the same pin always for the Ping.

I still think you should add a capacitor or some resistance inline (at least 200R) with the speaker just in case things freeze up and leave the output pin on causing it to burn out. Better safe than sorry IMO.

You have ping() defined as returning a long, yet it returns nothing. You also might want to do a few more low/high toggles of the sender to get it moving.

You might consider putting a 500R resistor across the terminals of the sensor if you don't have one already. Seems counterintuitive I know, but the transducer doesn't pass DC so the resistor prevents DC voltages from building up on the pin. Just repeating what I've seen and heard.

Hi! I´ve tried and disconnected everything from the arduino panel then connected only the sensor and a motor with motor shield. To the Ping example program i put a part, that the motor should go or not.
I connected to the computer and the chip on the Ping sensor started to heat heavily :frowning: Even without the signal cable it´s the same.

what voltage are you measuring to the ping sensor's +5V?

Yeah, +5 V.