Let's Make Robots!

General Programming - Debug


When I'm coding, depending on the programming language, I usually like to sprinkle debugging statements into my code to see data, code decisions and general information while the program is executing.  Using the Arduino, I can use the Serial library to stream print statements to the Serial Monitor for example.

Option 1: In code #if

The first option is very simple.  At the top of your source file, you put in the following line when you want to turn debugging code on:

#define DEBUG

Then in your code, you can put lines like the following that will be compiled and executed when the DEBUG flag is present, but will not be included in your code when the #define is removed:

#ifdef DEBUG
  Serial.println("Debugging statement");

Option 2: Debug Macro

Another option is to create a header file with a debugging macro that will execute or remove the code as requested.  You still need to define DEBUG, but you also need a header file included that contains the following:




#ifndef DEBUG_H
#define DEBUG_H

#ifdef DEBUG
#define DebugCode( code_fragment ) { code_fragment }
#define DebugCode( code_fragment )



Then, in your code you would use debugging code as follows:

  Serial.print("display and ");
  Serial.println("debugging statements go here");


Later on in our development cycle, would could add more to the DebugCode macro to always display a variable for example.  We would do this like:

#define DebugCode( code_fragment ) { \
  Serial.print("x="); \
  Serial.println(x); \
  code_fragment }

Now whenever we perform our debug code during execution, we would always see the value of our 'x' variable.


Both options are equally good in my opinion.  My preference is the second option because it leaves itself open to more customization and has the added benefit of not having to type and see all the #ifndef DEBUG...#endif statements cluttering up my code.

I'm sure there are many schools of thought on the handling of debug code. Let me know your thoughts.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I agree with you: debug is important.

One of my first personnal sketch was called "verbose":

inside, it had antologic part of revolutionnary code like :

     void loop()

     Serial.print("hey, do you realise that's the start of the loop man !?!")


Of course you choise output style wich match with the way you like to receive feedback informations.

I think the hot point of this reflexion have to deal with memory, _RAM memory_ wich seems to be first stuff that could ~constraint someone who code to search for a more efficient code. If I am not short, I put a maximum of non-code stuffs in a sketch, at the begining to explain it or to describe the circuit, inside, etc ..., for other to share, but too for me later.

I share with you the preoccupation to make maximum human-readable code (usefull is debug, but coments(=" // ") too, identations too, tabs for comfort of reading and serenity of creator/future debug man, ...).

Unfortunately I'm not able to share it with you as much I would want to : you could find libraries, and strategies without libraries for keeping an eye on free memory while a sketch runs;

and there is a third method wich consist to create a fonction "if debug" and so have the choice to activate, or no, followings associated parts of the code.


you will see in first example.

OK, I hope that will be usefull, I hope there are lot of other talentous English speaking persons who will answer you, I will try to follow this subject, even if I probably won't write again (I read LMR since II).

Good suggestion, thanks. 

However, this only works with the Arduino connected with the computer or using a LCD display.

I am often using LED's for debugging to see what happend while running the program without to be connected to a computer.

I'm glad you enjoyed this post.

You are totally correct that my example would only work with a LCD or connection to the computer but I'm sure we could change it to light up a sequence of LEDs or flash them a particular way if needed.

Let me know how you use this so I can learn different techniques as well.