4

What are some of the best programming practices that can be adopted while coding sketches for Arduino, so that it uses the least memory and performance should not suffer from memory outage, and still the code be robust?

From the link: https://www.arduino.cc/en/Products/Compare

I have learned that the Uno has 32 KB of Flash memory. The maximum available is in the MEGA with 256 KB. I think Uno and Mega are the most widely used by people.

dda
  • 1,595
  • 1
  • 12
  • 17
Ciasto piekarz
  • 575
  • 3
  • 12
  • 28

2 Answers2

5

Paul covered the most important points. This answer is intended to be a complement to his.

First, you should keep in mind the general rules, which are more important than the more specific ones. The general rule for saving RAM is: be aware of what you are storing, and do not store anything you do not really need. The general rule for saving flash is: be aware of what you are doing, and do not do anything you do not really need.

Now, a few specific tips:

Use the const qualifier for declaring constants (e.g. const int ledPin = 13;). The compiler will optimize-out the storage of the constant and use immediate addressing instead.

Use PROGMEM for arrays of constants.

Whenever possible, use static memory (globals and static locals) rather than heap allocation (malloc() and new). This will prevent memory fragmentation and make the memory usage more predictable (only the stack holds dynamic memory): you know at compile time, rather than run time, whether you are low in RAM.

For the same reason, prefer C strings (char *) over String objects.

Use the C99 types int8_t and uint8_t whenever 8 bits are enough for your integer variables.

Use the static qualifier for functions that will be used only in the same source file, especially for functions that are called only from one place. This gives the compiler more optimization opportunities.

If you need to optimize, use avr-nm to know what is consuming your precious RAM.

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
3

Keep in mind that RAM is even less. 32KB is a fair lot of code, since C/C++ is highly efficient. "Don't save **** in RAM."

If you need to load images into a display, save the images on an SD card. And "stream" it to the display. You can read and transmit the bytes one by one, or 10 by 10 or something.

Also, if you need long strings, you can use the "flashStringHelper" to store the string in flash memory. For example: println(F(”Error message: Couldn't connect to server));. By using F() around the string, you save valuable bytes.

Not using Arduino code, but pure AVR-C/C++, can save you a little also. (Will also save the bootloader piece of memory, if you program directly with a programmer.)

If you use Arduino, know how it works. The Arduino serial library uses ~256 or 500 bytes (I believe) as a buffer. You can change this if you need it for other applications.

Keep in mind that your saving technique depends on what resources are low. If you need to do a lot simultaneously, it's better to write non-blocking code.

But also remember: Don't optimize unless it's actually needed! Or, actually set measurable and realistic requirements, optimizing something to be "as fast as possible" can take infinite amounts of time... Run a google search on "premature optimization" as the old folks in the field like to call it.

Code Gorilla
  • 5,652
  • 1
  • 17
  • 31
aaa
  • 2,715
  • 2
  • 25
  • 40