Trying to reduce the size of my program (flash, not SRAM)

The title says it all. I've been desperately search on the internet to reduce the size of my badly written program and I'm giving up. I need YOUR help! Here's some information to help my on my quest:

  • I've removed all useless libraries
  • I've downgraded all variable type possible (e.g. Int to byte or Int to short)
  • My project has a SD card slot, maybe it can help.
  • Before you ask: No, I don't have enough money to buy an more expensive arduino
  • Currently my program takes 101% of the space available and I need at least 86% because I haven't finish coding it.

The code:

CODE - Pastebin.com (It's too long, I wasn't able to put it here)

My professional advice is to start completely over with a new program. Keep to old one, but improve your logic.

Paul

Paul_KD7HB:
My professional advice is to start completely over with a new program. Keep to old one, but improve your logic.

Paul

If I would have followed your advice, the second program may be worst.

Yikes! The sketch uses 33600 bytes of FLASH/PROGMEM?!? I looked at the code and don't see any large arrays. I even looked at the graphics libraries. Maybe there are fonts that take up a lot of space?

johnwasser:
Yikes! The sketch uses 33600 bytes of FLASH/PROGMEM?!? I looked at the code and don't see any large arrays. I even looked at the graphics libraries. Maybe there are fonts that take up a lot of space?

I've tryed to remove unused font library but it dosn't look like it change anything...

I got a file list of the build directories but I'm not sure I know what it all means. I think the files of interest are the .o files (object code). Maybe someone with more experience can say if any of the file sizes are unusually large. From what little I know it appears that the Adafruit_GFX.cpp.o and SdFile.cpp.o files take up the most room.

Johns-MacBook-Pro:arduino_build_896642 john$ ls -Rl
total 888
-rw-r--r--  1 john  staff     984 Jun 27 21:25 build.options.json
drwxr-xr-x  2 john  staff      68 Jun 27 21:09 core
-rw-r--r--  1 john  staff    4180 Jun 27 21:25 includes.cache
drwxr-xr-x  9 john  staff     306 Jun 27 21:09 libraries
drwxr-xr-x  3 john  staff     102 Jun 27 21:09 preproc
drwxr-xr-x  5 john  staff     170 Jun 27 21:25 sketch
-rw-r--r--  1 john  staff      13 Jun 27 21:25 sketch_jun27a.ino.eep
-rwxr-xr-x  1 john  staff  244808 Jun 27 21:25 sketch_jun27a.ino.elf
-rw-r--r--  1 john  staff   94526 Jun 27 21:25 sketch_jun27a.ino.hex
-rw-r--r--  1 john  staff   93844 Jun 27 21:25 sketch_jun27a.ino.with_bootloader.hex

./core:

./libraries:
total 0
drwxr-xr-x  6 john  staff  204 Jun 27 21:09 Adafruit_GFX_Library
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 Adafruit_ILI9341
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 Adafruit_STMPE610
drwxr-xr-x  7 john  staff  238 Jun 27 21:09 SD
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 SPI
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 SoftwareSerial
drwxr-xr-x  5 john  staff  170 Jun 27 21:09 Wire

./libraries/Adafruit_GFX_Library:
total 312
-rw-r--r--  1 john  staff    1572 Jun 27 21:09 Adafruit_GFX.cpp.d
-rw-r--r--  1 john  staff  146940 Jun 27 21:09 Adafruit_GFX.cpp.o
-rw-r--r--  1 john  staff     190 Jun 27 21:09 glcdfont.c.d
-rw-r--r--  1 john  staff    1636 Jun 27 21:09 glcdfont.c.o

./libraries/Adafruit_ILI9341:
total 136
-rw-r--r--  1 john  staff   1771 Jun 27 21:09 Adafruit_ILI9341.cpp.d
-rw-r--r--  1 john  staff  64596 Jun 27 21:09 Adafruit_ILI9341.cpp.o

./libraries/Adafruit_STMPE610:
total 96
-rw-r--r--  1 john  staff   1620 Jun 27 21:09 Adafruit_STMPE610.cpp.d
-rw-r--r--  1 john  staff  43520 Jun 27 21:09 Adafruit_STMPE610.cpp.o

