Debug Macro Not Working

//I wrote simple program to test debug macros, but it gives me compilation error

//Program:

#include <Arduino.h>

#define DEBUG

#ifdef DEBUG
  #define DPRINT(...)    Serial.print(__VA_ARGS__)
  #define DPRINTLN(...)  Serial.println(__VA_ARGS__)
#else
  // define blank line
  #define DPRINT(...)
  #define DPRINTLN(...)
#endif


void setup() {
  // put your setup code here, to run once:
  String str ="Hello MrNams";
  DPRINT("Here is my string %s",str);
}

void loop() {
  // put your main code here, to run repeatedly:
}

//Compilation Error:
src/main.cpp: In function 'void setup()':
src/main.cpp:7:50: error: no matching function for call to 'HardwareSerial::print(const char [21], String&)'
#define DPRINT(...) Serial.print(VA_ARGS)
^
src/main.cpp:19:3: note: in expansion of macro 'DPRINT'
DPRINT("Here is my string %s",str);
^~~~~~
In file included from C:/Users//.platformio/packages/framework-arduinoespressif32/cores/esp32/Stream.h:26,
from C:/Users//.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:177,
from src/main.cpp:2:
C:/Users//.platformio/packages/framework-arduinoespressif32/cores/esp32/Print.h:81:12: note: candidate: 'size_t Print::print(const __FlashStringHelper*)'
size_t print(const __FlashStringHelper *);
^~~~~
C:/Users//.platformio/packages/framework-arduinoespressif32/cores/esp32/Print.h:81:12: note: candidate expects 1 argument, 2 provided
C:/Users//.platformio/packages/framework-arduinoespressif32/cores/esp32/Print.h:82:12: note: candidate: 'size_t Print::print(const String&)'
size_t print(const String &);

Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

You are abusing the macro definition :frowning:

You need to set the baud rate too.

#include <Arduino.h>

#define DEBUG

#ifdef DEBUG
#define DPRINT(...)    Serial.print(__VA_ARGS__)
#define DPRINTLN(...)  Serial.println(__VA_ARGS__)
#else

// define blank line
#define DPRINT(...)    
#define DPRINTLN(...)
#endif


void setup()
{
  Serial.begin(9600);
  
  // put your setup code here, to run once:
  String str = "Hello MrNams";
  DPRINT("Here is my string ");
  DPRINTLN(str);
}

void loop()
{
  // put your main code here, to run repeatedly:
}

consider


char s [80];

int debug = 1;
int state;

// -----------------------------------------------------------------------------
void
funcA (void)
{
    int var = 123;

    if (debug & 2)  {
        sprintf(s, "%s: %d\n", __func__, var);
        Serial.print(s);
    }
}

// -----------------------------------------------------------------------------
void
funcB (void)
{
    if (debug & 1)  {
        sprintf(s, " %s:", __func__);
        Serial.println(s);
    }
}

// -----------------------------------------------------------------------------
void
funcC (void)
{
    if (debug & 1)  {
        sprintf(s, "  %s:", __func__);
        Serial.println(s);
    }
}

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin (9600);
}


void loop()
{
    switch (state++)  {
    case 0:
        funcA ();
        break;

    case 1:
        funcB ();
        break;

    case 2:
        funcC ();
        state = 0;
        debug++;
        break;
    }

    delay (500);
}

some output

 funcB:
  funcC:
funcA: 123
funcA: 123
 funcB:
  funcC:
 funcB:
  funcC:
funcA: 123
funcA: 123
 funcB:
  funcC:
 funcB:

I use platformio and VS Code and it shows macro expansion correct i.e.
Below code

  DPRINT("Here is my string %s",str);

is expanded to

  Serial.print("Here is my string %s",str);

put that line in a program and compile it for the Arduino uno and see what will happen

Same error, so issue is not MACRO expansion.
one thing is that it was working well, before i make it bit complicated like added firebase library for esp32, software serial library and it started failing.

Which Arduino board are you using ?

ESP32 Dev Board V1

Then you can use Serial.printf() to include text, variables and formatting all in a single statement if you want but you must get the format right

1 Like

Thank you all for valuable inputs, I am closing this top from my side.

The normal Serial.print() function only accepts a single argument (and occasionally a modifier.) You can do Serial.print(myNumber, HEX), but not Serial.print("Numer is", myNumber)

Serial.printf() (already mentioned) behaves more like the traditional C printf()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.