I am writing a small program that plays a song on a buzzer. I am creating a function that expects a two dimensional array. The array can contain unlimited rows and two coloumns. The first column contains a previously defined constant, which is the frequency of the note and the second is the length.
I googled how to pass an array to a function and I think I am doing as I read but for some reason the function does nothing. As I figured out adding some Serial.print() calls, I found out that the function sees a 0 length array.
//Definitons of frequencies and pinout and tempo
...
unsigned int mySong[][2]{
{G2, 2},
{B2, 1},
{D3, 1},
{G3, 2}
};
void playSong(unsigned int song[][2])
{
Serial.println(sizeof(song)/sizeof(song[0]));
for(int i = 0; i<sizeof(song)/sizeof(song[0]); i++)
{
tone(buzzerPin, song[i][0], (song[i][1]*tempo));
Serial.println(song[i][0]);Serial.println(song[i][1]);
delay(song[i][1]*tempo);
}
}
void setup() {
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
playSong(mySong);
}
But if I change every 'song' to 'mySong', it works fine and plays the music and the readings in the serial monitor are correct, so it suggests that the way I found out should be good, and the array and constants should also work fine, so the data gets lost when I am passing it to the function.
What may I be doing wrong?
Thanks in advance for any help.