./libraries/SD:
total 168
-rw-r--r--  1 john  staff   1763 Jun 27 21:09 File.cpp.d
-rw-r--r--  1 john  staff  25040 Jun 27 21:09 File.cpp.o
-rw-r--r--  1 john  staff   1759 Jun 27 21:09 SD.cpp.d
-rw-r--r--  1 john  staff  45732 Jun 27 21:09 SD.cpp.o
drwxr-xr-x  8 john  staff    272 Jun 27 21:09 utility

./libraries/SD/utility:
total 400
-rw-r--r--  1 john  staff    1525 Jun 27 21:09 Sd2Card.cpp.d
-rw-r--r--  1 john  staff   56052 Jun 27 21:09 Sd2Card.cpp.o
-rw-r--r--  1 john  staff    1658 Jun 27 21:09 SdFile.cpp.d
-rw-r--r--  1 john  staff  101120 Jun 27 21:09 SdFile.cpp.o
-rw-r--r--  1 john  staff    1662 Jun 27 21:09 SdVolume.cpp.d
-rw-r--r--  1 john  staff   28728 Jun 27 21:09 SdVolume.cpp.o

./libraries/SPI:
total 32
-rw-r--r--  1 john  staff  1335 Jun 27 21:09 SPI.cpp.d
-rw-r--r--  1 john  staff  9680 Jun 27 21:09 SPI.cpp.o

./libraries/SoftwareSerial:
total 80
-rw-r--r--  1 john  staff   1494 Jun 27 21:09 SoftwareSerial.cpp.d
-rw-r--r--  1 john  staff  33616 Jun 27 21:09 SoftwareSerial.cpp.o

./libraries/Wire:
total 72
-rw-r--r--  1 john  staff    769 Jun 27 21:09 Wire.cpp.d
-rw-r--r--  1 john  staff  30028 Jun 27 21:09 Wire.cpp.o
drwxr-xr-x  4 john  staff    136 Jun 27 21:09 utility

./libraries/Wire/utility:
total 56
-rw-r--r--  1 john  staff    598 Jun 27 21:09 twi.c.d
-rw-r--r--  1 john  staff  21260 Jun 27 21:09 twi.c.o

./preproc:
total 32
-rw-r--r--  1 john  staff  12961 Jun 27 21:25 ctags_target_for_gcc_minus_e.cpp

./sketch:
total 176
-rw-r--r--  1 john  staff  12916 Jun 27 21:25 sketch_jun27a.ino.cpp
-rw-r--r--  1 john  staff   2525 Jun 27 21:25 sketch_jun27a.ino.cpp.d
-rw-r--r--  1 john  staff  67236 Jun 27 21:25 sketch_jun27a.ino.cpp.o
Johns-MacBook-Pro:arduino_build_896642 john$

johnwasser:
I got a file list of the build directories but I'm not sure I know what it all means. I think the files of interest are the .o files (object code). Maybe someone with more experience can say if any of the file sizes are unusually large. From what little I know it appears that the Adafruit_GFX.cpp.o and SdFile.cpp.o files take up the most room.

Johns-MacBook-Pro:arduino_build_896642 john$ ls -Rl

total 888
-rw-r--r--  1 john  staff    984 Jun 27 21:25 build.options.json
drwxr-xr-x  2 john  staff      68 Jun 27 21:09 core
-rw-r--r--  1 john  staff    4180 Jun 27 21:25 includes.cache
drwxr-xr-x  9 john  staff    306 Jun 27 21:09 libraries
drwxr-xr-x  3 john  staff    102 Jun 27 21:09 preproc
drwxr-xr-x  5 john  staff    170 Jun 27 21:25 sketch
-rw-r--r--  1 john  staff      13 Jun 27 21:25 sketch_jun27a.ino.eep
-rwxr-xr-x  1 john  staff  244808 Jun 27 21:25 sketch_jun27a.ino.elf
-rw-r--r--  1 john  staff  94526 Jun 27 21:25 sketch_jun27a.ino.hex
-rw-r--r--  1 john  staff  93844 Jun 27 21:25 sketch_jun27a.ino.with_bootloader.hex

./core:

./libraries:
total 0
drwxr-xr-x  6 john  staff  204 Jun 27 21:09 Adafruit_GFX_Library
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 Adafruit_ILI9341
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 Adafruit_STMPE610
drwxr-xr-x  7 john  staff  238 Jun 27 21:09 SD
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 SPI
drwxr-xr-x  4 john  staff  136 Jun 27 21:09 SoftwareSerial
drwxr-xr-x  5 john  staff  170 Jun 27 21:09 Wire

