The Code I Used:
//Code by NonchalantWombat //Composite of various other code available online, using DHT.h, Wire.h, and LiquidCrystal.h libraries
int ledPin1 = 3;
DHT dht(DHTPIN, DHTTYPE);
int Lightpin = 3; int TempPin = 2;
LiquidCrystal_I2C lcd(0x3f,16,2); // run ic2_scanner sketch and get the IC2 address, which is 0x3f in my case,it could be 0x3f in many cases
void setup() { lcd.init(); // initialize the lcd pinMode(Lightpin, OUTPUT); // set pin to output pinMode(TempPin, INPUT); // set pin to output Serial.begin(9600); Serial.println("DHTxx test!"); dht.begin(); }
void loop() { delay(8000); float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; }
// Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false);
// Serial.print("Humidity: "); // Serial.print(h); // Serial.print(" %\t"); // Serial.print("Temperature: "); // Serial.print(t); // Serial.print(" *C "); // Serial.print(f); // Serial.print(" *F\t"); // Serial.print("Heat index: "); // Serial.print(hic); // Serial.print(" *C "); // Serial.print(hif); // Serial.println(" *F");
if (f > 101) digitalWrite(Lightpin, LOW); //This turns the light off if (f < 99) digitalWrite(Lightpin, HIGH); //This turns the light on
String TempString = String(f); String HumString = String(h);
//if (f = nan)
//{
// // action A
//}
//else
//{
// // action B
//}
// Print a message to the LCD.
lcd.setCursor(0,0);
// lcd.backlight();
lcd.print("Temp:");
lcd.setCursor(5,0);
lcd.print(TempString);
lcd.setCursor(0,1);
lcd.print("Humid:");
lcd.setCursor(6,1);
lcd.print(HumString);
delay(2000);
// lcd.setBacklight(0);
}