cavedu
  • CAVEDU教育團隊
  • BOSON電子積木套件
    • 認識BOSON
      • 輸入模組
        • i1旋鈕模組
        • i2r按鈕模組
        • i3自鎖開關
        • i4光感測器
        • i5傾斜感測器
        • i6水分感測器
        • i7火焰感測器
        • i8觸碰感測器
        • i9聲音感測器
        • i10紅外線接近度感測器
        • i11溫度感測器
        • i12導電感測開關
        • i13動作感測器
        • i16土壤濕度感測器
        • i17酸鹼值感測器
        • i18濕度感測器
        • i19防水溫度感測器
        • i20心律監測感測器
        • i22超音波感測器
      • 輸出模組
        • o2LED燈模組
        • o5蜂鳴器模組
        • o6風扇模組
        • o7錄音機模組/喇叭
        • o9馬達模組
        • o10伺服機模組
        • o11OLED顯示模組
        • 七彩燈條
      • 功能模組
        • f1運算邏輯模組AND
        • f2運算邏輯模組OR
        • f3運算邏輯模組NOT
        • f4分配模組
        • f6閾模組
        • f7計數模組
        • f8計時模組(0-60秒)
    • 實作範例教學
    • 資源鏈結
  • LinkIt 7697 BlocklyDuino 使用指南
  • LinkIt 7697 - Arduino IDE 開發指南
  • 💻Raspberry Pi Pico介紹
    • Raspberry Pi Pico 擴充板
    • Raspberry Pi Pico 使用Arduino IDE
  • 洞洞么教學材料包
    • 新版LinkIt 7697開發板 V1.1版使用序列埠印出指令亂碼問題
    • 模組使用教學
      • LED模組
      • LED模組(三色)
      • 按鈕模組
      • 旋轉電位計模組
      • 光敏模組
      • 微動開關模組
      • DHT11溫溼度感測器模組
      • 無源蜂鳴器模組
      • 有源蜂鳴器模組
      • 震動開關模組
      • 滾珠開關模組
      • 超音波測距感測器
      • 伺服機
      • 紅外線偵測模組
      • I2C 1602液晶顯示模組
      • 熱敏(溫度)模組
      • DHT 22溫溼度模組(For Pico)
    • micro:bit影片教學
  • 洞洞兩教學材料包(貓咪盃硬體組材料包)
    • 模組使用教學
      • LED模組
      • 繼電器
      • 大顆LED紅綠燈整合模組
      • 旋轉可變電阻
      • 光線模組
      • 土壤溼度模組
      • 避障紅外線模組
      • 按鈕模組
      • 滾珠開關模組
      • 霍爾磁性模組
      • RGB LED共陰模組
      • 電晶體MOS模組
      • 兩路L9110S馬達模組
      • 無源蜂鳴器
      • SR-04P超音波
      • DHT11溫溼度模組
      • 舵機
      • LCD1602液晶模組
      • Max7219 8X8 矩陣LED模組
      • WS2812 RGB12燈燈環模組
  • 洞洞伍AIoT教學材料包
    • Raspberry Pi Pico W 介紹
    • 程式撰寫軟體下載
    • 模組使用教學
      • 溫溼度感測器
      • 旋轉電位計模組
      • RFID模組
      • RGB全彩LED模組(共陽極)
      • 按鈕模組
      • OLED 顯示器模組
      • 有源蜂鳴器模組
      • SG90伺服馬達模組
      • PIR生物紅外線感測器模組
      • 氣體感測器Gas Sensor(MQ7)模組
      • 數位式光強度感測器模組
  • Arduino首次接觸就上手
    • 認識Hangeekduino圖形化介面
    • Hangeekduino範例程式
      • 輸入模組
        • 按鈕模組
        • 旋轉電位計模組
        • 聲音感測模組
        • 光感測器模組
        • 溫溼度感測模組
        • 三軸加速度模組
        • 氣壓感測模組
      • 輸出模組
        • LED模組
        • 蜂鳴器模組
        • OLED顯示模組
    • 書籍內容更新_202012更新
    • [Processing 互動裝置藝術結合 Arduino ]
      • 範例01_Processing IDE 下載、基本範例
      • 範例02_連接Arduino_數位輸入與類比輸入
      • 範例03_Processing控制LED
      • 範例04_療癒海浪 - 按鈕與多個類比感測器
      • 範例05_天空飄著雲 - DHT溫溼度感測器
      • 6. 滾球小遊戲 - 三軸加速度感測器
      • 範例07_風車轉轉 - 聲音感測器
    • Arduino BLE藍牙通訊 - Android 手機
      • Example 01
      • Example 02
      • Example 03
  • 其他感測器模組
    • 火焰感測器
    • 手指偵測心跳模組
    • 心律感測器
    • 科易KEYES 雨水感測器
    • Grove–VOC與eCO2 氣體感測器 (SGP30)
    • 高精度升降壓電源模組XYS3580
  • MCS Lite 介紹及感測器網頁遠端監控
    • 5-4 MCS Lite 介紹及MCS Lite 環境建置
    • 5-5 網頁遠端監控LED
    • 5-6 雲端溫濕度監控
  • 初學Jetson Nano不說No-CAVEDU教你一次懂
    • 書籍更新修改內容
Powered by GitBook
On this page
  • 【教學】Arduino首次接觸就上手_ep5 Processing 讀取溫溼度感測器
  • Arduino 端程式碼 - 自定義發送資料
  • Processing 程式範例 - 接收 Arduino 發送的溫溼度感測器資料來變換效果

Was this helpful?

  1. Arduino首次接觸就上手
  2. [Processing 互動裝置藝術結合 Arduino ]

範例05_天空飄著雲 - DHT溫溼度感測器

