Arduino blog

Arduino blog

Arduino thinkering

This blog is about things I come by related to Arduino which I feel are inspiring/important/funny enough to write about.

Debugging and unit testing Arduino (Part 2) Conceptually

eclipse pluginPosted by Jan Sat, May 02, 2015 21:02:23
As explained in the previous post we focus on debugging/unit testing your code. However in most cases your code is mixed and mingled with library code and arduino code.
I know your code is not because you strictly separate them smiley but I also know that the first time you want to debug you'll find some code that ......

To make this a bit real, lets look at the code of blink without delay
If we'd cut the code in your code and arduino code (as you always do smiley) the loop would look like

void loop()
ledState = calcLedState(millis(),ledState);
digitalWrite(ledPin, ledState);

And calcLedState would look like

uint8_t calcLedState(uint32_t currentMillis,uint8_t ledState)
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;

if (ledState == LOW)
return HIGH;
return LOW;

The method calcLedState no longer contains any arduino calls. As such we can create a C/C++ project and dump the code in there to debug the method calcLedState. This can be done with the standard CDT debugging tools (see the CDT documentation o how to do so).

This way of working works fine but there are some major drawbacks.
First of all there is the code duplication. As you copy your code to debug you will have to copy your debugged code back to your Arduino project. Chances for mistakes are bug. (typo intended)
Secondly there is a whole lot of Arduino code that is so useful you'll use it in your code. Think about constrain, map, max,...
The same counts for libraries; think about time, AES ....
All this code will force you to do more work to get your Arduino code to run in the new project.

So even-though this method works fine it turned out not to be practical enough to be usable.

To be usable you need an Arduino lookalike library/core that builds fine on your pc. It does not have to provide all the functionality but you need at least a function implementation for all Arduino methods.

I mean "delay" does not have to provide the functionality (wait number of millis, but the compiler and linker must be able to compile and link to enable debugging/unit testing.

On the other hand constraint, map, max ... must be available and implemented properly. Because you use them in your code, remember?.

So I came up with the idea of porting the Arduino AVR boards to the PC. That would satisfy these needs so you don't have to copy the code but only had to change the arduino board in the properties page of the project.

Again this worked fine (and lots better) but supporting multi configurations would make life even easier.
So I did that one to.smiley

The result is that I ended up with 1 project with 3 configurations. 1 configuration is used to compile and upload to Arduino; 1 is used to compile and debug locally and the last is used to unit test.

The next post will deal with how to set this up practically.

PS thanks for the comments. Keep me writing by keeping them coming :-)

  • Comments(4)//