Arduino playing God...


hey, because of not being able return array function, decided merge 2 different values 1 , return instead.
those values temperature , humidity.
basically, convert temperature , humidity ints (they're floats 2 decimal places) multiplying them 100 , multiply temperature 106 , add humidity.
   eg: temperature = 18.12Âșc
         humidity = 83.10%
         result = 18128310


here's code:
code: [select]

for (int i=0; < 5; i++) {
 receivedtemperature[i] = serial.read();
}
   
for (int i=0; < 5; i++) {
 receivedhumidity[i] = serial.read();
}



float temp, hum;
if (receivedtemperaturesign == '+') {
 temp = atof(receivedtemperature);
}
if(receivedtemperaturesign == '-') {
 temp = -1 * atof(receivedtemperature);
}

hum = atof(receivedhumidity);

unsigned long returningvalue = temp*1000000 + hum*100;

serial.print("temp: "); serial.println(temp);
serial.print("hum: "); serial.println(hum);
serial.print("returningvalue: "); serial.println(returningvalue);
serial.print("temp*10^6: "); serial.println(temp*1000000);
serial.print("hum*100: "); serial.println(hum*100);



my input: 18.1283.10 [temperature = 18.12   ,   humidity = 83.10]
the output:
temp: 18.1[glow]3[/glow]
hum: 83.10
returningvalue: 18136610
temp*10^6: 1812[glow]83[/glow]00.00
hum*100: 8310.00



i don't know why, seems that, while serial.reading, receivedtemperature array appending part of humidity input.
but: tried printing receivedtemperature[6] , gave me nothing.



what's wrong code? :/

the problem assuming floats store numbers fixed precision decimals.  the issue bit long talk in forum post should read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

simply (and imprecisely put) getting more sigfigs think.  just 1/3 can't represented in finite number of digits in decimal, decimal numbers expanded out in floats.

if want pack these numbers 1 long should cast them integers first

eg
code: [select]

unsigned long returningvalue = ((int)temp*100)*10000 + (int)(hum*100);


that said, still not great way it.  the best thing put temp , hum array

eg
code: [select]

void returntwothings (float *things) {
   float thing1 = getthing();
   float thing2 = getthing();
   things[0] = thing1;
   things[1] = thing2;
   return;
}


and caller should like:
code: [select]


//....
float values[2];
returntwothings(values);

//....



after these 2 lines values has thing1 , thing2.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Arduino playing God...


arduino

Comments