Previous範例04_療癒海浪 - 按鈕與多個類比感測器Next6. 滾球小遊戲 - 三軸加速度感測器

Last updated 3 years ago

Was this helpful?

【教學】Arduino首次接觸就上手_ep5 Processing 讀取溫溼度感測器

本篇要告訴您如何讀取 Arduino 首次接觸就上手的 DHT溫溼度感測器,並控制 Processing 畫面上的雲朵顏色與下雨效果。本範例一樣讓 Arduino 透過 Serial.write() 語法逐個發送資料。這樣的作法雖然較為複雜,但允許使用者加入更多自定義的效果,詳細說明請回顧先前的文章喔!

Arduino 端程式碼 - 自定義發送資料

請開啟 Arduino IDE (自行下載或使用 HangeekDuino 下的 Arduino 1.8.5),新增一個專案並填入以下內容或直接使用範例。請注意我們是使用 Arduino首次接觸就上手套件,所以 DHT11 溫溼度感測器的腳位是固定在 D3。。

檔名 -

#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  dht.begin(); 
}

void loop()
{
  delay(100);
  int h = dht.readHumidity();   //取得濕度
  int t = dht.readTemperature();  //取得溫度C
  Serial.write(h);
  Serial.write(t);  
}

Processing 程式範例 - 接收 Arduino 發送的溫溼度感測器資料來變換效果

新增一個 Processing 專案,並輸入以下程式碼,請注意 Drop.pde 中只有定義了 Drop 類別,需要與主程式在同一個資料夾中,如果檔案路徑有錯的話就無法執行,請務必注意喔。確認 Arduino 首次接觸就上手套件已正確燒錄以上 .ino 檔。輸入完成直接執行以下 pde,會看到以下執行畫面。用嘴巴對著DHT11感測器呵氣,就可以看到雲朵的藍色變深了,雨滴的線條也比較粗,下雨的速度也變快了,是不是很可愛呢?這些效果都可以自由在 Processing 中調整,試著嘗試各種不同的參數吧,一定會更得心應手的喔!

執行畫面如下:

執行畫面

濕度最低時,雲朵為白色,也沒有下雨

用嘴巴對著DHT11感測器呵氣,就可以看到雲朵的藍色變深了,雨滴的線條也比較粗,下雨的速度也變快了

主程式 sky_With_dht_processing.pde

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
Serial myPort;

String message;

Drop[] rains=new Drop[100];

int temp;
int count=0;//Serial read odd count is t ,even is h
int t, h, defaultT, defaultH, f, g;
//t:temperature, h:humidity, defaultT:first time t(basis), defaultH:first time h(basis), 
//f:t&defaultT difference, g:h&defaultH difference
int i=0;//just want speed add one time
int j=0;//just want speed add one time
int speed=0;

void setup() {
  size(800, 600);
  myPort = new Serial(this, Arduino.list()[0], 9600);

  for (int i=0; i<rains.length; i++) {
    rains[i]=new Drop();
  }
}
//wind size & color
//color will be influenced by t & h
void wind(int x, int y) {
  stroke(0);
  fill(255-y, 255-x-y, 255-x);//h up , r&g(color) must be down , t up ,g&b will be down
  beginShape();  
  vertex(100, 200); //check https://processing.org/reference/vertex_.html
  vertex(150, 250);
  vertex(200, 200);
  vertex(250, 250);
  vertex(300, 200);
  vertex(350, 250);
  vertex(400, 200);
  vertex(450, 250);
  vertex(500, 200);
  vertex(550, 250);
  vertex(600, 200);
  vertex(650, 250);
  vertex(700, 200);
  vertex(750, 150);
  vertex(700, 100);
  vertex(650, 50);
  vertex(600, 100);
  vertex(650, 50);
  vertex(600, 100);
  vertex(550, 50);
  vertex(500, 100);
  vertex(450, 50);
  vertex(400, 100);
  vertex(350, 50);
  vertex(300, 100);
  vertex(350, 50);
  vertex(300, 100);
  vertex(250, 50);
  vertex(200, 100);
  vertex(250, 50);
  vertex(200, 100);
  vertex(150, 50);
  vertex(100, 100);
  vertex(50, 150);
  vertex(100, 200);
  endShape();
}

void draw() {
  background(255);
  wind(f*40, g*25);
  for (int i=0; i<rains.length; i++) {
    rains[i].fall(speed);
    rains[i].show(speed);
  }
  if (myPort.available()>0) {
    temp = myPort.read(); 
    if (count%2==0) {
      h=temp;
    }
    if (count%2==1) {
      t=temp;
    }
    if (count==0) {
      defaultH=temp;
    }
    if (count==1) {
      defaultT=temp;
    }
    println("t:"+t+" "+"h:"+h);
    f=t-defaultT;
    g=h-defaultH;
    if (f>0||g>0) {
      if (g!=j) {
        println(g);
        speed=g;
      }
      textSize(50);
      fill(0);
    }  
    println("s="+speed);

    count=count+1;
    i=f;
    j=g;
  }
}

副程式 Drop.pde

class Drop{
  float x=random(100,700);
  float y=200;
  float z=random(0,10);
  float yspeed=z;
  float len=10;
  void fall(int in){
    float inspeed=map(in,1,10,1,5);
    y=y+yspeed*inspeed;
    if(y>height){
      y=200;
      yspeed=z;
    }
  }
  void show(int in){
    strokeWeight(in);
    stroke(0,0,0);
    line(x,y,x,y+len);
  }
}
Special thanks to Konny for the supporting!

檔名 -

檔名 -

DHT11感測器基本教學請點我
rain.ino
sky_With_dht_processing.pde
Drop.pde