Hello everybody,
the democode below makes use of what is called a macro.
A macro does pre-programmed automated keyboard typing into your source-code
just right after starting the compiling-process.
This means a macro is adding lines of code to the code you see in the editor.
very simple example you might have seen before:
#define myLED_Pin 13
The macro's name is "myLED_Pin" and what it does is replacing "myLED_Pin" with number 13
So if you code
pinMode(myLED_Pin,OUTPUT);`
the macro does
pinMode(13,OUTPUT);`
of course myLED_Pin is easier to understand than "13"
Macros can do much more. You can even use "macro-variables"
This is what the debug-macro does
Here is the democode that shows how it works. For easiest understanding read the code
upload it to your microcontroller and run the code with opened serial monitor
and then re-read the code.
Here is the code
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage dbg("fixed Text", variableName)
// example printing name and content of a variable called "myCounter"
// dbg("Demo-Text",myCounter);
// serialoutput will be '"Demo-Text" myCounter=1'
// which means this macro does three things
// 1. print the fixed text
// 2. print the NAME of the variable
// 3. print the CONTENT of the variable
// all in one line
int myCounter;
void setup() {
Serial.begin(115200);
Serial.print( F("\n Setup-Start \n") );
myCounter =0;
}
void loop() {
myCounter++;
dbg("Demo-Text",myCounter);
// whenever possible replace delay()
// by non-blocking timing based on millis()
delay(1000);
}
// serial output will look like this
/*
Setup-Start
"Demo-Text" myCounter=1
"Demo-Text" myCounter=2
"Demo-Text" myCounter=3
"Demo-Text" myCounter=4
...
*/
The main purpose is to add such lines for
- analysing what a code is doing
- analysing what values do variables really have
- when is what if-condition true etc.
For easy finding a particular place in your code give each dbg-statement a different number in the fixed text.
This enables to mark this number in the serial monitor change to the editor and do a search for this number
example
dbg(" 27: wait",myVar58);
if the characters 27: are specific to that line of code
mark 27:
Strg-C,
change to editor
Strg-f in the IDE-editor
Strg-V
Enter
brings you right to that line of code with the 27:
Additonal info how it works in post #3
best regards Stefan