2017年12月13日 星期三

arduino溫濕度計輸出LCD(四) 雙偵測器顯示

一個DHT11可以,那是否可以設定兩個以上的溫濕度計呢?
看到有人問這個問題,不過我google不到成品,那就自己設計看看
反正應該不難,就是給他貼兩倍就對了
  • 在#define這邊增加一個 DHT2PIN 4 ,設定是pin4,所以第一個DHT插pin2,第二個插pin4
    增加DHT dht2(DHT2PIN, DHTTYPE); 其實我也不知道這個在幹嘛,不過總覺得要增加

    這部分增加一組可以存數據的h2、t2、f2(其實f是華氏溫度根本也沒用到其實可以刪...)
      //第二個sensor
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h2 = dht2.readHumidity();
      // Read temperature as Celsius (the default)
      float t2 = dht2.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f2 = dht2.readTemperature(true);

    這一段我看不懂也不知道怎麼改,於是我沒複製兩份
      // 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);

    然後覺得跑出來的數字有小數點,但是這個DHT11根本無法測量小數點差異,於是加了int()將讀到的數字取整數(這個部分如果沒問小P老師我找半天找不到...)

      lcd.print(int(h));  //int(h)取h的整數
      lcd.print("% ");

    以下是修改後的程式碼,應該可以順利運作才是,而且拔除其中一個還能顯示!error!,完成這個雙溫濕度計偵測器的方案。不過DHT11感覺在濕度不是很精準,我買了很多個每個插上去數值都不太一樣,可能還是需要校正或是注意一下。
    成品長這樣




    // Example testing sketch for various DHT humidity/temperature sensors
    // Written by ladyada, public domain

    #include "DHT.h"

    #define DHTPIN 2     // what digital pin we're connected to
    #define DHT2PIN 4    // what digital2 pin we're connected to

    // Uncomment whatever type you're using!
    #define DHTTYPE DHT11   // DHT 11
    //#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    //#define DHTTYPE DHT21   // DHT 21 (AM2301)

    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>

    // 設定 LCD I2C 位址
    //LED_Fundino紅色板子用
    //LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
    //LED_MH黑色板子用
    LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

    DHT dht(DHTPIN, DHTTYPE);
    DHT dht2(DHT2PIN, DHTTYPE);

    void setup() {
      // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光
      lcd.begin(16, 2);
      //
      Serial.begin(9600);
      dht.begin();
      dht2.begin()
      // 閃爍兩次
      for (int i = 0; i < 1; i++) {
        lcd.backlight(); // 開啟背光
        delay(250);
        lcd.noBacklight(); // 關閉背光
        delay(250);
      }
      lcd.backlight();

      // 輸出初始化文字
      lcd.setCursor(0, 0); // 設定游標位置在第一行行首
      lcd.print("Hello, Maker!");
      delay(500);
      lcd.setCursor(0, 1); // 設定游標位置在第二行行首
      lcd.print("thermometer*2");
      delay(1000);
      lcd.clear(); //顯示清除
    }
    void loop() {
      // Wait a few seconds between measurements.
      delay(100);

      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      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);

      //第二個sensor
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h2 = dht2.readHumidity();
      // Read temperature as Celsius (the default)
      float t2 = dht2.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f2 = dht2.readTemperature(true);

      // 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);

      lcd.setCursor(0, 0); // 設定游標位置在第一行行首
      lcd.print("sensor1 sensor2");
      lcd.setCursor(0, 1); // 設定游標位置在第二行行首
       
      // 如果拔除sensor1出現error
      if (isnan(h) || isnan(t) || isnan(f)) {
        lcd.print("!error!");
        return;
      }
      lcd.print(int(h));  //int(h)取h的整數
      lcd.print("% ");
      lcd.print(int(t));
      lcd.print("C ");
      
      // 如果拔除sensor2出現error
      if (isnan(h2) || isnan(t2) || isnan(f2)) {
        lcd.print("!error!");
        return;
      }
      lcd.print(int(h2));
      lcd.print("% ");
      lcd.print(int(t2));
      lcd.print("C");
    }
  • 8 則留言:

    1. 你少一行程式碼
      dht2.begin()
      我很好奇你怎麼跑出正確數值的

      回覆刪除
      回覆
      1. 感覺有這行好像滿合理的,不過我這樣上傳可以顯示,說不定dht.begin()這個有就可以了

        刪除
      2. 首先非常感謝您的教學分享,對我幫助很大。
        我在家測試過了,可能溫濕度低不會有影響,昨天下雨濕度高達70%,sensor2的濕度值就飆到169%,加了這行程式碼就沒問題了。

        刪除
      3. 謝謝您的回饋
        難怪我給學生測兩個溫濕度差異很大,看來是有蟲了

        刪除
      4. 是有我這隻懶大蟲嗎?
        我其實初學不久,還希望老師能多多擔待。

        刪除
    2. 我也是初學者,就慢慢爬文慢慢除蟲學習
      不是資訊科系而且不懂程式的狀況下真的滿困難的
      唯一學過的程式語言也僅有大學3學分而已

      回覆刪除
      回覆
      1. 我發現如果你有個執念,就會比較快一點。
        這是個我非得要做出來的東西,所以我找到這行程式碼漏了。
        我也不是資訊系出身,上禮拜才剛開始學而已。

        刪除