Foreign unicode characters being returned.

Hello All,

I have some code that at times will return foreign unicode characters.
Now when I print the variable indivially, I get the correct values eg

I request a temperature from my DS18B20 which is returned as a float variable
I then request the distance from my HC-SR04 which is returned as a float variable

If I serial print the temperature float variable (code below) I get the temp printed eg 24.31

Serial.println(g_Temp);

If I serial print the distance float variable (code below) I get the distance printed eg 13.22

Serial.println(g_Dist);

However when I combine the two variables into a new variable (code below) for writing to my sd card I "SOMETIMES" get these foreign characters part way through eg

g_Data_For_SD = String(g_Temp) + "," + String(g_Dist);

returns
24.31,13äa

Now I stress that this happens approx 50% of the time. Does anyone know why??

Does anyone know why??

You used the String class, but did not show ALL of your code. There is NO reason to make one String that needs to be unpacked to be printed. The receiver has NO idea how many Serial.print() statements were used. So, quit wasting resources on the String class.

Thank you PaulS, however I am a little confused with your reply. I am obviously doing it all in incorrect way and wasting resources as you say. If you can help me I would be grateful.

I am collecting data from various sensors as float variables and then writing these all to a .CSV file on the SD Card. To write the floats to the csv file I am creating a new string variable to join all the values along with commas as separators.

g_Data_For_SD = String(g_Temp) + "," + String(g_Dist);
if (dataFile) {
    dataFile.println(g_Data_For_SD);
    dataFile.close();
    Serial.println(g_Data_For_SD);
}

The serial.println is for outputing the float variable to the Serial Plotter.

Could you shed a light on what you think I should be doing to improve the code/resources.

How do I create a string containing 2 float variables along with commas to be stored on an SD card within a csv file

Thanks

PaulS:
You used the String class, but did not show ALL of your code.

As requested, please post all your code.

SniffTheGlove:
How do I create a string containing 2 float variables along with commas to be stored on an SD card within a csv file

Just print the individual strings straight out to the file, no need to build one big String.
eg.

dataFile.print(g_Temp);
dataFile.print(",");
dataFile.println(g_Dist);

Just print the individual strings values straight out to the file