Stuck in for loop within setup()

Hi everyone - I'm working on a word generator using the Nokia 5510 LCD. For the title screen when you first turn it on, I have title text display within setup(). I'm now trying to incorporate a short 12 note song (There's a place in France where the... ahem) during the title screen, but the for loop that cycles through the notes and durations get's stuck. This results in a stuck low note (not a programmed one, sounds like the lowest frequency possible) from the piezo and the remaining title text and code never completes, and of course, I never make it into loop().

I'm thinking this is a noob grade question, but I'm stuck, just like my code...

Anyway, I'm stripping out the PROGMEM data and table because the data is, um, X-Rated. If you need the full code to debug, I can pm it. This just includes the notes, durations, setup(), and loop():

// Streets of Cairo aka There's a place in France
int melody[] = {
  NOTE_E4, NOTE_FS4,NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_B4, NOTE_FS4, NOTE_G4, NOTE_E4,0};

// Streets of Cairo durations
int noteDurations[] = {
  8,8,4,4,4,8,8,8,8,8,8,8,4,8 };

void setup(void)
{
  Serial.begin(9600);
  BetterRandomSeed();
  LcdInitialise();
  LcdClear();
  pinMode(button, INPUT);
  pinMode(statusLED, OUTPUT);
  pinMode(lcdLED, OUTPUT);
  
  // Turn on power LED
  digitalWrite(statusLED, HIGH);
  
  //Title screen
  // turn on the backlight for title
  digitalWrite(lcdLED, HIGH);
  cairo();
  gotoXY(8, 2);
  LcdString("DIRTY WORD  GENERATOR");
  
  delay(1500);
  LcdClear();
  gotoXY(8, 2);
  LcdString("  JT           2010");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("It's the     DWG, yo!");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("Let's get    dirty!");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("Press the    button");
  delay(2000);
  
  // turn off the backlight until button is pressed
  digitalWrite(lcdLED, LOW);
}

void loop(void)
{
  randomSeed(analogRead(0));
  buttonState = digitalRead(button);
  if(buttonState == HIGH)  {
    digitalWrite(lcdLED, HIGH);
    LcdClear();
    gotoXY(2,2);
    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[random(num_strings)])));
    LcdString (buffer);
    Serial.print(buffer);
    delay(1500);
    digitalWrite(lcdLED, LOW);
  } 
  delay(30);   
}

I'm now trying to incorporate a short 12 note song (There's a place in France where the... ahem) during the title screen, but the for loop that cycles through the notes and durations get's stuck. This results in a stuck low note (not a programmed one, sounds like the lowest frequency possible) from the piezo and the remaining title text and code never completes, and of course, I never make it into loop().

I'm thinking this is a noob grade question, but I'm stuck, just like my code...

The question is fine. The post is not. You say that you have a problem in a for loop, but the posted code contains no for loops.

We can't help you with the code you didn't post.

My bad, right you are. Forgot I moved the for loop into it’s own function while trying to solve my problem:

// Streets of Cairo aka There's a place in France
int melody[] = {
  NOTE_E4, NOTE_FS4,NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_B4, NOTE_FS4, NOTE_G4, NOTE_E4,0};

// Streets of Cairo durations
int noteDurations[] = {
  8,8,4,4,4,8,8,8,8,8,8,8,4,8 };

void setup(void)
{
  Serial.begin(9600);
  LcdInitialise();
  LcdClear();
  pinMode(button, INPUT);
  pinMode(statusLED, OUTPUT);
  pinMode(lcdLED, OUTPUT);
  
  // Turn on power LED
  digitalWrite(statusLED, HIGH);
  
  //Title screen
  // turn on the backlight for title
  digitalWrite(lcdLED, HIGH);
  gotoXY(8, 2);
  LcdString("DIRTY WORD  GENERATOR");
  // iterate over the notes for Streets of Cairo:
  for (int thisNote = 0; thisNote < 15; thisNote++) {
      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/noteDurations[thisNote];
      tone(10, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:
      noTone(10);
    }
  delay(1500);
  LcdClear();
  gotoXY(8, 2);
  LcdString("  JT           2010");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("It's the     DWG, yo!");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("Let's get    dirty!");
  delay(2000);
  LcdClear();
  gotoXY(8, 2);
  LcdString("Press the    button");
  delay(2000);
  
  // turn off the backlight until button is pressed
  digitalWrite(lcdLED, LOW);
}

void loop(void)
{
  randomSeed(analogRead(0));
  buttonState = digitalRead(button);
  if(buttonState == HIGH)  {
    digitalWrite(lcdLED, HIGH);
    LcdClear();
    gotoXY(2,2);
    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[random(num_strings)])));
    LcdString (buffer);
    Serial.print(buffer);
    delay(1500);
    digitalWrite(lcdLED, LOW);
  } 
  delay(30);   
}
int melody[] = {
  NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_FS4,
  NOTE_E4, NOTE_E4,  NOTE_FS4, NOTE_G4,
  NOTE_B4, NOTE_FS4, NOTE_G4, NOTE_E4,0};