./libraries/Adafruit_GFX_Library:
total 312
-rw-r--r--  1 john  staff    1572 Jun 27 21:09 Adafruit_GFX.cpp.d
-rw-r--r--  1 john  staff  146940 Jun 27 21:09 Adafruit_GFX.cpp.o
-rw-r--r--  1 john  staff    190 Jun 27 21:09 glcdfont.c.d
-rw-r--r--  1 john  staff    1636 Jun 27 21:09 glcdfont.c.o

./libraries/Adafruit_ILI9341:
total 136
-rw-r--r--  1 john  staff  1771 Jun 27 21:09 Adafruit_ILI9341.cpp.d
-rw-r--r--  1 john  staff  64596 Jun 27 21:09 Adafruit_ILI9341.cpp.o

./libraries/Adafruit_STMPE610:
total 96
-rw-r--r--  1 john  staff  1620 Jun 27 21:09 Adafruit_STMPE610.cpp.d
-rw-r--r--  1 john  staff  43520 Jun 27 21:09 Adafruit_STMPE610.cpp.o

./libraries/SD:
total 168
-rw-r--r--  1 john  staff  1763 Jun 27 21:09 File.cpp.d
-rw-r--r--  1 john  staff  25040 Jun 27 21:09 File.cpp.o
-rw-r--r--  1 john  staff  1759 Jun 27 21:09 SD.cpp.d
-rw-r--r--  1 john  staff  45732 Jun 27 21:09 SD.cpp.o
drwxr-xr-x  8 john  staff    272 Jun 27 21:09 utility

./libraries/SD/utility:
total 400
-rw-r--r--  1 john  staff    1525 Jun 27 21:09 Sd2Card.cpp.d
-rw-r--r--  1 john  staff  56052 Jun 27 21:09 Sd2Card.cpp.o
-rw-r--r--  1 john  staff    1658 Jun 27 21:09 SdFile.cpp.d
-rw-r--r--  1 john  staff  101120 Jun 27 21:09 SdFile.cpp.o
-rw-r--r--  1 john  staff    1662 Jun 27 21:09 SdVolume.cpp.d
-rw-r--r--  1 john  staff  28728 Jun 27 21:09 SdVolume.cpp.o

./libraries/SPI:
total 32
-rw-r--r--  1 john  staff  1335 Jun 27 21:09 SPI.cpp.d
-rw-r--r--  1 john  staff  9680 Jun 27 21:09 SPI.cpp.o

./libraries/SoftwareSerial:
total 80
-rw-r--r--  1 john  staff  1494 Jun 27 21:09 SoftwareSerial.cpp.d
-rw-r--r--  1 john  staff  33616 Jun 27 21:09 SoftwareSerial.cpp.o

./libraries/Wire:
total 72
-rw-r--r--  1 john  staff    769 Jun 27 21:09 Wire.cpp.d
-rw-r--r--  1 john  staff  30028 Jun 27 21:09 Wire.cpp.o
drwxr-xr-x  4 john  staff    136 Jun 27 21:09 utility

./libraries/Wire/utility:
total 56
-rw-r--r--  1 john  staff    598 Jun 27 21:09 twi.c.d
-rw-r--r--  1 john  staff  21260 Jun 27 21:09 twi.c.o

./preproc:
total 32
-rw-r--r--  1 john  staff  12961 Jun 27 21:25 ctags_target_for_gcc_minus_e.cpp

./sketch:
total 176
-rw-r--r--  1 john  staff  12916 Jun 27 21:25 sketch_jun27a.ino.cpp
-rw-r--r--  1 john  staff  2525 Jun 27 21:25 sketch_jun27a.ino.cpp.d
-rw-r--r--  1 john  staff  67236 Jun 27 21:25 sketch_jun27a.ino.cpp.o
Johns-MacBook-Pro:arduino_build_896642 john$

How do we remove completly a library? The GFX library isn't used (Not sure)

STUDIOCRAFTapps:
If I would have followed your advice, the second program may be worst.

Programming is an art form. My nephew is a commercial artist who lived with while going to Seattle Art Institute. He often worked on a piece and started over again and again till he got it right. Like programming each iteration is a learning experience.

I have had to start over on a program several times because of what I learned in doing. I have advised other programmers, at times, to do the same. A much better product was the result.

Working directly with the hardware is more difficult because you are dealing with more than just logic.

Paul

The code:

CODE - Pastebin.com (It's too long, I wasn't able to put it here)

