Go Down

Topic: Correct format to Serial.print a string of mixed data type? (Read 61742 times) previous topic - next topic

nychelp

Okay so why does this return garbage:

Code: [Select]
String toprint = "";
 toprint =  String(micros() + "," + sensorValue);


But this works just fine:
Code: [Select]
String toprint = "";
 toprint += micros();  
 toprint += ',';
 toprint += sensorValue;


micros() returns an unsigned long, "," is a string, and sensorValue is an int.  But the first block of code is simpler and seems like it should work?

MikMo

In the first sample you are using ",", in the second you are using ','

could that be the reaon ?

According to this:

http://arduino.cc/en/Tutorial/StringAdditionOperator

 // adding a constant character to a string:
 stringThree =  stringOne + 'A';

 // adding a constant string to a string:
 stringThree =  stringOne +  "abc";

nychelp

Good eye! If I change to ',' in the first code posted, I don't get garbage anymore, but the output only shows the micros() value - no comma or sensor reading.  Any thoughts?

dxw00d

Perhaps you should read http://arduino.cc/en/Reference/StringConstructor. There is no version that allows you to concatenate a number with a string.

MikMo

From the same page linked above:

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

int sensorValue = analogRead(A0);
  String stringOne = "Sensor value: ";
  String stringThree = stringOne + sensorValue;
  Serial.println(stringThree);
[Get Code]

results in "Sensor Value: 402" or whatever the analogRead() result is, but

int sensorValue = analogRead(A0);
  String stringThree = "Sensor value: " + sensorValue;
  Serial.println(stringThree);
[Get Code]

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.

nychelp

thanks dxw00d, I did. But here is shows that you can:
http://arduino.cc/en/Tutorial/StringAdditionOperator

dxw00d

Yes, you can concatenate a String and a number, but not using the String() constructor, as you were trying here:

Code: [Select]
toprint =  String(micros() + "," + sensorValue);

nychelp

thanks MikMo. but i don't think those comments apply to me, because in each case, i am initializing the string before concatenating, right?

nychelp

Okay dxw00d, so do you think the second code block I posted is the best way to do this?

PaulS

Quote
so do you think the second code block I posted is the best way to do this?

You haven't explained WHY you want to concatenate the data as a (very inefficient) way of collecting all the data in a wrapped char array, so, how can we tell you that it is the best way? Most likely, it either isn't or doesn't need to be done at all.
The art of getting good answers lies in asking good questions.

nychelp

Oh okay sorry PaulS, I'm trying to find the fastest way to print to a file, and wondering if a method using strings is faster than printing each line separately like this:

Code: [Select]
Serial.print(micros());   
  Serial.print(",");   
  Serial.println(sensorValue);   


I need to collect data fast and am wondering what the speed limit of the Arduino is for this.  Thanks!

PaulS

Quote
I'm trying to find the fastest way to print to a file, and wondering if a method using strings is faster than printing each line separately like this:

No, it is not. The end result is the same number of bytes that need to be sent to the serial port. Converting the numbers to characters imposes some unavoidable overhead. Allocating and freeing the memory needed to wrap those characters in a String instance adds more (avoidable) overhead.
The art of getting good answers lies in asking good questions.


Eckehard

Hy there,

there is a pretty cool way to use the String Object type inside the print statement:


Serial.println((String)"Hello "+3.2+' '+"World");


As you see: anything mixed up in one line, but it works. Why?

The reason is the way, C++ uses type casts. The first argument is casted from char * to String Object. Before the next operator is added, it is casted also to "String" and so on.

best regards, Eckehard


lastchancename

If your goal is fast & efficient, I'd completely drop the String class.

Then once I have th char[] arrays in hand, I'd use Serial.write() instead of Serial.print() - to remove a layer of unnecessary Arduino fluff.

Learn about C 'string' handling and processing* with char[] arrays

*not the dinky pseudo-language
Q: How many searches did you make before posting this question?      A: none
At the very least, take a guess at the solution, then we can help move forward from what you know already.

Go Up