The real flaw is testing if Serial is available all the time. For one thing, that wouldn't work on a Uno etc. How about something like this?
// make true to debug, false to not
#define DEBUG true
// conditional debugging
#if DEBUG
#define beginDebug() do { Serial.begin (115200); while (!Serial) { } } while (0)
#define Trace(x) Serial.print (x)
#define Trace2(x,y) Serial.print (x,y)
#define Traceln(x) Serial.println (x)
#define Traceln2(x,y) Serial.println (x,y)
#define TraceFunc() do { Serial.print (F("In function: ")); Serial.println (__PRETTY_FUNCTION__); } while (0)
#else
#define beginDebug() ((void) 0)
#define Trace(x) ((void) 0)
#define Trace2(x,y) ((void) 0)
#define Traceln(x) ((void) 0)
#define Traceln2(x,y) ((void) 0)
#define TraceFunc() ((void) 0)
#endif // DEBUG
long counter;
unsigned long start;
void setup() {
start = micros ();
beginDebug ();
Traceln (F("Commenced debugging!"));
TraceFunc (); // show current function name
} // end of setup
void foo ()
{
TraceFunc (); // show current function name
}
void loop()
{
counter++;
if (counter == 100000)
{
Traceln (F("100000 reached."));
Trace (F("took "));
Traceln (micros () - start);
counter = 0;
foo ();
} // end of if
} // end of loop
That only does the Serial.begin() once, and the test for waiting for Serial to become available once.
Then, depending on the DEBUG define, the debugging code is executed or not. The fancy ((void) 0)
calls get optimized away completely by the compiler if DEBUG is false, thus making them have no effect if debugging is off (which wouldn't apply to your code).
(Edited to add)
As you can deduce from my macros I wanted to debug IF the serial port is connected. I don't want to wait for it to be connected.
Well it wasn't immediately obvious from your code. In that case I suggest you test in the main loop
function every second or so, to see if Serial
is true. If so, set a flag. Test that flag in your debug function. That way you aren't slowing down every single Serial.print
by 10 ms.
Like this amended code:
// make true to debug, false to not
#define DEBUG true
// conditional debugging
#if DEBUG
#define beginDebug() do { Serial.begin (115200); } while (0)
#define Trace(x) do { if (serialConnected) Serial.print (x); } while (0)
#define Trace2(x,y) do { if (serialConnected) Serial.print (x, y); } while (0)
#define Traceln(x) do { if (serialConnected) Serial.println (x); } while (0)
#define Traceln2(x,y) do { if (serialConnected) Serial.println (x, y); } while (0)
#define TraceFunc() do { if (serialConnected) { Serial.print (F("In function: ")); Serial.println (__PRETTY_FUNCTION__);} } while (0)
#else
#define beginDebug() ((void) 0)
#define Trace(x) ((void) 0)
#define Trace2(x,y) ((void) 0)
#define Traceln(x) ((void) 0)
#define Traceln2(x,y) ((void) 0)
#define TraceFunc() ((void) 0)
#endif // DEBUG
const byte LED = 13;
bool serialConnected;
unsigned long timeSerialLastTested;
long counter;
unsigned long start;
void setup() {
start = micros ();
beginDebug ();
Traceln (F("Commenced debugging!"));
TraceFunc (); // show current function name
pinMode (LED, OUTPUT);
} // end of setup
void foo ()
{
TraceFunc (); // show current function name
}
void loop()
{
// every second, see if the Serial port is available
if (millis () - timeSerialLastTested >= 1000)
{
timeSerialLastTested = millis ();
serialConnected = Serial;
digitalWrite (LED, !digitalRead (LED)); // blink LED
}
counter++;
if (counter == 100000)
{
Traceln (F("100000 reached."));
Trace (F("took "));
Traceln (micros () - start);
counter = 0;
foo ();
} // end of if
} // end of loop