Large sketches - Arduino Mega 2560 hangs

Hello!

I am just facing the weirdest issue ever...

My code runs perfectly, SRAM is 95% free. By populating the SRAM with random strings up to 90% of its capacity (10% is left for the actual code) it runs like butter. But if I put large strings on flash memory using PROGMEM, and the usage is more than 29% (around 70.000 bytes), the Mega 2560 hangs.

It doesnt turn on or anything like that. Just sits there doing nothing. If I remove a few strings and the flash memory is at 28%, it runs perfetly again.

Oh, and also it uploads just fine. No errors or anthing.

Any ideas?

Thanks

By populating the SRAM with random strings up to 90% of its capacity (10% is left for the actual code)

This is nonsense. Code doesn't go in SRAM.

Any ideas?

Of course. Read the stickies at the top of the forum. The ones that say "POST YOUR CODE!".

sorry, my bad. by doing this, the array goes into FLASH memory:

const unsigned char normal[] PROGMEM = {132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 126, 128, 126, 128, 128, 128, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 126, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 128, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 126, 128, 128, 128, 128, 128, 126, 126, 128, 126, 128, 126, 128, 126, 128, 128, 126, 128, 132, 132, 132, 132, 132, 132, 132, 136, 136, 136, 132, 132, 132, 132, 132, 132, 132, 132, 126, 128, 128, 126, 128, 126, 128, 126, 128, 126, 128, 128, 126, 128, 126, 128, 126, 128, 128, 128, 128, 128, 126, 128, 128, 126, 132, 132, 132, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 132, 132, 132, 132, 132, 126, 126, 128, 126, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 126, 128, 126, 128, 126, 128, 132, 132, 136, 136, 141, 145, 145, 150, 150, 150, 150, 150, 150, 150, 145, 145, 141, 136, 132, 132, 126, 122, 122, 118, 118, 113, 113, 113, 113, 113, 113, 113, 118, 118, 118, 122, 122, 122, 122, 122, 126, 128, 128, 126, 132, 136, 141, 150, 159, 163, 168, 172, 177, 181, 181, 181, 181, 177, 172, 168, 159, 150, 145, 136, 128, 122, 118, 109, 104, 104, 100, 100, 100, 100, 100, 104, 104, 109, 109, 113, 113, 118, 118, 122, 122, 122, 128, 128, 132, 150, 163, 186, 208, 230, 248, 255, 255, 255, 255, 255, 255, 255, 255, 235, 221, 199, 177, 154, 132, 118, 100, 86, 73, 64, 59, 55, 55, 59, 64, 68, 73, 82, 91, 95, 104, 109, 113, 118, 122, 128, 126, 132, 132, 136, 159, 172, 195, 221, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 239, 221, 199, 177, 154, 128, 113, 95, 77, 68, 59, 55, 51, 55, 59, 60, 68, 73, 82, 91, 95, 104, 109, 113, 118, 118, 122, 122, 122, 122, 132, 154, 168, 190, 217, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 244, 226, 199, 177, 154, 128, 113, 95, 77, 68, 55, 55, 51, 51, 55, 60, 64, 68, 77, 86, 91, 95, 104, 109, 118, 122, 128, 132, 132, 136, 136, 154, 177, 195, 221, 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 230, 208, 186, 163, 136, 113, 95, 82, 68, 55, 51, 46, 42, 46, 51, 55, 64, 68, 82, 91, 104, 113, 122, 132, 141, 145, 154, 154, 159, 159, 168, 190, 203, 221, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 239, 217, 195, 172, 150, 126, 109, 91, 77, 64, 60, 55, 55, 55, 59, 64, 73, 82, 91, 104, 113, 126, 136, 141, 150, 154, 159, 159, 163, 159, 159, 177, 194, 208, 226, 244, 255, 255, 255, 255, 255, 255, 255, 253, 239, 226, 208, 190, 177, 159, 145, 132, 118, 109, 100, 91, 86, 86, 82, 82, 82, 82, 91, 91, 95, 100, 104, 113, 118, 118, 122, 128, 132, 136, 136, 136, 141, 177, 208, 217, 239, 255, 255, 255, 255, 255, 255, 255, 244, 217, 208, 195, 186, 163, 150, 141, 132, 122, 104, 95, 91, 82, 73, 64, 64, 64, 64, 60, 64, 68, 77, 82, 82, 91, 100, 109, 109, 113, 122, 128, 136, 136, 145, 203, 253, 248, 255, 255, 255, 255, 248, 212, 203, 203, 190, 163, 159, 159, 159, 150, 122, 109, 104, 95, 68, 55, 51, 55, 55, 51, 51, 64, 73, 77, 77, 82, 95, 95, 109, 104, 118, 132, 141, 145, 150, 168, 172, 181, 172, 253, 255, 255, 255, 255, 255, 255, 255, 203, 172, 172, 172, 159, 132, 122, 118, 128, 91, 59, 33, 28, 24, 10, 6, 1, 33, 46, 55, 60, 73, 86, 100, 104, 104, 118, 136, 150, 163, 168, 177, 190, 195, 195, 190, 186, 181, 253, 255, 255, 255, 255, 255, 255, 253, 190, 141, 128, 122, 132, 126, 104, 95, 95, 82, 59, 33, 10, 6, 19, 19, 33, 42, 64, 77, 100, 100, 95, 100, 100, 118, 128, 145, 150, 168, 181, 190, 195, 190, 186, 177, 172, 172, 235, 255, 255, 255, 255, 255, 239, 226, 186, 150, 118, 104, 118, 132, 132, 118, 95, 68, 55, 42, 24, 15, 15, 19, 37, 59, 82, 91, 100, 100, 100, 109, 113, 128, 136, 150, 159, 177, 181, 190, 190, 190, 177, 177, 163, 163, 221, 255, 255, 255, 255, 253, 208, 190, 168, 154, 136, 113, 109, 122, 132, 132, 118, 77, 37, 10, 6, 15, 37, 55, 68, 77, 82, 91, 100, 104, 104, 104, 109, 118, 136, 154, 172, 181, 186, 181, 177, 172, 168, 168, 159, 159, 217, 255, 255, 255, 255, 255, 190, 154, 132, 145, 150, 145, 132, 122, 113, 104, 104, 82, 59, 28
};

