Combining Serial prints ... HELP!

Hi,

I am working on a time sensitive application, and I need to cut down on the time-consuming Serial.print() function in the following code:

          distance = getDistance(Lsensor); 
          pressure = getPressure(Psensor);
          prSwitch = getPswitch(Pswitch); 
          temp = getTemperature(tmp); 
          Fldtemp = getTemperature(Fluidtmp); 

          Serial.print(1); 
          Serial.print(" ");
          Serial.print(millis());
          Serial.print(" ");
          Serial.print(distance);
          Serial.print(" ");
          Serial.print(pressure);
          Serial.print(" ");
          Serial.print(prSwitch);
          Serial.print(" ");
          Serial.print(temp); 
          Serial.print(" ");
          Serial.println(Fldtemp);

All the functions return int's.
I want to combine all of these variables into a string and spit them out on serial print in one shot.
I hope it can be done.

Thanks in advance

Take a look at sprintf. Also, see if you can run at a higher baud rate.

I am a bit confused on what sprintf() does.
could someone write a sample code combining two int variables and sending over serial?

Thanks

I am working on a time sensitive application, and I need to cut down on the time-consuming Serial.print() function in the following code:

Not sure what you're asking here; compared to the time taken to transmit the character, the time taken in the transmit routine itself is negligible.

Hi AWOL,

I need a way to send all the variables that I am sending using serial prints, in one call of serial println.
Is there a way to combine the variables that I have in a "data type" (not necessarily string) and send it via serial.println()?

Thanks

char buffer[100]; //This is where the data will be merged into. Ideally you should make this as small as you can whilst still having enough room
sprintf(buffer,"1 %ul %d %d %d %d %d", millis(), distance, pressure, prSwitch, Fldtemp); //merge together
Serial.println(buffer); //send

Basically sprintf has a set of special characters (the %_ where the _ is a letter(s)). As it works along it replaces each of these with the corresponding variable (first % is the first variable, second % is the second variable and so on).

Each of the special charaters formats the variable in a different way. For example %d takes an integer and converts it to a string of numbers. %c takes a byte and prints the corresponding ascii character (so 30 would print ' ' (space)). %ul takes an unsigned long type (which is why it is used for millis()).
There are others as well, you would have to look on the cplusplus reference (if you google sprintf() you should find it).

Note that in my example as I don't know the variable types of your numbers I have assumed that they are all bytes or integers (so %d) except for millis() which I know returns an unsigned long.

Thank you Tom for your example. And yes... all my vars are ints

I am wondering if there is space character between the variables in your example.

Thanks

I need a way to send all the variables that I am sending using serial prints, in one call of serial println.

Why?
What difference does it make?

AWOL:

I need a way to send all the variables that I am sending using serial prints, in one call of serial println.

Why?
What difference does it make?

That would be my question also. The speed of character leaving the arduino is not going to be much different no matter which method you use and I like easy to read code over more complex combined print statements.

Lefty

because it take a lot time sending the amount of data I have.
I am sampling all those variables at 1 ms period

What baud rate are you using?

But the sketch will still stall if you fill the transmit buffer, whichever approach you take.

I am using the maximum baud rate 115200.

Oh ... and how big is the transmit buffer?

Prior to 1.0, there was only the USART register as a buffer.
1.0 and after, there's a 64 byte buffer, plus the USART.

Have you bothered to work out the math for this?

at 115,200 baud, each bit is about 9 uS
A byte is 72 uS
A 4 byte int is 288 uS

Even if you usually send less than the full 4 bytes worth, it looks like there is no way to send all your data in less than a millsecond.
(I did not even include start bits!)
It might be a bit faster to send the data as hex bytes to cut down the conversion time, but that is probably not your problem.

Just for the sake of interest, the method using multiple print() calls is faster than the sprintf() method (in one simple test).

For a loop of 100 calls to printing the data for each, the results are:
multiple print()'s : 210556 us
sprintf() : 210972 us

That is at 115200 baud and the same group of calls as in that snippet of code you posted.

For a loop of 10000 calls to printing the data for each, the results are:
multiple print()'s : 23378412 us
sprintf() : 22798020 us

Interesting, sprintf() seems to have recovered. I think the first test may have been biased as for the first few iterations sprintf() had more to print as micros() had a larger value (more decimal digits)

But yeah as has been pointed out:
115,200 baud = 11520 bytes per second = 11.52 bytes per millisecond, not including the time it takes to get the data and load it into the buffer etc.