@@ -143,47 +143,46 @@ boolean DHT::read(void) {
143143 digitalWrite (_pin, LOW);
144144 delay (20 );
145145
146- // Turn off interrupts temporarily because the next sections are timing critical
147- // and we don't want any interruptions.
148- noInterrupts ();
146+ uint32_t cycles[80 ];
147+ {
148+ // Turn off interrupts temporarily because the next sections are timing critical
149+ // and we don't want any interruptions.
150+ InterruptLock lock;
149151
150- // End the start signal by setting data line high for 40 microseconds.
151- digitalWrite (_pin, HIGH);
152- delayMicroseconds (40 );
152+ // End the start signal by setting data line high for 40 microseconds.
153+ digitalWrite (_pin, HIGH);
154+ delayMicroseconds (40 );
153155
154- // Now start reading the data line to get the value from the DHT sensor.
155- pinMode (_pin, INPUT);
156- delayMicroseconds (10 ); // Delay a bit to let sensor pull data line low.
157-
158- // First expect a low signal for ~80 microseconds followed by a high signal
159- // for ~80 microseconds again.
160- if (expectPulse (LOW) == 0 ) {
161- DEBUG_PRINTLN (F (" Timeout waiting for start signal low pulse." ));
162- _lastresult = false ;
163- return _lastresult;
164- }
165- if (expectPulse (HIGH) == 0 ) {
166- DEBUG_PRINTLN (F (" Timeout waiting for start signal high pulse." ));
167- _lastresult = false ;
168- return _lastresult;
169- }
156+ // Now start reading the data line to get the value from the DHT sensor.
157+ pinMode (_pin, INPUT);
158+ delayMicroseconds (10 ); // Delay a bit to let sensor pull data line low.
170159
171- // Now read the 40 bits sent by the sensor. Each bit is sent as a 50
172- // microsecond low pulse followed by a variable length high pulse. If the
173- // high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
174- // then it's a 1. We measure the cycle count of the initial 50us low pulse
175- // and use that to compare to the cycle count of the high pulse to determine
176- // if the bit is a 0 (high state cycle count < low state cycle count), or a
177- // 1 (high state cycle count > low state cycle count). Note that for speed all
178- // the pulses are read into a array and then examined in a later step.
179- uint32_t cycles[80 ];
180- for (int i=0 ; i<80 ; i+=2 ) {
181- cycles[i] = expectPulse (LOW);
182- cycles[i+1 ] = expectPulse (HIGH);
183- }
160+ // First expect a low signal for ~80 microseconds followed by a high signal
161+ // for ~80 microseconds again.
162+ if (expectPulse (LOW) == 0 ) {
163+ DEBUG_PRINTLN (F (" Timeout waiting for start signal low pulse." ));
164+ _lastresult = false ;
165+ return _lastresult;
166+ }
167+ if (expectPulse (HIGH) == 0 ) {
168+ DEBUG_PRINTLN (F (" Timeout waiting for start signal high pulse." ));
169+ _lastresult = false ;
170+ return _lastresult;
171+ }
184172
185- // Re-enable interrupts, timing critical code is complete.
186- interrupts ();
173+ // Now read the 40 bits sent by the sensor. Each bit is sent as a 50
174+ // microsecond low pulse followed by a variable length high pulse. If the
175+ // high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
176+ // then it's a 1. We measure the cycle count of the initial 50us low pulse
177+ // and use that to compare to the cycle count of the high pulse to determine
178+ // if the bit is a 0 (high state cycle count < low state cycle count), or a
179+ // 1 (high state cycle count > low state cycle count). Note that for speed all
180+ // the pulses are read into a array and then examined in a later step.
181+ for (int i=0 ; i<80 ; i+=2 ) {
182+ cycles[i] = expectPulse (LOW);
183+ cycles[i+1 ] = expectPulse (HIGH);
184+ }
185+ } // Timing critical code is now complete.
187186
188187 // Inspect pulses and determine which ones are 0 (high state cycle count < low
189188 // state cycle count), or 1 (high state cycle count > low state cycle count).
0 commit comments