The String "115.22" has 6 characters. You allocate space to hold 6 characters Then, you tell the String object to fill the array that can hold 6 characters. Since the String class knows, but you apparently don't, that strings need to be NULL terminated, only "115.2" is extracted.
Why are you duplicating the String? Why are you adding 0 to a String? What the hell does that accomplish? I mean, think about it. A String is an object. How can you add an integer to an object? What is elephant + 7?
I got the StringtoInt function from (I think) the Arduino forum some time ago. It worked fine until I decided to not parse \n (which i use to signal end of message) into the String.
I don't get why the function stopped working. I thought the sting might no longer be terminated propperly.
Adding 0 was my try to to add the null termination
Dez Hex Okt ASCII
0 0x00 000 NUL
Obviously not correct but .. the function worked again.
From what you stated:
Then, you tell the String object to fill the array that can hold 6 characters. Since the String class knows, but you apparently don't, that strings need to be NULL terminated, only "115.2" is extracted.
most likely because the array got big enough.
Why are you duplicating the String?
You mean this?: String toconvert=convert;
Because I didn't change the function I got.
What the hell does that accomplish? I mean, think about it. A String is an object. How can you add an integer to an object? What is elephant + 7?
How do I correctly add the null termination? + NULL?
only "115.2" is extracted
So why do I then get 115.00 instead of 115.2 when using the function?
To what? The String is already NULL terminated. The toCharArray() method handles the NULL termination of the string.
So why do I then get 115.00 instead of 115.2 when using the function?
You get 115.00 because the float is printed to two decimal places. As to why the decimal part is lost, you have many places in the code that need fixing. Fix them, and then print stuff as you go, to see where the String, string, or float become incorrect.
I'm going to go out on a limb here and guess that, like compilation issues, something that is incorrect now, when fixed, will correct the issue that is occurring later.
Thanks a lot for the lesson. The main issue was that I didn't change the type of the return value when changing the function from StringtoInt into StringtoFloat.
So instead of "float convertStringtoFloat(String convert)" I used "int convertStringtoFloat(String convert)". Did you see this?
With all the print for debugging I found it.
In any case cleaning up the functions by removing the completely unnecessary +0 and the added overhead by doubling the String made things cleaner and less error prone.
Sometimes being only result orientated kicks back. I’ll dig more into functions I get from somewhere else next time.
The functions are now:
int convertStringtoInt(String convert){
char test_as_char[convert.length()];
convert.toCharArray(test_as_char, convert.length());
int F = atoi(test_as_char);
return F;
}
float convertStringtoFloat(String convert){
char test_as_char[convert.length()];
convert.toCharArray(test_as_char, convert.length());
float F = atof(test_as_char);
return F;
}
Your arrays are still to small. You are not allowing room for the NULL terminator. The toCharArray() method will put the NULL in the array, but YOU need to allow room for it.
And, yes you have to have the correct return type, and I missed that.