8

How can I print to the serial monitor a string or just single character followed by a variable like "L 55"

Root0x
  • 205
  • 1
  • 3
  • 7

3 Answers3

10
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
Bra1n
  • 1,028
  • 1
  • 7
  • 12
1

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
Tim MB
  • 111
  • 3
1

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
Dilby
  • 11
  • 1