2017年12月13日 星期三

arduino溫濕度計輸出LCD(三) 溫濕度計數據顯示於LCD

接下來要整合兩個文章,就可以將DHT11溫濕度計讀到的數據傳給LCD
首先要知道溫濕度計裡的程式碼代表的意思
Serial.print()  是把數據傳給電腦
lcd.print() 就是把數據傳給LCD
lcd.print("%   "); 這個是印出"雙引號中的文字",僅限於英數與符號

這樣我們就可以結合兩個程式讓數據輸出到LCD了,以下是我修改後的程式碼



  • #include "DHT.h"
    #define DHTPIN 2     // what digital 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紅色PCF8574T板子用
    //LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
    //LED_MH黑色PCF8574A1板子用
    LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

    DHT dht(DHTPIN, DHTTYPE);

    void setup() {
      // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光
      lcd.begin(16, 2);
      //
      Serial.begin(9600);
      dht.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");
      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);

      // 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("Humidity Temp.");
      lcd.setCursor(0, 1); // 設定游標位置在第二行行首
       
      // 如果拔除sensor1出現error
      if (isnan(h) || isnan(t) || isnan(f)) {
        lcd.print("!error!");
        return;
      }
      lcd.print(h);  //int(h)取h的整數
      lcd.print("%   ");
      lcd.print(t);
      lcd.print("C ");
    }



    這樣就可以做出一個簡單的溫濕度計並且顯示在LCD螢幕上了
    如果插上行動電源就可以帶著走完全沒問題。


  • 6 則留言:

    1. 'POSITIVE' was not declared in this scope請問該如何解決

      回覆刪除
      回覆
      1. 應該是跟下載的library有關
        你載的跟我載的不一樣

        刪除
      2. 可以向您詢問載點嗎
        因為我的也是顯示一樣的錯誤

        刪除
    2. 請跟著前兩篇文章步驟做看看
      Library不一樣就無法適用
      http://orangevblog.blogspot.com/2017/12/arduinolcd-lcdhello-world.html?m=1

      回覆刪除
    3. 請問可以同時用led 輸出,和電腦輸出到檔案嗎?

      回覆刪除
      回覆
      1. 可以的 相關資料可以搜尋Arduino excel之類的就有如何輸出電腦並匯入excel
        只要崁入適當的位置就可以

        刪除