My sketch is too big! Help!

Hi,

I’m in the process of creating a sketch in Arduino and when I verify the sketch an error message appears saying that the sketch is too large but I’m not quite sure why. This is the error message that appears:

Sketch uses 12,044 bytes (39%) of program storage space. Maximum is 30,720 bytes.
Global variables use 2,072 bytes (101%) of dynamic memory, leaving -24 bytes for local variables. Maximum is 2,048 bytes.

However, I’m not quite sure why there is this error as my sketch isn’t that big: I will upload my sketch too.

Thankyou in advance :slight_smile:

Updated_button_press_and_text_.ino (12.4 KB)

Your code is chock full of string literals. They all occupy space in SRAM, which is limited.

Your code abuses the hell out of memory with the stupid String class.

Global variables use 2,072 bytes (101%) of dynamic memory, leaving -24 bytes for local variables. Maximum is 2,048 bytes.

There are 2048 bytes of SRAM, used for stack, heap, variables, arrays, all the changeable stuff. You’re using 2072. Can likely move a lot of string variables into PROGMEM and get SRAM space free. Then your sketch will look somewhat bigger, while the dynamic memory usage goes down.

I use 1284P a lot, it has 16K of SRAM, I made a sketch that used 14625 bytes of it to blast out user changeable data at 20 KHz rate, 45 bytes at a time.

As CrossRoads points out, string literals are duplicated into SRAM memory. Try using the F() macro to avoid that. For example, instead of:

Serial.println("This is my code.");

change it to:

Serial.println(F("This is my code."));

to see if it helps.