[SOLVED]Function return String : loops over Setup !

Hi,
I have a strange behaviour : i’ve just written a small function that reads a TMP36 sensor value and returns the result as a String.
When doing this, it seems that the setup() function is called each time the main loop() function iterates ??
Here is my code :

#include <SPI.h>

void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);  
  Serial.println("Setup finished !");
}

String getTMP36()
{
  char c_tempC_TMP36[5];
  int valeur = analogRead(0); 
  float tension = valeur * 3.3;
  tension /= 1024.0;
  float temperature = ((tension * 1000) - 500) / 10;
  dtostrf(temperature, 5, 2, c_tempC_TMP36);
  return String(c_tempC_TMP36);
}

void loop()
{
  String TMP36_val = getTMP36();
  Serial.println("TMP36 : " + TMP36_val);
  delay(1000);
}

Here is the Serial output :

Setup finished !
TMP36 : 19.61
Setup finished !
TMP36 : 19.61
Setup finished !
TMP36 : 19.61
Setup finished !
TMP36 : 19.61
Setup finished !
TMP36 : 19.61
Setup finished !
ETC ETC......

Any idea about what’s wrong ?

Regards,

Any idea about what’s wrong ?

Besides your use of the String class? getTMP36() does not suggest to me that it is going to return a String. I’d actually expect it to return a float.

You are, in that function, trying to write 5 characters AND a trailing NULL into a 5 element array. That won’t work.

Thanks for this answer. I agree that my getTMP36() function should return a float, but for some reason i have to get this value in String format.

I've found my error thanks to you : I've just replaced

char c_tempC_TMP36[5];

by

char c_tempC_TMP36[6];

to give a place for the trailing NULL (i've forget about it) :)

kris92: I agree that my getTMP36() function should return a float, but for some reason i have to get this value in String format.

If you need to use the hated String class then return the value as a float and then convert it to a String if you want within the code which outputs it. It's not nice to pass String objects around by value.

Well, thanks, i will follow your advice, writing a function convertFloat2String(float myfloat){...} to output something like this :

EthernetClient.print("jsoncallback({tmp36:\"" + convertFloat2String(TMP36_float_val) + "\"});");

...event if i don't really understand why it would be "better"...and why Strings are so hated....

You don't need a function - just construct a String from the float value in the way you did before - but do it after the value has been returned rather than before.

String objects hold the content in a dynamic buffer allocated from the heap, and each time you copy the String object or change its value that involves allocating a new buffer and copying the content across and then releasing the old buffer. When you assign the return value of the function to a local String object you are actually making another copy of the String. This is very inefficient, and also increases the amount of heap allocation and deallocation which increases the memory required and increases the risk of heap fragmentation. Use of dynamic (heap) memory is a bad idea on an Arduino due to the extremely limited heap space and the extremely limited capability to manage that heap space so it's best to avoid String altogether, but if you must use it then it's best to avoid doing things that copy and modify String objects a lot.

Thanks a lot ! it's more clear now :) I have no more Strings in my code. I return my sensor value as Float, and i am able to return a float to the EthernetClient !

and i am able to return a float to the EthernetClient

That's so much better than converting to a string, then wrapping that string in a String, then unwrapping the String to get the string.