Single line #define to disable code?

I did some thinking. I want to be able to disable all Serial.print:s in my code.

Correct way:

#define USING_SERIAL
#ifdef USING_SERIAL
Serial.println("This will print");
#end if

But it is also possible to define the word Serial

#define MYSER Serial
MYSER.println("This will also print");

My question is if there is a way to #define MYSER so the whole Serial.print will never be compiled?

Or even better, is there a way to define-away Serial?

Here's my debug system:

#define DEBUG true  //set to true for debug output, false for no debug output
#define DEBUG_SERIAL if(DEBUG)Serial

Then use debug output like this:

DEBUG_SERIAL.println("Some debug output");

When DEBUG is set to false, the compiler will optimize the calls using DEBUG_SERIAL out of the code because it knows they will never run.

If you want your program to wait for Serial Monitor to be opened before running when using native USB boards (e.g., Leonardo, Micro, MKR, Nano 33 IoT), add this line:

#if DEBUG == true
  while(!Serial);
#endif  //DEBUG == true

In the rare case where your debug system needs to read serial input, you would use similar code:

#if DEBUG == true
  if(Serial.available()) {
    x = Serial.read();
  }
#endif  //DEBUG == true

This can easily be extended to allow multiple levels of debug output, still with no overhead when it's disabled:

#define DEBUG_ERROR true
#define DEBUG_ERROR_SERIAL if(DEBUG_ERROR)Serial

#define DEBUG_WARNING true
#define DEBUG_WARNING_SERIAL if(DEBUG_WARNING)Serial

#define DEBUG_INFORMATION true
#define DEBUG_INFORMATION_SERIAL if(DEBUG_INFORMATION)Serial

void setup() {
  Serial.begin(9600);
  while (!Serial);
  DEBUG_ERROR_SERIAL.println("This is an error message");
  DEBUG_WARNING_SERIAL.println("This is a warning message");
  DEBUG_INFORMATION_SERIAL.print("The state of pin 5 is ");
  DEBUG_INFORMATION_SERIAL.println(digitalRead(5) ? "HIGH" : "LOW");
  Serial.println("This is standard program output");
}

void loop() {}

You can use the boolean debug macros to switch on and off other parts of your code too.

Using Serial.begin() will cause a bunch of the serial code and memory to get included in your program, even if you never call Serial.print()
So you'll have to figure out how to conditionalize that as well.

I left Serial.begin() unmodified in the final code above because that was a demonstration of using debug output in addition to standard Serial output. If you only were using Serial for debug output, you would use the debug system for the Serial.begin() call as well.

DEBUG_SERIAL.begin(9600);

or if using multiple levels of debug output:

  #if DEBUG_ERROR == true || DEBUG_WARNING == true || DEBUG_INFORMATION == true
  Serial.begin(9600);
  #endif  // DEBUG_ERROR == true || DEBUG_WARNING == true || DEBUG_INFORMATION == true

THANK YOU!

This was exactly what I was longing for. Made it a bit shorter, and less readable, but I a lazy person. :wink:

#define USE_SERIAL false
#define DEBUG_ERROR true
#define DEBUG_WARNING false
#define DEBUG_INFO false
#define DEBUG_NOTICE false
#define S if(USE_SERIAL)Serial
#define DEBUG_E if(DEBUG_ERROR)Serial
#define DEBUG_W if(DEBUG_WARNING)Serial
#define DEBUG_I if(DEBUG_INFO)Serial
#define DEBUG_N if(DEBUG_NOTICE)Serial

void other_setup() {
#if USE_SERIAL == true || DEBUG_ERROR == true || DEBUG_WARNING == true || DEBUG_INFO == true || DEBUG_NOTICE == true
  Serial.begin(115200);
  while (!Serial);
  Serial.println("\n\nMQTT-client " MQTT_CLIENTID " compiled " __DATE__ " " __TIME__);
#endif  // USE_SERIAL == true || DEBUG_ERROR == true || DEBUG_WARNING == true || DEBUG_INFO == true || DEBUG_NOTICE == true
  SPIFFS.begin();
  relay(0); // By default - go auto
  ota_setup();
}

teddyz:

void other_setup() {

#if USE_SERIAL == true || DEBUG_ERROR == true | DEBUG_WARNING == true || DEBUG_INFO == true || DEBUG_NOTICE == true
  Serial.begin(115200);
  while (!Serial);
  Serial.println("\n\nMQTT-client " MQTT_CLIENTID " compiled " DATE " " TIME);
#endif  // USE_SERIAL == true || DEBUG_ERROR == true || DEBUG_WARNING == true || DEBUG_INFO == true || DEBUG_NOTICE == true
  SPIFFS.begin();
  relay(0); // By default - go auto
  ota_setup();
}

I thought #if #endif or #ifdef could only be used as preprocessor statements.

Is it possible to used them anywhere in the sketch?

moserroger:
Is it possible to used them anywhere in the sketch?

Yes.