Go Down

Topic: Casting Confusion (Read 454 times) previous topic - next topic

jduncan

I have some variables that I'm printing to an LCD, and also want to tweet.

I'm getting a simple float from a BMP085 using the adafruit library.
lcd.print() takes the float just fine.

I also want to construct a string to send out a tweet of the data.
I'm using http://arduino-tweet.appspot.com/ and it works great for a simple hard-coded const char.

Code: [Select]
pres = pres / 1000; //convert to mb from the default
lcd.print(pres, 1); //works great
lcd.print(" mb"); //works great
 
char pres_c = char(pres);
 
char twt = "This is my test tweet. The air pressure is" + pres_c + " mb"
 
tweetMsg(twt);


I'm getting the following errors:

Code: [Select]
firmware.ino: In function 'void getPress(float)':
firmware:222: error: invalid operands of types 'const char*' and 'const char [5]' to binary 'operator+'
firmware:224: error: invalid conversion from 'char' to 'char*'
firmware:224: error: initializing argument 1 of 'void tweetMsg(char*)'


Can someone set me on the right track? I've tried every combination/permutation that I can think of to cast these all the same.

jrdoner

I wrote a FloatToString function which permits expression of a float as a string object, with a specified number of decimal places precision.  Email me if you'd like a copy.

jrdoner@gmail.com

Arrch

Code: [Select]
char twt = "This is my test tweet. The air pressure is" + pres_c + " mb"

First, that giant string can't fit into a single character.

Second, You can't concatanate with c-strings using +. You need to use strcat() or sprintf().

Lastly, char() doesn't convert a float/int to a string. You need to use itoa() for ints or dtostrf() for floats.

Go Up