@@ -140,62 +140,63 @@ boolean DHT::read(void) {
140
140
141
141
// Turn off interrupts temporarily because the next sections are timing critical
142
142
// and we don't want any interruptions.
143
- noInterrupts ();
143
+ {
144
+ InterruptLock lock;
144
145
145
- // End the start signal by setting data line high for 40 microseconds.
146
- digitalWrite (_pin, HIGH);
147
- delayMicroseconds (40 );
146
+ // End the start signal by setting data line high for 40 microseconds.
147
+ digitalWrite (_pin, HIGH);
148
+ delayMicroseconds (40 );
148
149
149
- // Now start reading the data line to get the value from the DHT sensor.
150
- pinMode (_pin, INPUT);
151
- delayMicroseconds (10 ); // Delay a bit to let sensor pull data line low.
150
+ // Now start reading the data line to get the value from the DHT sensor.
151
+ pinMode (_pin, INPUT);
152
+ delayMicroseconds (10 ); // Delay a bit to let sensor pull data line low.
152
153
153
- // First expect a low signal for ~80 microseconds followed by a high signal
154
- // for ~80 microseconds again.
155
- if (expectPulse (LOW) == 0 ) {
156
- DEBUG_PRINTLN (F (" Timeout waiting for start signal low pulse." ));
157
- _lastresult = false ;
158
- return _lastresult;
159
- }
160
- if (expectPulse (HIGH) == 0 ) {
161
- DEBUG_PRINTLN (F (" Timeout waiting for start signal high pulse." ));
162
- _lastresult = false ;
163
- return _lastresult;
164
- }
165
-
166
- // Now read the 40 bits sent by the sensor. Each bit is sent as a 50
167
- // microsecond low pulse followed by a variable length high pulse. If the
168
- // high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
169
- // then it's a 1. We measure the cycle count of the initial 50us low pulse
170
- // and use that to compare to the cycle count of the high pulse to determine
171
- // if the bit is a 0 (high state cycle count < low state cycle count), or a
172
- // 1 (high state cycle count > low state cycle count).
173
- for (int i=0 ; i<40 ; ++i) {
174
- uint32_t lowCycles = expectPulse (LOW);
175
- if (lowCycles == 0 ) {
176
- DEBUG_PRINTLN (F (" Timeout waiting for bit low pulse." ));
154
+ // First expect a low signal for ~80 microseconds followed by a high signal
155
+ // for ~80 microseconds again.
156
+ if (expectPulse (LOW) == 0 ) {
157
+ DEBUG_PRINTLN (F (" Timeout waiting for start signal low pulse." ));
177
158
_lastresult = false ;
178
159
return _lastresult;
179
160
}
180
- uint32_t highCycles = expectPulse (HIGH);
181
- if (highCycles == 0 ) {
182
- DEBUG_PRINTLN (F (" Timeout waiting for bit high pulse." ));
161
+ if (expectPulse (HIGH) == 0 ) {
162
+ DEBUG_PRINTLN (F (" Timeout waiting for start signal high pulse." ));
183
163
_lastresult = false ;
184
164
return _lastresult;
185
165
}
186
- data[i/8 ] <<= 1 ;
187
- // Now compare the low and high cycle times to see if the bit is a 0 or 1.
188
- if (highCycles > lowCycles) {
189
- // High cycles are greater than 50us low cycle count, must be a 1.
190
- data[i/8 ] |= 1 ;
166
+
167
+ // Now read the 40 bits sent by the sensor. Each bit is sent as a 50
168
+ // microsecond low pulse followed by a variable length high pulse. If the
169
+ // high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
170
+ // then it's a 1. We measure the cycle count of the initial 50us low pulse
171
+ // and use that to compare to the cycle count of the high pulse to determine
172
+ // if the bit is a 0 (high state cycle count < low state cycle count), or a
173
+ // 1 (high state cycle count > low state cycle count).
174
+ for (int i=0 ; i<40 ; ++i) {
175
+ uint32_t lowCycles = expectPulse (LOW);
176
+ if (lowCycles == 0 ) {
177
+ DEBUG_PRINTLN (F (" Timeout waiting for bit low pulse." ));
178
+ _lastresult = false ;
179
+ return _lastresult;
180
+ }
181
+ uint32_t highCycles = expectPulse (HIGH);
182
+ if (highCycles == 0 ) {
183
+ DEBUG_PRINTLN (F (" Timeout waiting for bit high pulse." ));
184
+ _lastresult = false ;
185
+ return _lastresult;
186
+ }
187
+ data[i/8 ] <<= 1 ;
188
+ // Now compare the low and high cycle times to see if the bit is a 0 or 1.
189
+ if (highCycles > lowCycles) {
190
+ // High cycles are greater than 50us low cycle count, must be a 1.
191
+ data[i/8 ] |= 1 ;
192
+ }
193
+ // Else high cycles are less than (or equal to, a weird case) the 50us low
194
+ // cycle count so this must be a zero. Nothing needs to be changed in the
195
+ // stored data.
191
196
}
192
- // Else high cycles are less than (or equal to, a weird case) the 50us low
193
- // cycle count so this must be a zero. Nothing needs to be changed in the
194
- // stored data.
195
- }
197
+ // Timing critical code is now complete.
196
198
197
- // Re-enable interrupts, timing critical code is complete.
198
- interrupts ();
199
+ }
199
200
200
201
DEBUG_PRINTLN (F (" Received:" ));
201
202
DEBUG_PRINT (data[0 ], HEX); DEBUG_PRINT (F (" , " ));
0 commit comments