Why is my LCD displaying NAN

Hello I am confused as to why my LCD is displaying NAN for temperature and humidity. I am using a DHT11 Temperature and Humidity sensor. I have attached my code below can someone tell me how to fix this.

// References
//https://create.arduino.cc/projecthub/interpeo/temperature-humidity-on-lcd-0396b3?ref=search&ref_id=display%20temperature%20and%20humidity&offset=0
//

#include <LiquidCrystal.h> //commucates with LCD
#include "DHT.h" //commuciates with DHT11

#define DHTPIN 6 // digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11 sensor

// lcd display object instance
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// dht sensor object instance
DHT dht(DHTPIN, DHTTYPE);

// delay time settings
int stepDelay = 100;
int shortDelay = 200;
int mediumDelay = 500;
int oneSecDelay = 1000;
int longDelay = 3000;
int nCounter = 3;

// variables used in the sketch
float humidity = 0;
float tempFahr = 0;
float tempCels = 0;
float tempHeatC = 0;
float tempHeatF = 0;
float tempMIN = 0;
float tempMAX = 0;
bool isFirstExec = true;

//String txtTemp = ""; // Temperature text
//String txtUmidita = ""; // Humidity text
//String txtHeatTemp = ""; // Heat Temperature text

// debug
void debugDataCelsius(float h, float t) {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");

Serial.println("\t");
}

String getTextTemparature() {
String txt = "TEMP: ";
txt.concat(String(tempCels, 0));
txt.concat((char)223); // ASCII degree ° char ASCII Table - ASCII codes,hex,decimal,binary,html
txt.concat("C");
return txt;
}

String getTextHumidity() {
return "HUMIDITY: " + String(humidity, 0) + "%";
}

void readSensorData() {
//Serial.println("DHT11 sensor data request...");

humidity = dht.readHumidity(); // Read humidity data

tempCels = dht.readTemperature(); // Read temperature as Celsius

//tempFahr = dht.readTemperature(true); // Read temperature as Fahrenheit - not used

// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(tempCels) || isnan(tempFahr)) {
//Serial.println("Failed to read from DHT sensor!");
return;
}

// Compute heat index in Fahrenheit (the default)
//tempHeatF = dht.computeHeatIndex(tempFahr, humidity);

// Compute heat index in Celsius (isFahreheit = false)
tempHeatC = dht.computeHeatIndex(tempCels, humidity, false);
}

void printLcdTemperatureRow() {

lcd.setCursor(0, 0); // moving to first row

lcd.print(getTextTemparature()); // it always prints temp. in first row
}

void printLcdDataRow() {

lcd.setCursor(0, 1); // moving to second row

// it checks what to print
if (nCounter == 0) {
lcd.print(getTextHumidity()); // if loop #1 it prints humidity in second row
}
}

void scrollText(){
// scroll text to the right
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayRight();
delay(stepDelay);
}
}

void setup() {

// Serial.begin(9600); // debug

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

dht.begin();

//Serial.println("DHT11 Started..."); // debug
}

void loop() {

lcd.clear();

if (nCounter > 2) {
nCounter = 0; // reset counter
readSensorData(); // every 3 loops it reads data from sensor
}

//debugDataCelsius(humidity, tempCels, tempHeatC);

printLcdTemperatureRow();
printLcdDataRow();
delay(longDelay);

lcd.clear();

delay(mediumDelay);

printLcdTemperatureRow();
printLcdDataRow();
delay(longDelay);

scrollText();

nCounter++;

delay(mediumDelay);
}

Individual_Project.ino (3.68 KB)

Just post code in-line, it makes it easier to read. Also, WHAT sensor do you have and how is it wired? NAN is Not A Number, so the error is due to the sensor not reading, or giving out of spec data!

OK, just a hint if you propose to proceed to discuss things on this forum.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. I gather you have already done this to insert your code. Now just highlight each section of code (or output if you later need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. While you were lucky so far, If you do not post it as "code" it can easily be quite garbled and is generally more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper; yours is not. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks and only between complete functional blocks.