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.
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()?
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.
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.
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.