[RESOLVED] DS18B20 Library Modifications

My next guess then (which would easier be found with the Serial.prints in place) that the lib actually returns 0.0 but since you don’t seem to set the return value to ‘NaN’ or any other failure value when the while() bails out due to dsAttempts < 4 being not true anymore, you can’t tell 0.0 from a failure.

Yes. I’ve thrown some Serial.println() statements in to let me know which parts of getTemp() are executing. It seems when the 0.00’ is being returned, it skips everything and returns nothing. I’ll do some more sniffing around! :slight_smile:

All I was missing was the final call to getTemperature() before a final return statement! Code below if anyone else is following along in the future!

double getTemp(DS18B20 sensor){

int dsAttempts = 0;
double celsius = 123.45;
if(!sensor.search()){
  sensor.resetsearch();
  celsius = sensor.getTemperature();
  //Serial.print("First call to getTemperature()");
  //Serial.println(celsius);
  return celsius;
  while (!sensor.crcCheck() && dsAttempts < 4){
    Serial.println("Caught bad value.");
    dsAttempts++;
    Serial.print("Attempts to Read: ");
    Serial.println(dsAttempts);
    if (dsAttempts == 3){
      delay(1000);
    }
    sensor.resetsearch();
    celsius = sensor.getTemperature();
    //Serial.print("Second call to getTemperature()");
    //Serial.println(celsius);
    //continue;
    return celsius;
  }
  dsAttempts = 0;
  //DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
  //Serial.println(fahrenheit);
}
//return celsius;
celsius = sensor.getTemperature();
//Serial.println("Reached end of function, returning null probably");
return celsius;

}

Sorry to repopen an old thread, after testing for quite some time, the getTemp() method seems to return some rather odd values occasionally. I’ve seen -1000’s of Degrees, likewise for very high C values, which obviously aren’t right.

Here is the updated and current version of the getTemp() function, a bit lost now to be honest!

double getTemp(DS18B20 sensor){

int dsAttempts = 0;
double celsius = 0.00;
if(!sensor.search()){
  sensor.resetsearch();
  celsius = sensor.getTemperature();
  //Serial.print("First call to getTemperature()");
  //Serial.println(celsius);
  return celsius;
  while (!sensor.crcCheck() && dsAttempts < 4){
    Serial.println("Caught bad value.");
    dsAttempts++;
    Serial.print("Attempts to Read: ");
    Serial.println(dsAttempts);
    if (dsAttempts == 3){
      delay(1000);
    }
    sensor.resetsearch();
    celsius = sensor.getTemperature();
    return celsius;
  }
  dsAttempts = 0;
}
celsius = sensor.getTemperature();
return celsius;

}

That return celsius; after the first read renders the following validity check null and void - why is this there? :confused:

The same applies to the return celsius; at the end of the while() loop.

You don’t want to return prematurely, but only when you’re done reading and possibly re-reading if you got an error.

try this

double getTemp(DS18B20 sensor) {
  int dsAttempts = 0;
  double celsius = NAN; // this requires #include <math.h> to define Not-A-Number to distinguish invalid values (0.0 is also valid) 
 
  if(!sensor.search()) {
    sensor.resetsearch();
    celsius = sensor.getTemperature();
    while (!sensor.crcCheck() && dsAttempts < 4) {
      Serial.printlnf("Caught bad value (%d)", ++dsAttempts);
      if (dsAttempts == 3) {
        delay(1000);
      }
      sensor.resetsearch();
      celsius = sensor.getTemperature();
    }
  }

  if (dsAttempts >= 4) return NAN;
  else return celsius;
}

After the function call you should check the return value for !isnan() before using it.