random note generator - counting gremlins!

I'm having trouble with a music note generator program. It's in the early stages at the minute, I've just been sending messages to the serial monitor to check it works, before I write the rest of the program. I've used a method called GeneratePhrase() which has a WHILE loop inside. The while loop checks the length of the phrase (a random number generated at the start of the GeneratePhrase() method) and increments the NoteCounter whilst the NoteCounter is < length.

My problem is - the number of notes generated does not match the length and the NoteCounter does not seem to increment properly?

If some one could check my code I'd really appreciate it. When you run the program, start the serial monitor and it should print out the generated musical phrases as they occur. For my program to work correctly, I need the note number (which appears in the serial monitor) to start at 0 and count up to the current phrase length (int length = PhraseLength[random(0,6)])

Thanks in advance

Jim.

int PhraseLength[] = {2,4,8,16,32,64};   // set of musical phrase lengths in number of beats/notes
int NoteLength[] = {250,500,750,1000,2500,5000};   // set of note lengths in milliseconds
int NoteGap[] = {250,375,500,625,750,875,1000};   // set of gap lengths between notes in milliseconds
int notes[] = {};   // storage for note values
int lengths[] = {};   // storage for note lengths
int gaps[] = {};   // storage for gap between notes

int length = PhraseLength[random(0,6)];   // phrase length (current state)
int goldenProb = 0;   // 2:3 (golden ratio) probability of phrase repeating 
int NoteCounter = 0;   // used to count through notes during phrase generation
  
  void setup()  
  {                
  Serial.begin(9600);   
  Serial.println("Serial Connection Established");
  }
  
      void loop()
      {
      Serial.println();
      Serial.println("Main Loop......");
      Serial.println();
      
      delay(2000);
      
      GeneratePhrase();   // firstly start by genrating a musical phrase
      }
    
  int GeneratePhrase()   // musical phrase generator
  { 
      if (goldenProb < 1)   // 1 in 3 chance of generating a new phrase  
      {
      Serial.println("Phrase Generating!");
      length = (PhraseLength[random(0,6)]);   // generate new phrase length
      Serial.print("Length of phrase = ");
      Serial.print(length);
      Serial.println();
      
      NoteCounter = 0;   // reset note counter
      
          while (NoteCounter < length)
          { 
          notes[NoteCounter] = random(0,6);  
          lengths[NoteCounter] = random(0,6);
          gaps[NoteCounter] = random(0,7);
          
          Serial.print("Note number: ");
          Serial.print(NoteCounter);
          Serial.print(" stored,");
          Serial.print(" Note: ");
          Serial.print(notes[NoteCounter]);
          Serial.print(", ");
          Serial.print(" Length: ");
          Serial.print(lengths[NoteCounter]);
          Serial.print(" ms, ");
          Serial.print(" Gap after note: ");
          Serial.print(gaps[NoteCounter]);
          Serial.print(" ms. ");
          Serial.println();
          NoteCounter ++;   // increment note counter
          }
      }
      
      else   // 2 in 3 chance of phrase remaining the same
      {
      Serial.println("Phrase Repeated");
      }
      
  goldenProb = random(0,3);   // generate new probability to be tested at next phrase generation
  }
int notes[] = {};   // storage for note values
int lengths[] = {};   // storage for note lengths
int gaps[] = {};   // storage for gap between notes

These arrays are sized to hold the values in the initialization string. Since there are 0 values in the initialization strings, the arrays are pretty small.

You then proceed to try to store up to 64 values in these arrays.

When you write beyond the end of an array, it overwrites something else.

Statically size these array, to hold 64 values, and life will be better.

At least, that worked for me.

Thanks muchly, I didn't realize the arrays did that, now I do!