8

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

  • Read the Arduino docs for Serial.print. – jfpoilpret Mar 10 '15 at 19:52
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
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.