Go Down

### Topic: Wrong sign when converting (Read 740 times)previous topic - next topic

#### myerika

##### Nov 25, 2008, 10:33 am
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 />");

#### mem

#1
##### Nov 25, 2008, 11:08 am
try replacing:
[font=Courier New]  if(valr >= 0)
frac = (valr - int(valr)) * precision;
else
frac = (int(valr)- valr ) * precision;[/font]

with

[font=Courier New]  if(valr < 0.0){
client.print('-');
valr = -valr;
}
frac = (valr - int(valr)) * precision;[/font]

#### myerika

#2
##### Nov 25, 2008, 12:04 pm
Now I got a - but missplaced  .... 0,-42

#### mem

#3
##### Nov 25, 2008, 12:50 pm
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

#### myerika

#4
##### Nov 25, 2008, 02:11 pm
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");

}

#### mem

#5
##### Nov 25, 2008, 05:03 pm
you need to print the integer part after you print the sign. try this:
Code: [Select]
`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"); }`

#### myerika

#6
##### Nov 25, 2008, 08:48 pm
It works, Thank you!

Go Up