Code for variance in SetPoint

I have an adjustable setpoint that controls a relay based on the input from a DS18B20.

What code would enable a variance around a desired SetPoint of 0.5 deg C.

Example:

I adjust the set point to 45 deg C. Though to stop the relay switching on/off erratically around that temperature I'd like to have a variance of 0.5 deg C either side of the SetPoint. So the relay switches on at 44.5 and off at 45.5.

Would I use if else statements or is there another way?

Thanks

Would I use if else statements or is there another way?

Yes.

   if(temp > SetPoint-0.5 && temp < SetPoint+0.5)
   {
      // temp is within 1/2 a degree of the setpoint
   }

mack85n:
I adjust the set point to 45 deg C. Though to stop the relay switching on/off erratically around that temperature I'd like to have a variance of 0.5 deg C either side of the SetPoint. So the relay switches on at 44.5 and off at 45.5.

The term you're looking for is Hysteresis.

const float variance = 0.5; //degrees C, plus or minus
float setpoint = 45.0; 

void setup() {
  //set up things here, make output pins outputs...
}

void loop() {
  float temp = readTemperatureC();
  if(temp > setpoint + variance)  {
    Serial.println("Relay on!");
    relayOn();
  }
  if(temp < setpoint - variance) {
    Serial.println("Relay OFF");
    relayOff();
  }
}

Thanks,

There looks to be a few ways to achieve the hysteresis I'm looking for.