28k+ sketch and SD calls reset arduino

I have been pulling my hair out for the last hour trying to work out what's going on, but basically I have a large sketch I'm trying to integrate and SD card reader into and it was failing miserably. When I load the example sketch all is well. but when I copy that example into a function in my sketch it failed.

Basically I whittled it down to a ketch size issue. When the sketch is 28442 bytes long the device resets, when is a touch less, I get erratic behaviour. When it's a fair bit less (26k) all is good.

With no debug and a lot less helpful messages, my sketch is workable, but it's pretty close to the 28k and by the time I start writing the code to get and put stuff on the SD card, I'll probably pop it again :slight_smile:

Any idea why this is? At a guess has it anything to do with a 4k block size on the SD card going into memory and wiping out some of the code (28k + 4k = 32k)

Any idea how to get round it (other than a smaller sketch :))

Cheers
N.

Basically I whittled it down to a ketch size issue.

It is unlikely that sketch size has anything to do with your problem. It is much more likely that you have run out of SRAM. A sketch that nearly fills the Arduino's code space makes this very likely.

The usual solutions to free up SRAM are not going to be helpful for you, because they more data from SRAM to program space, and you don't have much of that to spare, either.

Pooh :frowning:

Looks like less functionality then, man that sucks a bit. On the plus side though not including the SD libraries does give me 11k spare :slight_smile:

For a single library to take up 33% of the available space seems a little harsh, is there any way to slim that down at all? Can I #define something that will compile out large chunks of stuff that's not used?

Looks like less functionality then

Or a Mega...

For a single library to take up 33% of the available space seems a little harsh

And incorrect, as I use the SD card and Ethernet shield, and my sketches are no where near as large as yours.

Can I #define something that will compile out large chunks of stuff that's not used?

If you are referring to "not used in the SD library", that is a pretty tightly integrated package. Everything is used. Whatever is not used in your particular situation is already left out by the linker.

And incorrect, as I use the SD card and Ethernet shield, and my sketches are no where near as large as yours.

My base sketch was 15k, if I #include <sd.h> and use SD.begin() and SD.exists(), the final sketch is 26k. That said I have managed to keep the lid to 27K by taking out all the luxuries :slight_smile:

The mega idea is a pretty good one. More pins as well… real time clock… gps… Garrrrrr!!! spending more money.

Thanks for the help and clarification.

Thanks for the help and clarification.

I just checked. With all the SD card use commented out, by WebServer sketch drops 9500 bytes in size, compared to when the code is present and logging data. This is using the SD class that comes with the Arduino software (this is with 0022). So, yeah, the library is a resource hog.

I know that fat16lib complains often about the Arduino team's delivering an older, less efficient, version of his library. Perhaps it would be worth looking in the Storage forum for one of his posts, with links to more recent versions, to see if the resource use can be slimmed down any.

Cool, I'll take a peek in the week. There seems to be support for FAT32 as well as FAT16, it might be a good idea to be able to compile out support for the FAT32... that might shave a couple of KB off the library.

Cheers
N.

The SRAM requirements for SDfat are not insignificant either. The library requires 512 bytes of ram just for cache alone, so if you're using anywhere near 1.5k of SRAM before you add in the SD library then expect to have problems.

Thanks extent. And another thing to consider is text strings in debug messages... while very helpful to see on the serial interface or LCD screen, then eat RAM. I had to optimise my debug statements with a debugOutput() function and then #define myDebug 0&&debugOutput, but then I couldn't use the SD card if I reused them.

If you're going to use an SD card and a complicated sketch, as PaulS mentioned, go get a mega :slight_smile: I'm using 2 stepper motors, a 2 channel opto-isolator, 4 input buttons, a serial LCD screen and the SD card... and it all just about fits... nobody breathe too hard near it though :slight_smile:

Post your sketch - someone will no doubt have suggestions regarding how to make it smaller.