So, you dumped it in the rubbish bin, instead. Probably a good idea.

That last statement is NOT true. When you use Reply, or create a new post, there is a link at the bottom of the text entry area, called Additional Options. One of them lets you attach you code HERE.

STUDIOCRAFTapps:
How do we remove completly a library? The GFX library isn't used (Not sure)

Sadly, Adafruit_ILI9341.h includes Adafruit_GFX.h and class Adafruit_ILI9341 inherits from Adafruit_GFX. You can't get all those fancy graphics for nothing. :slight_smile:

johnwasser:
I got a file list infected

Fixt. :wink: Seriously though, never download from pastebin.

@STUDIOCRAFTapps, do like PaulS suggested: use the Attachments and other options to attach your INO file. Also post links of the libraries you downloaded (or the name and version from the Library Manager).

Alternatively, set up a github account.

Either way, more people would take a look at your code.

Here's the list of what my program should do:

  • Read Informations from the Data Folders (DATA0,DATA1...) in the SD card like: Name, Port, Min and Max and store it in an array.

LOOP:

  • Read Cereal information (or Serial, I don't remember) and store it in floating point variable (Value0 ,Value1...)
  • Each 5 sec, the program write into the DF0 file of every single Data Folders (For example: If the data folder's port is 2, the program will write the value from the variable Value2) but If the folder is full (> than 255 values)
  • Each 1 min, the program write into the DF1 file of every single Data Folders
  • Do the same thing for 5min, 1h and 12h

Here's every single problem:

  • The program takes 97% of the flash memory (And I haven't finish coding it yet)
  • The program isn't able to write to the DF files (not the DATA ones)
  • Sometime the arduino restart after SavaData(byte) function has been called (and it can also freeze)
  • Sometime a weird DA file (not DA.txt) appear for no reasons
    Edit: not only a "DA" file, an "@" and "D" file
  • The program isn't able to load the BMP file/they aren't displayed
  • Sometime the program stop writing on the SD card...

and many, many other things...

I don't know what to do anymore :o

GreenOS2.ino (11 KB)

Your code does not seem to compile; it starts with this section when I compile (IDE 1.6.7)

void SaveData (byte DFCode) {
  for(int i = 0; i < counter; i++) {
    File file = SD.open("/F/DATA" + String(i) + "/DF" + String(DFCode) +".txt",FILE_WRITE);
    if(ports[i] == 0) {
      file.println(Value0);
    } else if(ports[i] == 1) {
      file.println(Value1);
    } else if(ports[i] == 2) {
      file.println(Value2);
    } else if(ports[i] == 3) {
      file.println(Value3);
    }
    file.close();
  }
}

ports is a pointer to a String (capital S); not related to the integer that you compare it with.

Which board? For an 328 based board you are at the limits of RAM usage (around 1500 bytes) and that is probably your main cause of the crashes / reboots; String (capital S) chews away memory without warning. Get rid of the String (capital S) and use normal nul terminated character arrays.

The TFT stuff usually is also expensive on memory (I haven't used one yet).

You can save some by not using Strings.

You could also put Value0..3, Cursor0..4, TCursor0..4 in arrays and process them in loops instead of repeating the (almost) same code multiple times.

But overall, your code is probably dwarfed by the Adafruit and other libraries.

sterretje:
Your code does not seem to compile; it starts with this section when I compile (IDE 1.6.7)

void SaveData (byte DFCode) {

for(int i = 0; i < counter; i++) {
    File file = SD.open("/F/DATA" + String(i) + "/DF" + String(DFCode) +".txt",FILE_WRITE);
    if(ports[i] == 0) {
      file.println(Value0);
    } else if(ports[i] == 1) {
      file.println(Value1);
    } else if(ports[i] == 2) {
      file.println(Value2);
    } else if(ports[i] == 3) {
      file.println(Value3);
    }
    file.close();
  }
}



ports is a pointer to a String (capital S); not related to the integer that you compare it with.

Which board? For an 328 based board you are at the limits of RAM usage (around 1500 bytes) and that is probably your main cause of the crashes / reboots; String (capital S) chews away memory without warning. Get rid of the String (capital S) and use normal nul terminated character arrays.

The TFT stuff usually is also expensive on memory (I haven't used one yet).

Is there a way to verify how much RAM is taken while the pogram is running?

https://playground.arduino.cc/Code/AvailableMemory

But that requires both flash and ram so your mileage may vary.