Sunday, April 22, 2018

Tips and tricks to reduce Arduino sketch size

If you are using Arduino board and you have limited flash memory size, the following are some tips and tricks to reduce sketch size:

[1] First of all, try to divide your code into blocks to help you analyze what portion of code consumes much memory. For example, make blocks for "Measuring", "Control", "Display", ... etc. Create a spread sheet named "Sketch size analysis" and write down the size of each code block and sort blocks from largest to smallest. The following is an example for sketch size analysis of one of my projects:



[2] Reduce #include of libraries as possible as long as compiling will not output errors

[3] Avoid using serial port initialization and serial port printing for debugging purpose

[4] Avoid using float numbers as possible

[5] Avoid using string arrays as possible. Use character arrays instead "const char*"

[6] Use #define for constants so the compiler will replace this constant name with the corresponding value

[7] Get rid of non-used portions of code

[8] For TFT displays, avoid using custom fonts, use small font sizes, reduce number of character bitmaps in font file. You can reduce number of characters by using all letters as small (abcd...) or all letter as capital (ABCD...)

[9] For TFT displays, draw logos, icons, and clip arts using union and intersection of geometric shapes instead of using bitmaps

[10] Convert duplicated code to function

[11] Reduce string constants whenever possible

[12] Avoid using math functions like "pow()" or "log()"

[13] Calculate arrays in real-time

[14] Avoid using string functions like "sprintf()" and "dtostrf()"

[15] Avoid using standard functions that have too much options or cases while you only have one case. Create your own function

[16] Reduce variables used as possible

[17] Store 8 Boolean variables in one byte using bitwise operations

[18] Use the minimal variable size possible for your data type

[19] Don't assign default values for global variables that will be calculated later or called from EEPROM

[20] Store const arrays to EEPROM if you have enough space in EEPROM






1 comment:

  1. The main motive of the Big data banking analytics is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete