Pages: [1]   Go Down
Author Topic: How to print two variable on the serial monitor  (Read 6240 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: November 22, 2010, 03:43:00 am by retrolefty » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12456
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

YOu could create you own function to do that;

Code:
// 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();
}
« Last Edit: November 22, 2010, 10:10:56 am by robtillaart » Logged

Rob Tillaart

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

Utah
Offline Offline
Newbie
*
Karma: 0
Posts: 24
High Priced Toy Specialist
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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?
Logged

Utah
Offline Offline
Newbie
*
Karma: 0
Posts: 24
High Priced Toy Specialist
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK that makes sense,
Logged

Pages: [1]   Go Up
Jump to: