Temperature spikes and drops with ds18b20

Replace your getTemp function with this.

const unsigned char t_mask[4] = {0x7, 0x3, 0x1, 0x0};
float getTemp()
{
  byte i;
  // This isn't actually used
  byte present = 0;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  // Limit the number of consecutive failures
  int loop_count = 3;

  while(loop_count--) {
    if ( !ds.search(addr)) {

      ds.reset_search();
      delay(250);
      continue;
    }

    if (OneWire::crc8(addr, 8)) {
      //    Serial.println("CRC of address is not valid!");
      continue;
    }
    Serial.println();

    // the first ROM byte indicates which chip
    // We know this is a DS18B20 so don't bother checking it


    ds.reset();
    ds.select(addr);
    ds.write(0x44,1);         // start conversion, with parasite power on at the end

    // The spec says 750mS is worst case. I've been using 800. 1000 is overkill
    delay(1000);
    // we might do a ds.depower() here, but the reset will take care of it.

    present = ds.reset();
    ds.select(addr);    
    ds.write(0xBE);         // Read Scratchpad

    for ( i = 0; i < 9; i++) {           // we need 9 bytes
      data[i] = ds.read();
    }

    // Make sure the CRC of the data is OK
    if(OneWire::crc8(data, 9)) {
      // report CRC failure 
      // Serial.println("CRC of data failed");
      // and try again
      continue;
    }

    // convert the data to actual temperature

    unsigned int raw = (data[1] << 8) | data[0];
    cfg = (data[4] & 0x60) >> 5;
    raw &= ~t_mask[cfg];
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    return fahrenheit;
  }
  // return an obviously incorrect temperature to indicate that three consecutive
  // reads of the temperature failed.
  // For an aquarium this temperature would be a disaster :-)
  // If the outside temperature is being measured then return, say, -60 or +180
  return 0;
}

If the search for the device or either of the CRCs fails, it reads the temperature again and returns zero if it fails after three retries.
I have also changed the code so that it removes some unnecessary tests - you know you've got a DS18B20 so there's no need for the code to make sure. If you add more sensors or change the sensor type, you'll have to add the test back in.
I've also added my fix for a bug in the original version. The code only worked if you were reading with the default 12-bit precision. Any other precision would return incorrect results. I haven't got a DS18S20 so I haven't tested the original code to see if it works with other than the default precision.

Pete