Temperature & Humidity Sensor Incremental Change

Hi Everybody,

I am looking for help writing a code which will enable me to trigger a relay for a certain amount of time (20 seconds) every time a Humidity and Temperature Sensor reads an incremental change, of +/- 1 degree for example.

I have searched for a code similar to this but have been unable to find one that works for me. So, any and all links are welcome.

Here is my code which I partly amended from another code I found.

int dataPin = 3;
int clockPin = 5;

int currentTemp       = 0;
int currentHumidty    = 0;
int previousTemp      = 0;
int previousHumidity  = 0; 
int delta             = 0; 
int deltaValue        = 0;

// will change 
int Relay = 3
// change


void setup()
{
   Serial.begin(38400); // Open serial connection to report values to host
   Serial.println("Starting up");
}

void loop()
{
currentTemp = analogRead(dataPin); //read the sensor

    currentTemp = map(currentTemp, 0, 1023, 0, 255); // map the value
    currentTemp = constrain(currentTemp, 0, 255); // not sure this is useful

    delta = abs(previousTemp - currentTemp); // calculate the absolute value of the difference btw privous and current light value

    if (delta >= deltaValue) { // if the difference is higher than a threshold
        Serial.print("currentTemp: ");
        Serial.println(currentTemp);
            digitalWrite(Relay, HIGH);
        else {
            digitalWrite(Relay, LOW);
        }
    
   currentTemp = previousTemp; 
    
  delay(2000);
}

Thank you anyone who can help with this.

    currentTemp = analogRead(dataPin); //read the sensor
    currentTemp = map(currentTemp, 0, 1023, 0, 255); // map the value
    currentTemp = constrain(currentTemp, 0, 255); // not sure this is useful

it is enough to do

    currentTemp = analogRead(dataPin)/ 4;

some refactor

const int dataPin = A0;
const int clockPin = 5;
const int RelayPin = 3;


int currentTemp       = 0;
int currentHumidty    = 0;
int previousTemp      = 0;
int previousHumidity  = 0;

int delta             = 0;
const int deltaValue  = 0;


void setup()
{
  Serial.begin(38400); // Open serial connection to report values to host
  Serial.println("Starting up");

  previousTemp = analogRead(dataPin) / 4;
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);
}


void loop()
{
  currentTemp = analogRead(dataPin) / 4;

  delta = abs(previousTemp - currentTemp);
  if (delta >= deltaValue)
  {
    Serial.print("currentTemp: ");
    Serial.println(currentTemp);
    digitalWrite(RelayPin, HIGH);
    delay(20000);
    digitalWrite(RelayPin, LOW);
  }

  currentTemp = previousTemp;
}