Another Debug Question

I would like to use a macro to enable selective turn on/off of certain lines of code that I might insert for debug purposes. One way to accomplish that would be to use a macro to insert a "//" prefix and turn that line of code into a comment.

Unfortunately I don't know how to define a "//" into a macro to accomplish that. The complier apparently assumes that the "//" is a comment for that line of code and not as an element of the #define statement.

Here's an example of what I want:

#define debug_remove1 //
#define debug_remove2 //
#define etc, etc,

in the code:
debug_remove1 price * qty = total_price; Serial.print (total_price);

In another code location:
debug_remove2 a * b = c; Serial.print (c);

So by defining a specific #define statement as "//" or nothing I can activate/deactivate selective lines of code.

Any ideas on how to insert "//" into the macro definition? Or any other way to accomplish selective turn on/off of specific debug statements?

Thank you,
Frank

Don't do that, put

#ifdef 
...
#endif

around the debug code.

Duh... ya'think? :slight_smile:

Out of curiosity, is their a way to define "//" in a macro? Maybe an escape char? I tried "" but that didn't seem to work.

Thanks
Frank

frank2644:
Out of curiosity, is their a way to define "//" in a macro?

No. If you use '//' it is removed because it is a comment. If you try to paste it together from two separate tokens you get an error: "error: pasting "/" and "/" does not give a valid preprocessing token"

#define comment(a,b) a##b
comment(/,/) This is a comment?

Same for #define comment /##/

If there are specific functions that you know you will be using for debug purposes (like Serial prints) you can do something like this:

#ifdef DEBUG
  #define DEBUG_PRINTLN(x) Serial.println(x)
#else
  #define DEBUG_PRINTLN(x)
#endif

With your DEBUG symbol defined, something like DEBUG_PRINTLN("Enter temperature unit conversion function") will be expanded to Serial.println("Enter temperature unit conversion function"), but without the DEBUG symbol defined the same line will expand to nothing and disappear.

Thanks everyone,

I've read so many debug threads my head is spinning, but I think I have a passable understanding now.

Frank

This works for me:

#define DEBUG true              // Set to true for debug output, false for no debug output
#define Serial if(DEBUG)Serial

One of the debug techniques I've adopted from another thread. I just added line number to the message. Actually made an include file with the definitions.

definition:
#define debug_print1(...) Serial.print(F(#VA_ARGS" = ")); Serial.print (VA_ARGS);Serial.print(F("--LINE #"));Serial.println(LINE)

in code:
debug_print1(counter);

Deactivate by #define debug_print1(...) //no definition

Add debug_print2(...), print3, etc.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.