Go Down

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

myerika

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 = analogRead(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

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

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

mem

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

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

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


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy