Answers:
对于调试打印,您可以定义一个宏来打印变量的名称和值,如下所示:
#define PRINTLN(var) Serial.print(#var ": "); Serial.println(var)
然后您可以这样使用:
int x = 5;
PRINTLN(x);
// Prints 'x: 5'
这也很好:
#define PRINT(var) Serial.print(#var ":\t"); Serial.print(var); Serial.print('\t')
#define PRINTLN(var) Serial.print(#var ":\t"); Serial.println(var)
当像这样循环使用时
PRINT(x);
PRINT(y);
PRINTLN(z);
打印这样的输出:
x:  3   y:  0.77    z:  2
x:  3   y:  0.80    z:  2
x:  3   y:  0.83    z:  2
非常感谢你的回答。我做的 ...
#define DEBUG  //If you comment this line, the functions below are defined as blank lines.
#ifdef DEBUG    //Macros 
  #define Say(var)    Serial.print(#var"\t")   //debug print, do not need to put text in between of double quotes
  #define SayLn(var)  Serial.println(#var)  //debug print with new line
  #define VSay(var)    Serial.print(#var " =\t"); Serial.print(var);Serial.print("\t")     //variable debug print
  #define VSayLn(var)  Serial.print(#var " =\t"); Serial.println(var)  //variable debug print with new line
#else
  #define Say(...)     //now defines a blank line
  #define SayLn(...)   //now defines a blank line
  #define VSay(...)     //now defines a blank line
  #define VSayLn(...)   //now defines a blank line
#endif
if (some_condition) VSayLn(some_var);将无法按预期工作。标准解决方法是#define VSayLn(var) do { Serial.print(#var " =\t"); Serial.println(var); } while (0)。cf 为什么在宏中使用看起来毫无意义的do-while和if-else语句?
                    
Serial.print。