Pages: [1]   Go Down
 Author Topic: Wrong sign when converting  (Read 457 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
 « on: November 25, 2008, 04:33:09 am » Bigger Smaller Reset

I have a temp sensor (LM335A) connected to analog pin 0. When converting the values to degress Celsius I got wrong sign if the temperature is -0,xx. For example:
-1,xx will be displayed as -1,xx  (OK)  but
-0,xx  will be displayed as 0,xx   (wrong, missing - sign when temp is 0,  something)
I have picked up the code from different examples in Arduinos examples, but because of lack of skills in this area I can't find the error in the code:

// *************************************************************************************
unsigned int precision =100;
unsigned int frac;
double val;
double valr;

client.print("Analog 0: ");
val = val - 10;             // calibration of LM335
val = val * 0.48875;     // 5Volt/1023 * 100
valr = val - 273.15;      // convert Kelvin to Celsius

client.print(int(valr));  // prints the int part
client.print(",");          // print the decimal point

if(valr >= 0)
frac = (valr - int(valr)) * precision;
else
frac = (int(valr)- valr ) * precision;
int  frac1 = frac;
while( frac1 /= 10 )
precision /= 10;
precision /= 10;
while(  precision /= 10)
client.print("0");
client.print(frac,DEC) ;
client.print(" &#176C");
client.println("<br />");
 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #1 on: November 25, 2008, 05:08:02 am » Bigger Smaller Reset

try replacing:
if(valr >= 0)
frac = (valr - int(valr)) * precision;
else
frac = (int(valr)- valr ) * precision;

with

if(valr < 0.0){
client.print('-');
valr = -valr;
}
frac = (valr - int(valr)) * precision;
 Logged

0
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
 « Reply #2 on: November 25, 2008, 06:04:11 am » Bigger Smaller Reset

Now I got a - but missplaced  .... 0,-42
 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #3 on: November 25, 2008, 06:50:00 am » Bigger Smaller Reset

Can you post a version of the code that uses Serial so I can have a look and try running it?  Just replace your sensor code with a hard coded floating point value
 Logged

0
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
 « Reply #4 on: November 25, 2008, 08:11:59 am » Bigger Smaller Reset

Here it comes, you have to change the different val statement:

void setup()
{

Serial.begin(9600);

}

void loop()
{
unsigned int precision =100;
unsigned int frac;
double val;
double valr;

val = 567;                // expected result = -0,91 (actual 0,-91)
//  val = 560;                // expected result = -4,33   (actual -4,-33)
//  val = 618;                // expected result = 24,01   (actual 24,01)
val = val - 10;          // calibration of LM335
val = val * 0.48875;     // 5/1023 * 100
valr = val - 273.15;     // Kelvin to Celsius
Serial.print("Analog 0: ");
Serial.print(int(valr));  //prints the int part
Serial.print(","); // print the decimal point

if(valr < 0.0){
Serial.print('-');
valr = -valr;
}
frac = (valr - int(valr)) * precision;
int  frac1 = frac;
while( frac1 /= 10 )
precision /= 10;
precision /= 10;
while(  precision /= 10)
Serial.print("0");
Serial.print(frac,DEC) ;
Serial.print(" &#176C");

}
 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6250
Have fun!
 « Reply #5 on: November 25, 2008, 11:03:55 am » Bigger Smaller Reset

you need to print the integer part after you print the sign. try this:
Code:
void setup(){
Serial.begin(9600);
}

void loop()
{
unsigned int precision =100;
unsigned int frac;
double val;
double valr;

val = 567;                // expected result = -0,91 (actual 0,-91)
//  val = 560;                // expected result = -4,33   (actual -4,-33)
//  val = 618;                // expected result = 24,01   (actual 24,01)
val = val - 10;          // calibration of LM335
val = val * 0.48875;     // 5/1023 * 100
valr = val - 273.15;     // Kelvin to Celsius
Serial.print("Analog 0: ");
if(valr < 0.0){
Serial.print('-');
valr = -valr;
}
Serial.print(int(valr));  //prints the int part
Serial.print(","); // print the decimal point
frac = (valr - int(valr)) * precision;
int  frac1 = frac;
while( frac1 /= 10 )
precision /= 10;
precision /= 10;
while(  precision /= 10)
Serial.print("0");
Serial.print(frac,DEC) ;
Serial.print(" &#176C");
}
 Logged

0
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
 « Reply #6 on: November 25, 2008, 02:48:39 pm » Bigger Smaller Reset

It works, Thank you! ;D
 Logged

 Pages: [1]   Go Up