I'm using Ralph Bacons YouTube video that explains how to incorporate Serial.printf() or not.
This works very well and I've increased the number of #define required in order to select the correct Serial.printf().
The code is as follows:
#define DEBUG1 0 // if 0 then DEBUG(n) does not display, if 1 then it does
#define DEBUG2 0
#define DEBUG3 0
#define DEBUG4 0
#if DEBUG1 == 1
#define debug1(a) Serial.printf(a)
#else
#define debug1(a)
#endif
#if DEBUG2 == 1
#define debug2(a,b) Serial.printf(a,b)
#else
#define debug2(a,b)
#endif
#if DEBUG3 == 1
#define debug3(a,b,c) Serial.printf(a,b,c)
#else
#define debug3(a,b,c)
#endif
#if DEBUG4 == 1
#define debug4(a,b,c,d) Serial.printf(a,b,c,d)
#else
#define debug4(a,b,c,d)
#endif
so in the code you use debug1(), debug2(), debug3() and debug(4).
Whilst this works fine I was thinking if there was a way to determine which debug(n) was required so you could just use debug() and the # code would be simpler.
Thank you for your prompt reply. To test your suggestion I've written this test code:
#define DEBUG 1 // if 0 then debug does not display
#if DEBUG == 1
#define debug(...) Serial.printf(__VA_ARGS__)
#else
#define debug(...)
#endif
String test = "hello";
void setup() {
Serial.begin(115200);
while (!Serial);
;
debug("this is a one test \n");
debug("this is a two test %s \n", test );
debug("three things %d %d \n", 1, 2);
debug("four things %d %d \n", 3, 4, 5);
}
That works except with 4 variables it only prints 3. So I increased the number of dots from
2 to 4
Do you mean that it not print "5" in the line below?
Do you know the syntax of the printf operator? It prints as many variables as the number of "%xx" placeholders you specified.
So you need to add another %d in your line that way:
#define DEBUG_SWITCH true // Switch on or off the debugging logs
#if DEBUG_SWITCH == true
#define DEBUG_printf(...) Serial.printf(__VA_ARGS__)
#else
#define DEBUG_printf(...)
#endif
// #include <LibPrintf.h> // If your Arduino IDE does not know "printf", seek library "arduino-printf" at GitHub
while (!Serial) { } // Wait until the serial bus to your Laptop is ready for transmission. Some types of microprocessor require this delay.
DEBUG_printf(F("\n[SETUP] This is the log of sketch '" __FILE__ "', compilled on " __DATE__ " " __TIME__ "\n"));
DEBUG_printf("[LOOP] I send integer numbers (e.g. %d), float numbers (%3.2f), hex numbers (0x%X) and strings (%s) to the serial port.\n", 1, 2.2, 3*3*3, "Four");