I'm making some code for school, and is adding debug code to it. When compiling, I get the error as written in the title. This is the code in question:
#define DEBUG 1
#ifdef DEBUG
#define Serial.println(x) DEBUG_PRINT(x)
#else
#define DEBUG_PRINT(x)
#endif
And the code in my void setup, which has Serial.begin:
#ifdef DEBUG
Serial.begin(9600);
DEBUG_PRINT("Asteroid");
#endif
What am I getting wrong? I get the feeling it's my define text, but I'm unsure.
Thank you for your time Gaarden
2 Answers 2
In short: you have your macro backwards.
Macros are not formed as "Take this set of commands and call it X" but "Make this macro X and have it equate to this set of commands".
Where you have:
#define Serial.println(x) DEBUG_PRINT(x)
it should instead read:
#define DEBUG_PRINT(x) Serial.println(x)
When you call it, because the macro doesn't contain a semi-colon in it, you need to specify the semi-colon as if you were calling a function:
DEBUG_PRINT("Aardvark");
You can omit the semi-colon only if you have a semi-colon in your macro:
#define DEBUG_PRINT(x) Serial.println(x);
and then:
DEBUG_PRINT("Aardvark")
By doing it that way your "empty" macro now does become truly empty when used. Without the semi-colon in the macro you would have to use:
DEBUG_PRINT("Aardvark");
Which for the empty variant would end up as:
;
Not always desirable, and can cause some problems.
So by adding the semi-colon into the macro you can omit it from the usage of the macro, and your empty variant now becomes:
You defined the pre-processor macro the wrong way around.
Think it is like an assign statement. You come up with a name and assign something to it.
#define SHORT_NAME(ARG) some_real_function_that_has_an_awkward_long_name(ARG)
See the manual of your favourite compiler GCC for reference.
It is always a good idea to do that when using features you haven't used before. Sometimes it can be enlighting.
Explore related questions
See similar questions with these tags.
.
. Thus you cannot define a macroSerial.println(x)
. You probably wanted to define it the other way around anyway.#define DEBUG_PRINT(x) Serial.println(x)
;