How can I print to the serial monitor a string or just single character followed by a variable like "L 55"
asked Mar 9, 2015 at 18:01
3 Answers 3
int Var = 55;
//Do it in 2 lines e.g.
Serial.print("L "); // String
Serial.println(Var); // Print Variable on same line then send a line feed
answered Mar 9, 2015 at 18:51
For debug printing, you can define a macro to print both the name and value of a variable like this:
#define PRINTLN(var) Serial.print(#var ": "); Serial.println(var)
which you then use like this:
int x = 5;
PRINTLN(x);
// Prints 'x: 5'
Also this is nice:
#define PRINT(var) Serial.print(#var ":\t"); Serial.print(var); Serial.print('\t')
#define PRINTLN(var) Serial.print(#var ":\t"); Serial.println(var)
when used in a loop like so
PRINT(x);
PRINT(y);
PRINTLN(z);
prints an output like this:
x: 3 y: 0.77 z: 2
x: 3 y: 0.80 z: 2
x: 3 y: 0.83 z: 2
Thanks a lot for your answers. I made this ...
#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);
won't work as intended. The standard fix is to#define VSayLn(var) do { Serial.print(#var " =\t"); Serial.println(var); } while (0)
. C.f. Why use apparently meaningless do-while and if-else statements in macros?Edgar Bonet– Edgar Bonet2018年01月09日 08:51:22 +00:00Commented Jan 9, 2018 at 8:51
Serial.print
.