2016-12-16 65 views
0

我有一個溫度和溼度的代碼我想補充:「如果溫度低於5度顯示40液晶屏上時,如果語句代碼。我如何做到這一點。如何使用傳感器

#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("%"); 
    } 

這是到目前爲止我的代碼

+1

這看起來像C,而不是Java。爲什麼這個標記的Java? –

+1

Arduino是C++而不是Java。但是,語法基本相同,如果(溫度<5){//執行此顯示操作} else {//執行此操作} – DejaVuSansMono

+0

在開始編碼之前,您是否至少閱讀過一本C或C++書籍?如果是這樣,你讀過關於條件語句(又名IF)的章節嗎? – frarugi87

回答

0

就在這裏,在你的代碼

float temperature, humidity; 

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

添加條件

float temperature, humidity; 

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

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

    delay(2000); 
0

這裏是這樣做的更加begineer友好的方法:

float temperature, humidity; 

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

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

delay(2000);