Some Arduino (0022) tips

Image Caption
Image Caption

In my last project , I was using Arduino alpha version 0022. At the beginning everything was ok but when I started doing more complex stuff such as: concatenating strings, reusing string objects, assigning one string to another and overall leaving my app working for few hours…then serious memory leaks and corruption problems were happening. Programming with Arduino processing is particularly sensible to memory problems mainly because the loop() function is continuously running, so string objects are continuously being reused, resized, etc..

In order to have a more precise control of memory usage in my Arduino application I decided to use classic C language dynamic char arrays. For a programmer with extensive background in C language this was the shortest solution. Searching on google I realized I was not the only one having this problem so hopefully next Arduino releases fix this bug.

Another problem that I experienced while running my Arduino application is that the execution stopped at some point for no reason. After a while I realized that doing many Serial.println(..) was causing it, so I removed some and everything was working again. I suppose having a lot of debugging information going through the serial port could be interfering with the rest of operations.

Tips when using Arduino Libraries:

  • Better do not use the String Library, implement dynamic char arrays instead.
  • Limited Use of Serial.println(..).

And one last tip for debugging your Arduino Application: measure how much memory is free with the following code Free Memory!.

This article was originally published in Magda’s personal blog