Temp Sensor Reading Question

Hi all. First post here. New to Arduino projects and really appreciate the forum so far, thanks to everyone who posts!

I have a question about a project I'm working on for a controlled reptile environment and haven't been able to figure it out on my own.

Currently the Arduino is programmed to read a temp/humidity sensor and turn on/off a heat lamp depending on current temp. The issue is if I unplug the sensor it will display the last recorded reading. If I plug the sensor back in the values update accordingly. Otherwise the serial print, LCD screen and heat lamp relay all act as if the last recorded values are the current values.

This issue only started happening once I added the day light lamp timer and relay code which when I removed one at a time doesn't resolve so it's hard to isolate. Similarly, if I re-upload the code to the Arduino it will display the correct reading whether unplugged or not which I don't understand why the loop restarting is not fulfilling a similar function.

For clarification the full code turns on/off a day light lamp through relay, heat lamp control through relay and temp/humidity sensor with LCD readings.

#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);   // the display is connected to said pins
int val;
int tempPin = 0;      // temp sensor is connected to pin A0 
int hlamp = 9;        // heat lamp is to pin 9
const int lamp = 8;   // lamp is connected to pin 8
int lampState = LOW;
dht DHT;

unsigned long previousMillis = 0;  // beginning delay for lamp
const long interval = 60000;       // on for x amount of time

void setup(){
  Serial.begin(9600);
  delay(500);                 //Delay to let system boot
  delay(1000);                //Wait before accessing Sensor
   lcd.begin(16, 2);          //lcd = liquid crystal display  the lcd is 16x2 ; 2 rows 16 cells
   pinMode(hlamp, OUTPUT);
   pinMode(lamp, OUTPUT);
   digitalWrite(lamp, HIGH);
}//end "setup()"
 
void loop(){
  
//begin lamp timer
  unsigned long currentMillis = millis();
 if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (lampState == LOW) {
      lampState = HIGH;
   } else {
      lampState = LOW;
    }}
    digitalWrite(lamp, lampState);
//end lamp timer

  DHT.read11(dht_apin);
  
  if (1.8*DHT.temperature+32 < 91) // max temp is 91
{
  digitalWrite(hlamp, LOW);        // turn on heat lamp
  Serial.print("LAMP ON");
}
else // else turn off the heat lamp
{
  digitalWrite(hlamp, HIGH);
  Serial.print("LAMP OFF ");
} 
    Serial.print(", HUMIDITY:, ");
    Serial.print((int)round(DHT.humidity));
    Serial.print(" , TEMP:, ");
    
    Serial.println(round(1.8*DHT.temperature+32));

  lcd.print("  TEMP    HUMID");
  lcd.setCursor(2, 1);
  lcd.print((int)round(1.8*DHT.temperature+32));
  lcd.setCursor(5,1);
  lcd.print("F");

  lcd.setCursor(10, 1);
  lcd.print((int)round(DHT.humidity));
  lcd.setCursor(13, 1);
  lcd.print("%");
  delay(3000);

lcd.clear();

}// end loop

Thank you in advance.

Good job with the code tags on your first post.

The issue is if I unplug the sensor it will display the last recorded reading.

DHT.read11(dht_apin);

This function has a return value and you should use it for error checking. Before using any values check the value of the function return like in the library example.

//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 5

void setup()
{
  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(2000);
}
//
// END OF FILE
//