Piezo music element to ping

Hi, I am a beginner programmer with abysmal knowledge on the correct terminologies of arduino so excuse me for my cheap terms. :sweat_smile:
I am currently working on a piezo music element which plays a melody when the ping sensor detects ‚Äėnothing‚Äô within its vicinity(If an object is within 60 inches, it turns off). I just pasted the piezo music code in to the ping sensor code and fortunately it works :stuck_out_tongue_closed_eyes:. But the piezo only makes a ‚Äėbeep, beep beep‚Äô sound continously without making any noticeable melody notes which wasn‚Äôt a surprise. This is the code(I think you can ignore the servo and led pins, they are extravaganza stuff)

#include <Servo.h>
Servo LFTleg;
Servo RGTleg;
Servo Tail;
Servo Frontpaw;

int pingPin = 7;
const int ledPin = 13;
int speakerPin = 3;

int length = 15;
char notes[] = "ccggaagffeeddc";
int beats [] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2);
digitalWrite(speakerPin, HIGH);
    digitalWrite(speakerPin, LOW);


void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1519, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
   for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
  // play the tone corresponding to the note name

void setup()
  pinMode (ledPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);

void loop()
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  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("in, ");
  if (inches <= 60) {
    digitalWrite (speakerPin, LOW);
    digitalWrite (ledPin, HIGH);
  else {
    digitalWrite (speakerPin, HIGH);
    for (int i = 0; i < length; i++) 
      playNote(notes[i], beats[i] * tempo);
    delay(tempo / 2); 
    digitalWrite (ledPin, LOW);


long microsecondsToInches(long microseconds)
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 73.746 / 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;

Any suggestions from an experienced programmer? And can you tell me how and why the code is malfunctioning?
I want to know for further projects, thanks! :slight_smile:

There are two types of piezo one that is le a speaker and one that makes a sound when you simply apply a voltage.
Connect it directly to 5V if it makes a noise then you have the wrong type.

I tested the piezo element and it played the star wars theme just fine :drooling_face:
I think there's a problem with my code but I can't specifically point out what's wrong cuz i am a novice :fearful:
any more suggestions?


    digitalWrite (speakerPin, HIGH);

for (int i = 0; i < length; i++)
     playNote(notes[i], beats[i] * tempo);
   delay(tempo / 2);
   digitalWrite (ledPin, LOW);

What is that part supposed to do?