877
December 22, 2022, 12:07pm
1
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...
docdoc
December 22, 2022, 12:10pm
3
Idk, but why not doing #define DEBUG_PRINT2(x,y) Serial.print (x,y)
and then DEBUG_PRINT2(count_case, 0);
?
1 Like
877
December 22, 2022, 12:13pm
4
docdoc:
Idk, but why not doing #define DEBUG_PRINT2(x,y) Serial.print (x,y)
and then DEBUG_PRINT2(count_case, 0);
?
Yes that works thank you!
877
December 22, 2022, 12:15pm
5
b707:
did you try that?
Yes I tried it and it works, but gives errors for single case arguments.
Creating another define for these two argument cases works:
b707
December 22, 2022, 12:22pm
6
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.
docdoc
December 22, 2022, 12:29pm
7
Yep, because there's no overloading for #define symbols.
system
Closed
June 20, 2023, 12:30pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.