Passed data to method is null

I’m facing some weird issues. I’m using a buzzer to play a tune on my arduino. My issue isn’t with the buzzer; rather, it’s an issue with my code. I have a method that will return to me the frequency. To use this method, I need to pass in a string. This is my issue. When passing the string, the string in the method becomes null/empty. Here’s an example:

getStuff("5");

void getStuff(String num) {
        ....
}

The string num would become null. Even though we passed in a valid string, num, for some strange reason, is null.

Here’s the weird thing about my issue. I only run into it when I have an array greater than 52. When I only have 52 notes or less, I won’t run into the issue. But once I add that 53’rd, then it’s an issue. And the weird thing is, the method doesn’t even use my array of notes. I pass in the note to the method(which is not null, I checked), but in the method, it is null/empty.

To be more clear, here is my code:

const int buzzerPin = 9;

String notes[] = {"C6", "C6", "G5", "G5", "F5", "F5", "D6", "D6", "C6", "C6", "G5", "G5", "F5", "F5", "D6", "D6", "E6", "E6", "D6", "D6", 
"G6", "G6", "F6", "G6", "F6", "F6", "E6", "E6", "D6", "D6", "C6", "C6", "B5", "B5",
"C6", "C6", "G5", "G5", "F5", "F5", "D6", "D6", "C6", "C6", "G5", "G5", "F5", "F5", "D6", "D6", "E6", "E6", "D6", "D6", "G6", "A6"
};
const int songLength = sizeof(notes) / sizeof(notes[0]);

float beats[] = {1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5,
1.5, 0.5, 0.25, 0.25, 1, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5,
1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5,
1.5, 1
};

String noteList[] = {"A1", "A2", "A3", "A4", "A5", "A6", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", 
                    "D3", "D4", "D5", "D6", "E1", "E2", "E3", "E4", "E5", "E6", "F1", "F2", "F3", "F4", "F5", "F6", "G1", "G2", "G3", "G4", "G5", "G6"};
int frequencies[] = {55, 110, 220, 440, 880, 1760, 62, 123, 247, 494, 988, 1976, 33, 65, 131, 262, 523, 1046, 37, 73, 147, 294, 587, 1175, 
                    41, 82, 165, 330, 659, 1318, 44, 87, 175, 349, 698, 1397, 49, 98, 196, 392, 784, 1568};
const int noteListLength = sizeof(noteList) / sizeof(noteList[0]); 

int tempo = 396;


void setup() 
{
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
}


void loop() 
{
  int i;
  float duration;
  
  for (i = 0; i < songLength; i++)
  {
    duration = beats[i] * tempo;
    
    if (notes[i] == " ")
    {
      Serial.println("Skip");
      delay(duration);
    }
    else   
    {
      Serial.print(notes[i]);
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);         
    }
    delay(tempo/10);
  }
  while(true){}
}

int frequency(String note) 
{ 
  int i;
  for (i = 0; i < noteListLength; i++) 
  {
    Serial.println(note);
    if (noteList[i] == note)         
    {
      return(frequencies[i]);
    }
  }
  Serial.println("Failed");
  return(0);
}

As you see, I log the note before I pass it in, and after it’s been passed in to the method. Before, it gives me the correct string. But after, it’s just null/empty.

I have no idea what is up with this. As you can see in my frequency method, I never refer to my notes array, only the list of predefined notes. What is happening, and can I fix it? Thanks.

That's a lot of arrays. What board are you using? My guess is that you're running out of memory.

You don’t have that many different elements in the notes[] array, so I would code those as bytes. The frequency() function would have to be modified to interpret the byte values that come in to the proper frequencies, but I don’t think that would be too hard. Next, I would change the beats[] array into byte values. Since you have a minimum value of .25 and a max value of 1.5, you could have a byte value of 1 correspond to .25, 2 = .50, 3 = .75, and so on. This reduces the memory by 3 bytes per note. You can adjust the math for tempo * beats for the new values. The same indexing scheme can be applied to the notelist[] array. I think you’d have enough memory after doing that.

To use this method, I need to pass in a string.

So, why are you passing it a String, instead. String and string are NOT interchangeable. Get rid of EVERY String in your code. You do NOT need Strings. You do need strings.