I like programming in small spaces, it makes one think efficiently and not be sloppy.
Example: I was looking at an embedded device the other day, and found a complete Tomcat server and X desktop running on a Raspberry Pi type environment. So that kind of stuff makes me shudder, it just looks wrong and to me it shows that whoever put that together (it was a company product, not just a hobby project) is not really “thinking” embedded. Yes we have more powerful embedded CPUs available now, and more memory, but that all costs power. So really: code efficiency = power efficiency!
Back to Arduino, a standard Arduino Uno type board (Atmel ATMEGA 328P) has 32K of program storage space, and 2K of dynamic memory. The latter is used for runtime variables and the stack, so you want to be sure you always have enough spare there, particularly when using some libraries that need a bit of working space.
A bit of background… while in “big” CPUs the program and variable memory space is generally shared, it’s often separated in microcontrollers. This makes sense considering the architecture: the program code is flashed and doesn’t change, while the variable space needs to be written at runtime.
The OneWire library, used to interface with for instance Maxim one-wire sensors, applies a little lookup table to do its CRC (checksum) calculation. In this case it’s optimising for time, as using a table lookup CRC is much faster than just calculating for each byte. But, it does optimise the table, by using the PROGMEM modifier when declaring the static array. It can be optimised further still, see my post on reducing a CRC lookup table from 256 entries to 32 entries (I submitted a patch and pull request). What PROGMEM does is tell the compiler to put the array in program storage rather than the dynamic variable space.
Arduino uses the standard GNU C++ compiler to cross-compile for the Atmel chips (that form the core of most Arduino boards), and this compiler “normally” just puts any variable (even if “const” – constant) with the rest of the variables. This way of organising is perfectly sensible except with these microcontrollers. So this is why we need to give the compiler a hint that certain variables can be placed in program memory instead!
Now consider this piece of code:
Where does the “Hello, world!” string get stored by default? In the variable space!
I hadn’t really thought about that, until I ran short of dynamic memory in the controller of my hot water system. The issue actually manifested itself by causing incorrect temperature readings, and sometimes crashing. When looking at it closer, it became clear that the poor thing was running out of memory and overwriting other stuff or otherwise just getting stuck. The controller used is an Freetronics EtherTen, which is basically an Arduino Uno with an Ethernet breakout integrated on the same board. Using Ethernet with a Uno gets really tight, but it can be very beneficial: the controller is powered through 802.3f PoE, and communicates using UDP packets.
I knew that I wasn’t actually using that many actual variables, but was aware that the Ethernet library does require a fair bit of space (unfortunately the “how much” is not documented, so I was just running on a “as much as possible”). Yet after compiling I was using in the range of 1K of the dynamic variable space, which just looked like too much. So that’s when I started hunting, thought it over in terms of how I know compilers think, and then found the little note near the bottom of the PROGMEM page, explaining that you can use the F() macro on static strings to also place them in program storage. Eureka!
When you compile an Arduino sketch, right at the end you get to see how much memory is used. Below is the output on a tiny sketch that only prints the hello world as normal:
Sketch uses 1374 bytes (4%) of program storage space. Maximum is 32256 bytes. Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.
If you use the F() modifier for this example, you get:
Sketch uses 1398 bytes (4%) of program storage space. Maximum is 32256 bytes. Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
The string is 13 bytes long plus its ‘\0’ terminator, and indeed we see that the 14 bytes have shifted from dynamic memory to program storage. Great score, because we know that that string can’t be modified anyway so we have no reason to have it in dynamic variable space. It’s really a constant.
Armed with that new wisdom I went through my code and changed the serial monitoring strings to use the F() modifier. That way I “recovered” well over 200 bytes of dynamic variable space, which should provide the Ethernet library with plenty of room!
Now, you may wonder, “why doesn’t he just use an #ifdef to remove the serial debugging code?”. I could do that, but then I really have no way to easily debug the system on this hardware platform, as it wouldn’t work with the debugging code enabled (unless the Ethernet library is not active). So that wouldn’t be a winner. This approach works, and now with the extra knowledge of F() for string constants I have enough space to have the controller do everything it needs to.
Finally, while I did an OSDC conference talk on this solar hot water controller (video on this the PV system in my home) some years ago already, I realise I hadn’t actually published the code. Done now, arjenlentz/HomeResourceMonitor on GitHub. It contains some hardcoded stuff (mostly #define macros) for our family situation, but the main story is that the automatic control of the boost saves us a lot of money. And of course, the ability to see what goes on (feedback) is an important factor for gaining understanding, which in turn leads to adjusting behaviour.