Hey, because of not being able to return an array from a function, I decided to merge two different values into one and return that instead.
Those values are Temperature and Humidity.
Basically, I convert the Temperature and Humidity into ints (they're floats with 2 decimal places) by multiplying them by 100 and then I multiply the Temperature by 106 and then add the Humidity. Eg: Temperature = 18.12ºC
Humidity = 83.10%
Result = 18128310
My input: 18.1283.10 [Temperature = 18.12 and Humidity = 83.10] The output:
Temp: 18.13
Hum: 83.10
returningValue: 18136610
Temp10^6: 18128300.00
Hum100: 8310.00
I don't know why, but it seems that, while Serial.reading, the receivedTemperature array is also appending part of the Humidity input.
BUT: I tried printing receivedTemperature[6] and it gave me nothing.
The problem is that you are assuming that floats store numbers as fixed precision decimals. The issue is a bit long to talk about in a forum post but you should read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html
simply (and imprecisely put) you are getting more sigfigs than you think. Just like 1/3 can't be represented in a finite number of digits in decimal, some decimal numbers get expanded out in floats.
IF you want to pack these numbers into one long you should cast them to integers first
eg
unsigned long returningValue = ((int)Temp*100)*10000 + (int)(Hum*100);
that said, its still not a great way to do it. the best thing to do is to put temp and hum into an array
Thanks a lot for both the incredible link and the last code block: it works perfectly.
Now, if I wanted to return values of different types (like an int, a float, a char and a boolean), could I still use that last code block of yours? I don't think so...
And is there any possible way for me to get 18.12 instead of 18.13? Like, I don't know, keeping only the two decimal places and "eliminating" the rest while NOT ROUNDING the last decimal place?
Yes, yes: the function works perfectly! The only problem is that with my Arduino the returned value of the Temperature is 0.01 higher than the inputted Temperature. I subtracted those 0.01 and now the inputs and outputs match (go figure..).
I just wanted to know if it was possible to return more than one type at the same time... I'm still a newb, so sorry if this question seems trivial.. I just learned from the code you posted before what the '__*__var' actually meant.. So yeah..
Of course, the arguments do not need to be hardcoded values.
The only drawback to this approach is that the compiler does not properly generate function prototypes for functions with reference arguments, but the function can be defined before it is called, and the prototype is not needed.