8

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
1
  • Read the Arduino docs for Serial.print. Commented Mar 10, 2015 at 19:52

3 Answers 3

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
answered Mar 9, 2015 at 18:51
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
answered Sep 27, 2017 at 10:16
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
answered Jan 8, 2018 at 21:40
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.