how to code if statement when using a sensor

1.4k Views Asked by At

i have a temperature and humidity code i want to add "if temperature is less than 5 degrees display 40 on lcd screen. How do i do this.

    #include "DHT.h"
    #include <LiquidCrystal.h>

    #define DHTPIN 22     // what pin we're connected to

    #define DHTTYPE DHT11   

    DHT dht(DHTPIN, DHTTYPE);
    LiquidCrystal lcd(8,9,4,5,6,7); 

    void setup(void) {
    lcd.begin(16, 2);
    lcd.print("Reading sensor");
    dht.begin();
        }

    void loop() {

    float temperature, humidity;

    humidity = dht.readHumidity();
    temperature = dht.readTemperature();
    delay(2000); 

    lcd.clear();

    char tempF[6]; 
    char humF[6];
    dtostrf(temperature, 5, 1, tempF);
    dtostrf(humidity, 2, 0, humF);

    lcd.print("T:"); 
    lcd.print(tempF);
    lcd.print((char)223);
    lcd.print("C ");
    lcd.print("H: ");
    lcd.print(humF);
    lcd.print("%");
    }

this is my code so far

2

There are 2 best solutions below

0
On

Right here, in your code

    float temperature, humidity;

    humidity = dht.readHumidity();
    temperature = dht.readTemperature();
    delay(2000); 

add the condition

    float temperature, humidity;

    humidity = dht.readHumidity();
    temperature = dht.readTemperature();

    temperature = (temperature < 5)?40:temperature;

    delay(2000); 
0
On

Here is a more begineer friendly method of doing this:

float temperature, humidity;

humidity = dht.readHumidity();
temperature = dht.readTemperature();

  if(temperature < 5)
  {
    //code to display 40 on lcd screen
  }

delay(2000);