11//
22// FILE: dhtnew.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.4.19
4+ // VERSION: 0.4.20
55// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66// URL: https://github.com/RobTillaart/DHTNEW
77//
@@ -47,10 +47,10 @@ void DHTNEW::reset()
4747
4848 _wakeupDelay = 0 ;
4949 _type = 0 ;
50- _humOffset = ( float ) 0.0 ;
51- _tempOffset = ( float ) 0.0 ;
52- _humidity = ( float ) 0.0 ;
53- _temperature = ( float ) 0.0 ;
50+ _humOffset = 0.0 ;
51+ _tempOffset = 0.0 ;
52+ _humidity = 0.0 ;
53+ _temperature = 0.0 ;
5454 _lastRead = 0 ;
5555 _disableIRQ = true ;
5656 _waitForRead = false ;
@@ -74,18 +74,22 @@ uint8_t DHTNEW::getType()
7474
7575void DHTNEW::setType (uint8_t type)
7676{
77- if ((type == 0 ) || (type == 11 ))
77+ // default type == 0 or unsupported
78+ _type = 0 ;
79+ _wakeupDelay = DHTLIB_DHT11_WAKEUP;
80+
81+ if ((type == 22 ) || (type == 23 ))
7882 {
7983 _type = type;
80- _wakeupDelay = DHTLIB_DHT11_WAKEUP ;
84+ _wakeupDelay = DHTLIB_DHT_WAKEUP ;
8185 }
82- if (( type == 22 ) || (type == 23 ) )
86+ else if (type == 11 )
8387 {
8488 _type = type;
85- _wakeupDelay = DHTLIB_DHT_WAKEUP ;
89+ _wakeupDelay = DHTLIB_DHT11_WAKEUP ;
8690 }
8791 // experimental 0.4.14
88- if (type == 70 )
92+ else if (type == 70 )
8993 {
9094 _type = type;
9195 _wakeupDelay = DHTLIB_SI7021_WAKEUP;
@@ -192,7 +196,7 @@ int DHTNEW::_read()
192196 int16_t t = ((_bits[2 ] & 0x7F ) * 256 + _bits[3 ]);
193197 if (t == 0 )
194198 {
195- _temperature = ( float ) 0.0 ; // prevent -0.0;
199+ _temperature = 0.0 ; // prevent -0.0;
196200 }
197201 else
198202 {
@@ -205,22 +209,22 @@ int DHTNEW::_read()
205209 }
206210
207211
208- // HEXDUMP DEBUG
212+ // HEXDUMP DEBUG
209213 /*
210214 Serial.println();
211- // CHECKSUM
215+ // CHECKSUM
212216 if (_bits[4] < 0x10) Serial.print(0);
213217 Serial.print(_bits[4], HEX);
214218 Serial.print(" ");
215- // TEMPERATURE
219+ // TEMPERATURE
216220 if (_bits[2] < 0x10) Serial.print(0);
217221 Serial.print(_bits[2], HEX);
218222 if (_bits[3] < 0x10) Serial.print(0);
219223 Serial.print(_bits[3], HEX);
220224 Serial.print(" ");
221225 Serial.print(_temperature, 1);
222226 Serial.print(" ");
223- // HUMIDITY
227+ // HUMIDITY
224228 if (_bits[0] < 0x10) Serial.print(0);
225229 Serial.print(_bits[0], HEX);
226230 if (_bits[1] < 0x10) Serial.print(0);
@@ -241,13 +245,13 @@ int DHTNEW::_read()
241245 }
242246#endif
243247
244- if (_humOffset != ( float ) 0.0 )
248+ if (_humOffset != 0.0 )
245249 {
246250 _humidity += _humOffset;
247- if (_humidity < 0 ) _humidity = 0 ;
248- if (_humidity > 100 ) _humidity = 100 ;
251+ if (_humidity > 100 ) _humidity = 100 ;
252+ else if (_humidity < 0 ) _humidity = 0 ;
249253 }
250- if (_tempOffset != ( float ) 0.0 )
254+ if (_tempOffset != 0.0 )
251255 {
252256 _temperature += _tempOffset;
253257 }
@@ -266,7 +270,7 @@ int DHTNEW::_read()
266270void DHTNEW::powerUp ()
267271{
268272 digitalWrite (_dataPin, HIGH);
269- // do a dummy read to sync the sensor
273+ // do a dummy read to synchronise the sensor
270274 read ();
271275};
272276
@@ -298,7 +302,10 @@ int DHTNEW::_readSensor()
298302 uint8_t idx = 0 ;
299303
300304 // EMPTY BUFFER
301- for (uint8_t i = 0 ; i < 5 ; i++) _bits[i] = 0 ;
305+ for (uint8_t i = 0 ; i < 5 ; i++)
306+ {
307+ _bits[i] = 0 ;
308+ }
302309
303310 // REQUEST SAMPLE - SEND WAKEUP TO SENSOR
304311 pinMode (_dataPin, OUTPUT);
@@ -412,7 +419,7 @@ bool DHTNEW::_waitFor(uint8_t state, uint32_t timeout)
412419 uint8_t count = 2 ;
413420 while ((micros () - start) < timeout)
414421 {
415- // d elayMicroseconds (1); // less # reads ==> minimizes # glitch reads
422+ // delayMicroseconds (1); // less # reads ==> minimizes # glitch reads
416423 if (digitalRead (_dataPin) == state)
417424 {
418425 count--;
0 commit comments