12V fan doesn't work with IF statement using temperature sensors DS18B20

Hello, please take a look at the sketch below. We sued the first one to test connections and see if it drives our fan. And it worked fine.
int fanPin = 9; // Fan pin

void setup() {
// nothing happens in setup
}

void loop() {
int fadeValue =0 ;
analogWrite(fanPin, fadeValue);
// wait for 3000 milliseconds to see the fade effect
delay(30000);

fadeValue =255 ;
analogWrite(fanPin, fadeValue);
// wait for 3000 milliseconds to see the fade effect
delay(30000);

The first file we used just to test if everything is working , and fan went on and off. It has separate voltage supply. The larger document is the program that needs fixing. It compiles no problem, both temperature sensors work. It displays temperature: 1st line is peltier, 2nd line is heatsink. Then clears and displays "Fan OFF", then goes back to displaying temperature. Fan doesn't go on no matter how much temperature difference we set. We think that it is something with not reading temperature in the format suitable to use in our IF statement.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <math.h>

#define ONE_WIRE_BUS 2 // Data wire is plugged into pin 3 on the Arduino

OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices

DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

// LCD Define
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Pins used on the LCD for our Arduino Uno

// Temperature Sensor Addresses
DeviceAddress PeltierTemp = { 0x28, 0x08, 0xC9, 0x5B, 0x04, 0x00, 0x00, 0xBC };
DeviceAddress HeatSinkTemp = { 0x28, 0x4C, 0x0D, 0x5B, 0x04, 0x00, 0x00, 0x28 };

const int fanPin = 9; // Fan connected to digital pin 9
int TemperatureDifference;

void setup(void)
{
// initialize serial communications at 9600 bps (serial port)
Serial.begin(9600);

delay(1000);

// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(PeltierTemp, 10);
sensors.setResolution(HeatSinkTemp, 10);

}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {

lcd.print("F)");
lcd.print(DallasTemperature::toFahrenheit(tempC));
}
}
void loop()
{

lcd.clear();

sensors.requestTemperatures();
lcd.begin(16, 2); //Number of columns/rows in LCD screen

lcd.print("Peltier("); //Prints Device Temperature on LCD
printTemperature(PeltierTemp);

lcd.setCursor(0, 1); //New line of the LCD display
lcd.print("HeatSink(");
printTemperature(HeatSinkTemp);
delay (5000);
lcd.clear();

TemperatureDifference = PeltierTemp - HeatSinkTemp;
int fanSpeed;
if (TemperatureDifference <= 5) {
fanSpeed=255;
analogWrite(fanPin, fanSpeed);
lcd.begin(16, 2);
lcd.print("Fan Off");
delay (500);
}
else {
fanSpeed=255;
analogWrite(fanPin, fanSpeed);
lcd.clear();
lcd.begin(16, 2);
lcd.print("Fan Speed 100%");
delay (500);
}

delay(5000);

}
Thank you so much for the help!

Did you read this?

Why not?

Would it be fair to say that whatever the temperature difference, the fan is on the same speed?

How does this compile at all? You are trying to divide between two DeviceAddress objects and assign the value to a float.

// Temperature Sensor Addresses
DeviceAddress PeltierTemp = { 0x28, 0x08, 0xC9, 0x5B, 0x04, 0x00, 0x00, 0xBC };
DeviceAddress HeatSinkTemp = { 0x28, 0x4C, 0x0D, 0x5B, 0x04, 0x00, 0x00, 0x28 };
...
int TemperatureDifference;
...
TemperatureDifference = PeltierTemp - HeatSinkTemp;

I believe that's your problem!
Try this instead:

float peltier = sensors.getTempC(PeltierTemp);
float heatSink = sensors.getTempC(HeatSinkTemp);
if (peltier - heatSink <= 5) {
...
}

Thank you, I tried the
float peltier=getTempC.....
just the way you suggested, but fan is still not going on. We can see that if statement works properly, because once we create sufficient temperature difference LCD displays "Fan Speed 100%".

Post the code you have now.
Remember to use code tags.

It compiles no problem,

Nonsense.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <math.h>


#define ONE_WIRE_BUS 2 // Data wire is plugged into pin 3 on the Arduino


OneWire oneWire(ONE_WIRE_BUS);  // Setup a oneWire instance to communicate with any OneWire devices


DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.

// LCD Define
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Pins used on the LCD for our Arduino Uno


// Temperature Sensor Addresses
DeviceAddress PeltierTemp = { 0x28, 0x08, 0xC9, 0x5B, 0x04, 0x00, 0x00, 0xBC };
DeviceAddress HeatSinkTemp = { 0x28, 0x4C, 0x0D, 0x5B, 0x04, 0x00, 0x00, 0x28 };

const int fanPin = 9; // Fan connected to digital pin 9
//double TemperatureDifference;
//double ftemp1;
//double ftemp2;




void setup(void)
{
  // initialize serial communications at 9600 bps (serial port)
  Serial.begin(9600);
  
  delay(1000);


  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(PeltierTemp, 10);
  sensors.setResolution(HeatSinkTemp, 10);

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    lcd.print("Error");
  } else {
   
    lcd.print("F)");
    lcd.print(DallasTemperature::toFahrenheit(tempC));
  }
}
void loop()
{ 
 
  lcd.clear();
  
  sensors.requestTemperatures();
   lcd.begin(16, 2);  //Number of columns/rows in LCD screen
   
  lcd.print("Peltier(");  //Prints Device Temperature on LCD
  printTemperature(PeltierTemp);
  
  lcd.setCursor(0, 1);          //New line of the LCD display
  lcd.print("HeatSink(");
  printTemperature(HeatSinkTemp);
  delay (5000);
  lcd.clear();
  //ftemp1 = ((sensors.getTempCByIndex(1)* 1.8) + 32.0); //Converts temp reading F
  
  //ftemp2 = ((sensors.getTempCByIndex(0)* 1.8) + 32.0); //Converts temp reading F
  //TemperatureDifference = ftemp1 - ftemp2;
  float peltier = sensors.getTempC(PeltierTemp);
  float heatSink = sensors.getTempC(HeatSinkTemp);
  int fanSpeed=0;
  if (peltier-heatSink <= 5) {
    fanSpeed=0;
    analogWrite(fanPin, fanSpeed);
    lcd.begin(16, 2);
    lcd.print("Fan Off");
    delay (500);
  }
      else {
      fanSpeed=255;
      analogWrite(fanPin, fanSpeed);
      lcd.clear();
      lcd.begin(16, 2);
      lcd.print("Fan Speed 100%");
      delay (500);
  }
  
  delay(5000);
  

}