If i have like 5 of these, the arduino doesn't power up.

And actually using strings like this:

Serial.println("Waiting for information");

The string between "" goes into SRAM. So I'm using F() to keep them into flash. And this is the issue. Combining the arrays and Strings, my FLASH usage is 30%, which makes the Mega crash. If I remove a single array, making the flash 28% full, it powers up and runs perfectly.

The huge arrays are PCM audios, by the way. And after declaring, all I'm doing is playing them using PCM Library.

    startPlayback(normal, sizeof(normal));
    delay(sizeof(normal)/8);

Thanks

The issue may be that the size of ONE of your arrays is too large. Using 28% of flash, or 85%, isn't the issue.

You can either post your code or live with the problem. Your choice.

the code is too big to be posted here, mate :confused: says it exceeds the 9000 characters .

and being too large isn't the issue. because if I repeat the array by calling it "normal2", "normal3" and etc, it hangs on 29% still

How are you using these large arrays?
Are you moving them to RAM to send the data or
are you buffering small chunks at a time?
Dwight

Small chunks.

They are PCM audios, so I'm using this to play them: Arduino Playground - PCMAudio

And works just fine with FLASH memory below 29%, that is what I don't get. My sketch is using 3% of SRAM on global variables and everything else is local. Used the freeRam() function to monitor SRAM usage and its ALWAYS below 5~6%.

Maybe you are shifting your data above 64k so pgm_read_near will fail (wrap)?

That's it! My sketch without the audio files has 10k~. With the audio files on the FLASH memory, it works until the total size exceeds 74k, which means it fails when I put more then 64K directly on the FLASH. Thanks a lot. Will be looking how I can fix that :slight_smile:

Thanks again!

That's it!

And THAT is why we ask you to post your code. Guessing should not be necessary.

How many times I have to tell you I can't post it? It has more than 9000 characters dude
Unless theres a way to append an external file or whatever, which I don't know..

But thanks for helping

Unless theres a way to append an external file or whatever, which I don't know..

Well, of course there is. Use Reply, not the useless Quick Reply, and there will be a link below the text window, Additional Options. Click the link, and you can attach files to your post.

ok ty