Pages: [1]   Go Down
Author Topic: SHT21 float values & dtostrf string?  (Read 2516 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I think I am making a simple mistake but just can't see it !!  Any idea why my float to string conversion is always reporting "2.0" instead of the value output from a SHT21 temp/rh sensor?

My code is:

Code:
/*
*  Read the Relative Humidity and Temperature values from SHT21 sensor
*  Clock is Analog pin 5
*  Data is Analog pin 4
*
*/

#include <Wire.h>
#include <SHT21.h>
#include<stdlib.h>

char temp_str[10];
char  rh_str[10];

float temp;
float rh;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
}

void loop()
{
  SHT21.readSensor();
 
  temp=(SHT21.temp, 2);
  rh=(SHT21.humi, 2);
  dtostrf(temp,6,2,temp_str);
  dtostrf(rh,6,2,rh_str);
 

  Serial.println("Humidity(%RH): ");
  Serial.println(rh_str); //prints "2.0"
  Serial.println(SHT21.humi, 2); //works
  Serial.println("Temperature(C): ");
  Serial.println(temp_str); //prints "2.0"
  Serial.println(SHT21.temp, 2); //works
  delay(5000);
}

.... and the output is:

Code:
Humidity(%RH):
  2.00
57.50
Temperature(C):
  2.00
25.74
Humidity(%RH):
  2.00
56.42
Temperature(C):
  2.00
25.74
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very wrong code

temp = (SHT21.temp,2);  does not copy with 2 decimals  - you copy/pasted a pattern from Serial print where the 2 is a parameter (#decimals) of the print function for floats.

The statement  (SHT21.temp,2) is a compound statement existing of 2 statements executed from left to right. The return value of the last statement is 2 (as odd as it may sounds) and this value is assigned to temp.

For humidity the error is the same smiley-wink


Check this variation:
Code:
/*
*  Read the Relative Humidity and Temperature values from SHT21 sensor
*  Clock is Analog pin 5
*  Data is Analog pin 4
*
*/

#include <Wire.h>
#include <SHT21.h>
#include<stdlib.h>

char temp_str[10];
char  rh_str[10];

float temp;
float rh;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
}

void loop()
{
  SHT21.readSensor();
  
  temp = SHT21.temp;
  rh = SHT21.humi;

  dtostrf(temp, 6, 2, temp_str);
  dtostrf(rh, 6, 2, rh_str);
  
  Serial.println("Humidity(%RH): ");
  Serial.println(rh_str);  
  Serial.println(SHT21.humi, 2);         //works

  Serial.println("Temperature(C): ");
  Serial.println(temp_str);  
  Serial.println(SHT21.temp, 2);         //works

  delay(5000);
}
« Last Edit: September 11, 2011, 04:02:04 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wonderful, thanks for both the explaination and fix - works great and I can now print to LCD :-)

Cheers.
Logged

Pages: [1]   Go Up
Jump to: