DEFINE_PRINT question

I adopted this code from somewhere and hit a snag recently...

#define DEBUG                       // Enable disable debug serial prints - comment out this line only (saves memory).


//Debugging
#ifdef DEBUG
#define DEBUG_PRINT(x)    Serial.print (x)
#define DEBUG_PRINTLN(x)  Serial.println (x)
#define DEBUG_PRINTLNHEX(x) Serial.println (x, HEX)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTLNHEX(x)
#endif
//

MISC CODE BLAH BLAH


 float count_switch = interruptCounter_switch;

 DEBUG_PRINT(count_case, 0);  // Prints the count to the serial monitor
 DEBUG_PRINT(" count\r\n");

Upon compile I get the error:
macro "DEBUG_PRINT" passed 2 arguments, but takes just 1

I think the issue is here:

#define DEBUG_PRINT(x)    Serial.print (x)

Not sure how to allow for a second argument (while allowing for single argument cases, would it be like this?

#define DEBUG_PRINTx,y)    Serial.print (x,y)

I'm not sure if the letter x is important to be honest, or if I can add a letter y just like that. I suspect life will not be that easy...

did you try that?

Idk, but why not doing #define DEBUG_PRINT2(x,y) Serial.print (x,y) and then DEBUG_PRINT2(count_case, 0);?

1 Like

Yes that works thank you!

Yes I tried it and it works, but gives errors for single case arguments.
Creating another define for these two argument cases works:

Of course it doesn't work:

In the first approximation, the define is nothing more than a text substitution. By setting two defines with the same name, you simply redefine the second "on top" of the first. The first one stops working.

To use the macro with variable list of arguments a different syntax is used:

#define DEBUG_PRINT(...)    Serial.print (__VA_ARGS__)

In this case all arguments of DEBUG_PRINT() will transferred to Serial.print() call. Therefore you can use the same macro with any set of arguments which is supported by Serial.print() method itself.

Yep, because there's no overloading for #define symbols.

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