Are you familiar with templates and variadic functions and overloading?
This code defines a variadic template function called debug().
I’m using function overloading - which means I have two functions here with the same name and depending on the parameters of the call, the compiler will call one version or the other.
I’m using recursion to print an arbitrary number of arguments to the serial port. Recursion means the function is calling itself over and over again until a final stage is reached (called the base recursion case).
Here the base case is the version with no parameters
void debug() {
Serial.println();
}
This function takes no arguments and outputs a newline when the recursion ends. This marks the end of your debug message
The recursive case is:
template<typename T, typename... Args>
void debug(T first, Args... args) {
Serial.print(first);
Serial.write(' ');
debug(args...);
}
This is a variadic (accepts variable number of arguments) template (does not have types defined for the parameters) function that takes one argument first of type T and a parameter pack args.... which you can imagine as a grouping of all the other parameters in the call - it’s the way you capture zero or more remaining arguments.
The function is simple : I print whatever was the first parameter (the compiler will know which type T is and so call the right version of print()), then I write a space to separate the parameters of your debug call but you could remove that and have the space as you did in the text you pass, and then recursively call the debug function with the remaining arguments args....
Each recursive call removes one element from the parameter pack, moving towards the base case debug() with no arguments. At that point, a newline is printed to finish the output and voila !
Does it make more sense? Templates are powerful and the recursion is likely flattened by the optimizer which then would mean the code will look like what you had in the first place, a series of calls to Serial.print().
This seems to me a lot of work still to go through the whole code and comment out stuff. What I use is
#define DEBUG 1 // SET TO 0 OUT TO REMOVE TRACES
#if DEBUG
#define D_SerialBegin(...) Serial.begin(__VA_ARGS__);
#define D_print(...) Serial.print(__VA_ARGS__)
#define D_write(...) Serial.write(__VA_ARGS__)
#define D_println(...) Serial.println(__VA_ARGS__)
#else
#define D_SerialBegin(...)
#define D_print(...)
#define D_write(...)
#define D_println(...)
#endif
instead of Serial.print() or Serial.write() you use D_print() and D_write()
Here we use the __VA_ARGS__ notation that captures all the parameters you had for the macro call.
If you have #DEBUG to 1 then the macro call is replaced by its matching Serial output call with the arguments.
if you set the #DEBUG macro to 0 then there is nothing generated in the replacement - all traces and opening of the Serial port will go away when you compile!
➜ this lets you follow best coding practice and keep multiple instructions separated and with just one character to change (the 1 into 0) all your debug statements are cleaned up!
Edit : fixed a bug in the macro (see the good catch comment below)