PID dead zone

Hi , I use pid code from Arduino example to control relay,input is temperature thats work fine but i want to add dead zone ,if mu set point is 50 I want 50+-2 ,that will be from 48-52 to do nothing in that range,can someone help me?

my code

#include <PID_v1.h>
#define RelayPin 6

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
  windowStartTime = millis();
  //initialize the variables we're linked to
  Setpoint = 50;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on

void loop()
  Input = analogRead(0);

   * turn the output pin on/off based on pid output
  if(millis() - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH);
  else digitalWrite(RelayPin,LOW);


This should do what you appear to want:

    if (Input > Setpoint -2 && Input < Setpoint+2) {
        Output = 0;
    } else {
1 Like