Debug code setup problem

I have created a small debug code at the start of all my sketches I can enable or diable with the first line
DEBUG = (0 OFF 1 ON)

#define DEBUG 1
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif

it removes and disables any Serial.print or Serial.println in the sketch to save on memory, works quite well,
HOWEVER...

if i use something like;
debug(mbyte, DEC);
OR
debugln(mbyte,DEC);

i get the error code:
HEX DEC BIN Tables from 0-254/HEX DEC BIN Tables from 0-254.ino:26:21: error: macro "debugln" passed 2 arguments, but takes just 1

exit status 1

Compilation error: macro "debugln" passed 2 arguments, but takes just 1

I know its probably a simple fix to be able to use the code and i've hsmmered my head for hours and googled a lot with no help.

Am I just stupid??!! :melting_face:

Thanks in advance!!

you define debug() with one parameter

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

but call it with two

debug(mbyte, DEC);

hence the error message

Compilation error: macro "debugln" passed 2 arguments, but takes just 1
1 Like

Can you try something like this instead?

#if DEBUG == 1
  #define debug   Serial.print
  #define debugln Serial.println
#else
  #define debug   (void)
  #define debugln (void)
#endif

I guess that will generate a warnings when DEBUG==0 so maybe something like this instead:

#if DEBUG == 1
  #define debug(...)   Serial.print(__VA_ARGS__)
  #define debugln(...) Serial.println(__VA_ARGS__)
#else
  #define debug(...)
  #define debugln(...)
#endif
1 Like

THANK YOU!!!!! That did it! I assumed that just defining them would work but im guessing it just didnt know what to do with the extra command?

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