Correct use of millis()

Hi,

I am using the Adafruit DHT library for my DHT22 sensor.

Everything works great, but there is a delay in the library that is blocking my other code. I display the temperature on a tft screen, but every time I initiate a reading from the sensor, my display hangs.

I believe it is caused by this section:

// Go into high impedence state to let pull-up raise data line level and
  // start the reading process.
  digitalWrite(_pin, HIGH);
  delay(250);

  // First set data line low for 20 milliseconds.
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(20);

I modified it to the following. The sensor seems to work fine, but I just want to check if I implemented it properly:

 uint32_t lastMillis= 0; //Save the last millis()

 // Go into high impedence state to let pull-up raise data line level and
  // start the reading process.
  digitalWrite(_pin, HIGH);
  if (millis() - lastMillis > 250)
  {
  // First set data line low for 20 milliseconds.
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(20);
  lastMillis = millis();
  }

My final question: Why did Adafruit implement a delay there?

Yes, you've implemented it correctly.

Preference and clarity might dictate you avoid using "lastMillis" or "previousMillis" as it's not the last millis() when you set it, but it becomes that, so your English teacher might suggest using "timeStamp" or "processGood" or something that means the same thing everywhere you use the term.

Perehama:
Yes, you've implemented it correctly.

Preference and clarity might dictate you avoid using "lastMillis" or "previousMillis" as it's not the last millis() when you set it, but it becomes that, so your English teacher might suggest using "timeStamp" or "processGood" or something that means the same thing everywhere you use the term.

Thank you for the confirmation and the feedback!

You have another delay still in the program, of 20 milliseconds, which blocks the code for 320,000 clock cycles, if you are running at 16MHz. You may not notice the delay here, but you remove it essentially the same way, however, name your time stamp separately.

Perehama:
You have another delay still in the program, of 20 milliseconds, which blocks the code for 320,000 clock cycles, if you are running at 16MHz. You may not notice the delay here, but you remove it essentially the same way, however, name your time stamp separately.

Yes, but I am unsure how to remove the last delay because no code is executed in that period.

This is the complete section of the boolean:

boolean DHT::read(bool force) {
  // Check if sensor was read less than two seconds ago and return early
  // to use last reading.
  uint32_t currenttime = millis();
  uint32_t lastMillis = 0; //Save the last millis
  if (!force && ((currenttime - _lastreadtime) < 2000)) {
    return _lastresult; // return last correct measurement
  }
  _lastreadtime = currenttime;

  // Reset 40 bits of received data to zero.
  data[0] = data[1] = data[2] = data[3] = data[4] = 0;

  // Send start signal.  See DHT datasheet for full signal diagram:
  //   http://www.adafruit.com/datasheets/Digital%20humidity%20and%20temperature%20sensor%20AM2302.pdf

  // Go into high impedence state to let pull-up raise data line level and
  // start the reading process.
  digitalWrite(_pin, HIGH);
  if (millis() - lastMillis > 250)
  {
  // First set data line low for 20 milliseconds.
  
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(20);
  lastMillis = millis();
  }

Should I put the entire section in a while loop that last 270 ms? That's 250 from the first millis + 20 ms for the last.