Go Down

Topic: How to print two variable on the serial monitor (Read 11468 times) previous topic - next topic

liabilityquek

Hi peeps, how do i print two variables on the serial monitor. For instances i would like to print a voltage and temperature value on the same line.
Is there any way to println(volt,temp) together? Plus i need to print voltage as DEC, so is there any way to indicate volt as DEC in the earlier process?

retrolefty

#1
Nov 22, 2010, 09:38 am Last Edit: Nov 22, 2010, 09:43 am by retrolefty Reason: 1
Quote
how do i print two variables on the serial monitor. For instances i would like to print a voltage and temperature value on the same line.
Is there any way to println(volt,temp) together? Plus i need to print voltage as DEC, so is there any way to indicate volt as DEC in the earlier process?


There is a way to do that. The basic problem is you can't just use println() statements where you want to do multiple writes to the same line, you would rather use print() statements instead and only use println() for the last item to be printed for the message line. You in effect have to build your line message using as many print() statements as necessary including adding spaces and fixed labels, etc, and then finish off the line message with a final println() statement.

I'm not quite clear about your second question? Read the following reference pages for printing to see if you can figure it out.

http://arduino.cc/en/Serial/Print
http://arduino.cc/en/Serial/Println

Lefty


robtillaart

#2
Nov 22, 2010, 04:10 pm Last Edit: Nov 22, 2010, 04:10 pm by robtillaart Reason: 1
YOu could create you own function to do that;

Code: [Select]

// not compiled or tested
Void MyPrintln(float volts, float temp)
{
 Serial.print(volts,2);
 Serial.print(" VOLTS  ");
 Serial.print(temp,2);
 Serial.print(" Celsius ");
 Serial.println();
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

dglstrp

Interesting, in another language I program in we can print text and the variable in the same statement, like this

print("explanation %s more text %s", SerialData1, SerialData2);

The %s is for serial data, %d would be for digital data.

Nothing like that for the Arduino though?

Coding Badly


There are (at least) two problems with having that much flexibility...

The first is the amount of memory needed for the code.  sprintf (+Serial.print) is a reasonable alternative to a flexible print.  It adds ~1700 bytes to the program size.  If the Arduino folks had included a flexible print, the users that didn't need it would lose about 1700 bytes of program space (5.2% on a '328 processor).  That's a fairly high price to pay.  I certainly do not want to pay that price.

Second, there are dangers with the C-print family.  I've been programming in C(++) for ... well ... let's just say a while and I still occasionally get a printf format wrong.  From having done it so many times, I know how to debug that kind of mistake.  How would a new user fare?  Would they be able to determine why their Sketch keeps restarting?  Or garbage is output instead of a number?

dglstrp


Go Up