There are 13 values in the note array, with the last one being 0.

int noteDurations[] = {
  8,8,4,4,
  4,8,8,8,
  8,8,8,8,
  4,8 };

There are 14 values in the noteDurations array.

  for (int thisNote = 0; thisNote < 15; thisNote++)

So, thisNote will take on values from 0 to 14.

What note will be played when thisNote is 13? For how long?
What note will be played when thisNote is 14? For how long?

Thank ya, thank ya, PaulS. Simple error on my part. I've been staring at this code so long that I forgot to, you know, add stuff up. :P

Changed the array back to 13 values, brought my durations back to 13, and the for loop now loops from 0 to 12. And now the rest of setup() runs. Tip of the hat.

The use of structures could’ve been used to group both the frequency and duration components together making the mis-match a little more obvious.

Something along the following:

// <http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292630502>

// Streets of Cairo aka There's a place in France

struct note_t
{
    int freq, length;
};

note_t  melody[] =
{
      NOTE_E4,  8
    , NOTE_FS4, 8
    , NOTE_G4,  4
    , NOTE_FS4, 4
    , NOTE_E4,  4
    , NOTE_E4,  8
    , NOTE_FS4, 8
    , NOTE_G4,  8
    , NOTE_B4,  8
    , NOTE_FS4, 8
    , NOTE_G4,  8
    , NOTE_E4,  8
    , 0,        4
};

const int   kMELODY_LENGTH = (sizeof(melody) / sizeof(melody[0]));

void loop()
{
    randomSeed(analogRead(0));
    buttonState = digitalRead(button);
    if ( buttonState == HIGH)
    {
        digitalWrite(lcdLED, HIGH);

        strcpy_P(buffer, (char*)pgm_read_word(&(string_table[random(num_strings)])));
        LcdClear();     gotoXY(2,2);    LcdString (buffer);

        Serial.print(buffer);   delay(1500);

        digitalWrite(lcdLED, LOW);
    }

    delay(30); 
}


void setup()
{
    Serial.begin(9600);

    LcdInitialise();
    LcdClear();

    pinMode(button, INPUT);
    pinMode(statusLED, OUTPUT);
    pinMode(lcdLED, OUTPUT);
    
    // Turn on power LED
    digitalWrite(statusLED, HIGH);
    
    // Title screen
    // turn on the backlight for title
    digitalWrite(lcdLED, HIGH);

    gotoXY(8, 2);   LcdString("DIRTY WORD GENERATOR");


    // Iterate over the notes for Streets of Cairo:

    for (int thisNote = 0; thisNote < kMELODY_LENGTH; thisNote++)
    {
        // To calculate the note duration, take one second divided by the note
        // type.
        // e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

        int     noteDuration        = 1000 / melody[thisNote].length;
        tone(10, melody[thisNote].freq, noteDuration);
        

        // To distinguish the notes, set a minimum time between them. The note's
        // duration + 30% seems to work well:

        int     pauseBetweenNotes   = noteDuration * 1.30;
        delay(pauseBetweenNotes);


        // stop the tone playing

        noTone(10);
    }

    delay(1500);

    LcdClear(); gotoXY(8, 2);   LcdString(" JT      2010");         delay(2000);
    LcdClear(); gotoXY(8, 2);   LcdString("It's the   DWG, yo!");   delay(2000);
    LcdClear(); gotoXY(8, 2);   LcdString("Let's get  dirty!");     delay(2000);
    LcdClear(); gotoXY(8, 2);   LcdString("Press the  button");     delay(2000);
    

    // turn off the backlight until button is pressed

    digitalWrite(lcdLED, LOW);
}