MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / read

Method read

libraries/DHTlib/dht.cpp:133–191  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

131
132
133int8_t dht::read(uint8_t pin)
134{
135 // READ VALUES
136 if (_disableIRQ) noInterrupts();
137 int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);
138 if (_disableIRQ) interrupts();
139
140 // these bits are always zero, masking them reduces errors.
141 bits[0] &= 0x03;
142 bits[2] &= 0x83;
143
144 // CONVERT AND STORE
145 humidity = (bits[0] * 256 + bits[1]) * 0.1;
146 int16_t t = ((bits[2] & 0x7F) * 256 + bits[3]);
147 if (t == 0)
148 {
149 temperature = 0.0; // prevent -0.0;
150 }
151 else
152 {
153 temperature = t * 0.1;
154 if((bits[2] & 0x80) == 0x80 )
155 {
156 temperature = -temperature;
157 }
158 }
159
160 // HEXDUMP DEBUG
161 /*
162 Serial.println();
163 // CHECKSUM
164 if (_bits[4] < 0x10) Serial.print(0);
165 Serial.print(_bits[4], HEX);
166 Serial.print(" ");
167 // TEMPERATURE
168 if (_bits[2] < 0x10) Serial.print(0);
169 Serial.print(_bits[2], HEX);
170 if (_bits[3] < 0x10) Serial.print(0);
171 Serial.print(_bits[3], HEX);
172 Serial.print(" ");
173 Serial.print(temperature, 1);
174 Serial.print(" ");
175 // HUMIDITY
176 if (_bits[0] < 0x10) Serial.print(0);
177 Serial.print(_bits[0], HEX);
178 if (_bits[1] < 0x10) Serial.print(0);
179 Serial.print(_bits[1], HEX);
180 Serial.print(" ");
181 Serial.print(humidity, 1);
182 */
183
184 // TEST CHECKSUM
185 uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
186 if (bits[4] != sum)
187 {
188 return DHTLIB_ERROR_CHECKSUM;
189 }
190 return result;

Callers 5

_readRawMethod · 0.45
readScratchPadMethod · 0.45
getModeMethod · 0.45
getUIDMethod · 0.45
_readByteMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected