Instructables
If you've every hit that 32,256 byte maximum on an Arduino Uno and wondered if you could make your sketch smaller instead of upgrading to a Mega, this is for you!

The advice given at the url the compiler gives is good advice, but how exactly does one "make your program shorter"?  There are several ways, and I'll cover using "#if" defines to switch between debugging and production code, and an intro on how to trim down generic libraries. 

 
Remove these adsRemove these ads by Signing Up

Step 1: Conditional Compilation

Picture of Conditional Compilation
Conditional compilation is one method you can use to save program space without removing code.  It allows you to turn features on and off as needed.

An example can be found in one of the DHT22 sensor libraries to disable the code that returns float values.  At line 35, 57, and 65 you can see "#if !defined(DHT22_NO_FLOAT)".   By default DHT22_NO_FLOAT is not defined so the code is included.   You can define it in your code, before including the library, to disable these functions if you do not need them and reduce the compiled size slightly like:

 #define DHT22_NO_FLOAT
 #include <DHT22.h>

You can also use #defines in your code to switch out debugging and production code, such as:

//uncomment this for dev mode
//#define DEVMODE 1

void setup(){
#if defined(DEVMODE)
    Serial.begin(115200);
    Serial.print("Devmode ON");
#else
  // Setup the LCD
    myGLCD.InitLCD();
#endif

  i2cbuffer = "";
  i2cOutput = "";
  Wire.begin(I2C_SLAVE);
  Wire.onRequest(sendI2Cdata);
  Wire.onReceive(recvI2Cdata);

#if defined(DEVMODE)
  Serial.println("started");
#endif
}

In this case, I don't initialize the LCD, and later in the code I also don't use LCD drawing functions.  This allows me to test the i2c methods while watching the serial port, but sacrifices the LCD display.
versos-tk4 months ago

Could you please upload the edited utft library because I'm new to Arduino.

chrwei (author)  versos-tk4 months ago

My copy of UTFT can be found at https://bitbucket.org/chrwei/sharing/src/001a25e10904af15b2b11cc6583618d2cf37e919/arduino/libraries/myUTFT/?at=master

versos-tk chrwei4 months ago

Thank you!

chrwei (author)  versos-tk4 months ago

if you don't have an ITDB28 LCD it won't do you any good, but if you do I can send it to you.

Thanks for doing this instructable! I want to trim down the Ethernet.h library, but it seems to be built in to the Arduino IDE. Any ideas about that?
chrwei (author)  dlginstructables1 year ago
It appears to be split up, some files, like Client.h, built-in (in hardware/arduino/cores/arduino/ ) and some in libraries/Ethernet. It should be possible to copy files from there into the libraries to over-ride them, assuming there's even anything to be trimmed. I've not tried though.

At first look there seems to be some room for trimming, such as if you only wanted an ethernet client OR server, or don't need DHCP. I'd start by copying the Ethernet folder and start there, seeing how far that takes you.
I'm using a Mac and I can't find any parts of the library in any folder. Any ideas there?
Right-click on the Arduino.app application>Show Contents. It's in Resources>Java>libraries
I did actually try putting Ethernet.h and .cpp in my libraries folder and then trimmed some of what I thought was unused code, but it didn't seem like the compiler was grabbing these libraries.
chrwei (author)  dlginstructables1 year ago
if the include is you'd need to change it to "Client.h". might also have to just rename it.

I don't know much about Mac's. I know linux everything is all in one folder, and Windows there are libraries in Program Files and in your Documents\Ardunio\Libraries folders where the latter is supposed to apply first.
dda1 year ago
Great advice, thanks. I managed to get my sketch to fit on my Esplora following all your advice.