unsigned int* problem after passed to method

Hi,

I’m trying to convert some rtttl string (nokia ringtone) to an array of frequencies and duration, in order to play it async.

The conversion ends ok and when I print the array, it’s just ok. It’s in the form id: (value)

Converting rtttl to Frequencies
Longitud26
Frequencies array after conversion:
0:(622) 1:(659) 2:(0) 3:(740) 4:(784) 5:(0) 6:(622) 7:(659) 8:(0) 9:(740) 10:(784) 11:(0) 12:(1047) 13:(988) 14:(0) 15:(622) 16:(659) 17:(0) 18:(988) 19:(932) 20:(0) 21:(880) 22:(784) 23:(659) 24:(587) 25:(659)

The array is then passed to playMelody Method

void playMelody(unsigned int frequencies[], unsigned long durations[], unsigned long length, unsigned long loopCount) {
        stopTone();
        melodyFrequencies = frequencies;
        melodyDurations = durations;
..
..
..

melodyFrequencies and melodyDurations are global

unsigned int* melodyFrequencies;
unsigned long* melodyDurations;

Here is where things get weird, when I print the received array I get this:

Frequencies received by PlayMelody method
0:(622) 1:(659) 2:(0) 3:(740) 4:(784) 5:(0) 6:(622) 7:(2048) 8:(234) 9:(512) 10:(512) 11:(32262) 12:(51464) 13:(59400) 14:(47874) 15:(3840) 16:(63494) 17:(0) 18:(988) 19:(932) 20:(0) 21:(880) 22:(784) 23:(659) 24:(587) 25:(659)

The array is the same until id 7 and it goes back to normal in id 17. The modified values are not random they are consistent in every execution.

I’m stuck here, I have try everything but can figure it out.

Thanks

prueba_sonido_rttl2.ino (10.4 KB)

Why are you passing data to the function, and then pointing a global pointer to it? Point the global pointer to the array instead of passing the array to the function.

        debugl("Frequencies received by PlayMelody method");
        playMelodyTone(melodyIdx);

Why are you printing the heading in one place and the data in another? The data you are printing is NOT the data passed to the function.

Why does playMelodyTone() not play anything?

Which Arduino are you running this on? How much free memory do you have to copy large arrays in?

PaulS:
Why are you passing data to the function, and then pointing a global pointer to it? Point the global pointer to the array instead of passing the array to the function.

        debugl("Frequencies received by PlayMelody method");

playMelodyTone(melodyIdx);

Yes you’re right, I didin’t do this part of the code, its unnecesary to pass it to the function. Changed.

PaulS:
Why are you printing the heading in one place and the data in another? The data you are printing is NOT the data passed to the function.

Ok, changed. The data seems find when asigned to global array. The problem is in the playMelodyTone method?

PaulS:
Why does playMelodyTone() not play anything?

Its commented. Just for testing

//TimerFreeTone(BUZZER, melodyFrequencies[melodyIdx], melodyDurations[melodyIdx]);

PaulS:
Which Arduino are you running this on? How much free memory do you have to copy large arrays in?

Its Arduino nano, AtMega328. I’m using 4.332 bytes for program of 30.720 bytes. And 636 bytes of dynamic memory leaving 1.412 bytes for local variables. I know there is little space but is just 26 position array, no?

prueba_sonido_rttl2.ino (10.7 KB)

Hi, I added some memory debug with the MemoryFree.h library.

This is the output

Free memory just befor updateMFree memory: 1544
Converting rtttl to Frequencies
Length 26
Frequencies array after conversion:
0:(622) 1:(659) 2:(0) 3:(740) 4:(784) 5:(0) 6:(622) 7:(659) 8:(0) 9:(740) 10:(784) 11:(0) 12:(1047) 13:(988) 14:(0) 15:(622) 16:(659) 17:(0) 18:(988) 19:(932) 20:(0) 21:(880) 22:(784) 23:(659) 24:(587) 25:(659) 

Free memory in stopTone: 1336

Playing Melody
Longitud: 26

Frequencies received by PlayMelody method
0: (622) 1: (659) 2: (0) 3: (740) 4: (784) 5: (0) 6: (622) 7: (659) 8: (0) 9: (740) 10: (784) 11: (0) 12: (1047) 13: (988) 14: (0) 15: (622) 16: (659) 17: (0) 18: (988) 19: (932) 20: (0) 21: (880) 22: (784) 23: (659) 24: (587) 25: (659) 
Free memory before playMelodyTone: 1338

Frequencies played by playMelodyTone
0:(622) 
Free memory after playMelodyTone: 1338

Free memory on updateMelody: 1538
1:(587)

After the first array are made the free memory reduces from 1544 to 1336. This stay the same until the first tone is played and setup finished.

But just when loop starts the free memory goes up to 1538. Could this mean some array data has been remove from ram?

I change de definition from int to long, but it just make things worst, now frequencies value get really high.

unsigned long* melodyFrequencies;
unsigned long* melodyDurations;

prueba_sonido_rttl2.ino (11.2 KB)

Ok this is what I found out until now… passing the parameters is not the problem, everything works ok until the global pointers are asigned.

I change the global pointers to simple arrays and the melody plays ok, no memory corruption. But this make big impact in memory use, if I use a melody of 200 notes, there is almost no memory left (823 bytes left of 2k)

prueba_sonido_rttl3.ino (11.3 KB)