milesburton / Arduino-Temperature-Control-Library

Arduino Temperature Library
https://www.milesburton.com/w/index.php/Dallas_Temperature_Control_Library
969 stars 487 forks source link

Making blockTillConversionComplete public #178

Closed gaboflowers closed 3 years ago

gaboflowers commented 3 years ago

We must measure from several DS18B20 at once, so we setWaitForConversion(false), run the multiple requestTemperaturesByAddress() needed, and then use blockTillConversionComplete manually after the last request.

RobTillaart commented 3 years ago

According to the datasheet waiting 750 ms (12 bit assumed) guarantees all will be ready. so a simple delay or while loop will do the trick. So if you wait 750 ms delay(750); after you started the first one you can start reading them.

[pseudo code]

uint32_t start;

void loop()
{
  ...
  start = millis();
  for (int i = 0; i < sensorCount; i++)  requestTemperatureByAddress(address[i]);

  while (millis() - start < 750);  // blocking wait   or   // delay(750);

  for (int i = 0; i < sensorCount; i++)
  {
    float t = getTempC(address[i]);
    process(t);  // whatever
  }
  ...
}

Better is not to block your code like

bool requestInProgress = false;
uint32_t start;

void loop()
{
  if (requestInProgress == false)
  {
    requestInProgress = true;
    start = millis();
    for (int i = 0; i < sensorCount; i++)  requestTemperatureByAddress(address[i]);
  }

  if (requestInProgress && (millis() - start >= 750))
  {
    for (int i = 0; i < sensorCount; i++)
    {
      float t = getTempC(address[i]);
      process(t);  // whatever
    }
  }
 ...
}
  1. If you time test the sensors you might tweak 750 with a lower value representing the 'slowest' sensor.
  2. If all sensors are on one bus a single call might be more efficient
    void requestTemperatures(void);