Any advice on how to repeat the song 6 times instead of infinitely? //Here is my code so far
int speakerPin = 9;

int length = 28; 

char notes[] = &quot;GGAGcB GGAGdc GGxecBA yyecdc&quot;;

int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1, 2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };

int tempo = 150;

void playTone(int tone, int duration) {

for (long i = 0; i &lt; duration * 1000L; i += tone * 2) {

   digitalWrite(speakerPin, HIGH);

   delayMicroseconds(tone);

   digitalWrite(speakerPin, LOW);

   delayMicroseconds(tone);

}

}

void playNote(char note, int duration) {

char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',           

                 'c', 'd', 'e', 'f', 'g', 'a', 'b',

                 'x', 'y' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,

                 956,  834,  765,  593,  468,  346,  224,

                 655 , 715 };

int SPEE = 5;


for (int i = 0; i &lt; 17; i++) {

   if (names[i] == note) {
    int newduration = duration/SPEE;
     playTone(tones[i], newduration);

   }

}

}

void setup() {

pinMode(speakerPin, OUTPUT);

}

void loop() {

for (int i = 0; i &lt; length; i++) {

   if (notes[i] == ' ') {

     delay(beats[i] * tempo);

   } else {

     playNote(notes[i], beats[i] * tempo);

   }


   delay(tempo);

}

}
Assuming, the program can stop after playing 6 times, put another for loop in the loop function:
void loop() 
{
   for (int nTimes = 0; nTimes &lt; 6; nTimes++)
   {
      // Existing loop code
   } 

   while(true) {};
}

A less crude way is to create a global variable:
int _timesPlayed = 0;

And change the for loop:
void loop() 
{
   if (_timesPlayed &lt; 6)
   {
      // Existing loop code

     _timesPlayed++;
   }
}