Buzzer/Piezo Tone

Please i need some help to make this code run only with a push button , any one could help me ?

const int buzzerPin = 13;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 113;
void setup()
{
  pinMode(buzzerPin, OUTPUT);
}
void loop()
{
  int i, duration;
  for (i = 0; i < songLength; i++)
  {
    duration = beats[i] * tempo;
    if (notes[i] == ' ')
    {
      delay(duration);
    }
    else
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration)
    }
    delay(tempo/10);
  }
  while(true){}
}
int frequency(char note)
{
  int i;
  const int numNotes = 8;
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  for (i = 0; i < numNotes; i++)
  {
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }
  return(0);
}

I won't try to re-write your code but the "secret" is an [u]If-Statement[/u] (or other conditional branching/execution). The [u]Button Example[/u] turns-on the LED if the button is pushed.

while(true){}

That's a "trap". It's an infinite do-nothing loop that you can't break-out of (until a hardware reset). It's the kind of thing (usually a bug) that will make you think the Arduino has crashed or frozen-up, but really, it's just running a do-nothing loop like crazy.

You could use a do-nothing while() loop that loops continuously until a button is pressed. :wink:

I've deleted your other cross-post @Abdelrahman_Taher.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.