Loading...
Pages: [1]   Go Down
Author Topic: dtostrf() alternative for concatenating a float and a string  (Read 7247 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to concatenate a string and a floating point number to send to the serial monitor and the only way I can find to do it is using dtostrf(), but that seems a little clumsy.
For example (this works):
Code:
  int potValue = analogRead(pinSensor);
  float volts = 5*(float)potValue/1023;
  String strLab = "Pot Voltage = ";
  char tmp[10];
  dtostrf(volts,1,2,tmp);
  String strOut = strLab + tmp;

  Serial.println(strOut);

This doesn't work:
Code:
  int potValue = analogRead(pinSensor);
  float volts = 5*(float)potValue/1023;
  char tmp[10];
  dtostrf(volts,1,2,tmp);

  Serial.println("Pot Voltage = " + tmp);
 
  BlinkLED(pinLED,potValue,potValue);
nor does:
Code:
  int potValue = analogRead(pinSensor);
  float volts = 5*(float)potValue/1023;
  char tmp[10];
  dtostrf(volts,1,2,tmp);
  String strOut = "Pot Voltage = " + tmp;

  Serial.println(strOut);
both give the error:  invalid operands of types 'const char [15]' and 'char [10]' to binary 'operator+'

What I'd really like to do is somthing like:
Code:
  int potValue = analogRead(pinSensor);
  float volts = 5*(float)potValue/1023;

  Serial.println("Pot Voltage = " + String(Volts));
but String doesn't work with floating point numbers. 

I've looked at the String constructor tutorial, the String Addition Examples and googled this as well to no avail.  Any suggestions?
Thanks,
Jason
Logged

California
Offline Offline
Edison Member
*
Karma: 37
Posts: 1827
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any suggestions?
Use null terminated character arrays instead of Strings and use sprintf to "concatenate"
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 776
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char voltageMsg[25];
sprintf(voltageMsg, "Pot Voltage = %f", volts);
Serial.println(voltageMsg);
Logged


Netherlands
Offline Offline
Tesla Member
***
Karma: 87
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this,
Code:
int potValue = analogRead(pinSensor);
float volts = 5*(float)potValue/1023;

char tmp[25] = "Pot Voltage = ";

dtostrf(volts,1,2, &tmp[12]);

Serial.println(tmp);

The char array is declared big enough to hold the fixed string and the float.  (25)
The whole trick is to let the dtostrf add to the tmp array at the right position. (you might need to change 12 in 13 or so)

give it a try.


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
use sprintf to "concatenate"
Great idea, except that sprintf() on the Arduino does not deal with floating point values.

OP: Why do you feel it is necessary to concatenate the data before sending it?
Code:
Serial.print("Pot Voltage = ");
Serial.print(volts);
The device on the other end of the serial port will not know that two Serial calls were made to send the data.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 87
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
The device on the other end of the serial port will not know that two Serial calls were made to send the data.
For Serial you are 100% right. however if you use the print on a TCP socket, multiple calls might cause fragmentation of IP packets.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

California
Offline Offline
Edison Member
*
Karma: 37
Posts: 1827
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
use sprintf to "concatenate"
Great idea, except that sprintf() on the Arduino does not deal with floating point values.
Didn't realize that, thanks.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this,
Code:
int potValue = analogRead(pinSensor);
float volts = 5*(float)potValue/1023;

char tmp[25] = "Pot Voltage = ";

dtostrf(volts,1,2, &tmp[12]);

Serial.println(tmp);

The char array is declared big enough to hold the fixed string and the float.  (25)
The whole trick is to let the dtostrf add to the tmp array at the right position. (you might need to change 12 in 13 or so)

give it a try.




I'll give this a shot--I wonder if it'd be possible to write a wrapper function for dtostrf so that you could just use it in-line. 
Is there a reason that adding the string literal and variable (see my first post for code) doesn't work?  seems like it should from the string constructor tutorial.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 87
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


String strOut = "Pot Voltage = " + tmp;

does not work as the addition of two char[] is not defined..

String strOut = String("Pot Voltage = ") + String(tmp); 

might work. String("not tried");  smiley-wink


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to concatenate a string and a floating point number to send to the serial monitor  ...

... however if you use the print on a TCP socket ...

Which, fortunately, we don't have to worry about. smiley
Logged


Pages: [1]   Go Up
Print
 
Jump to: