เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผล 7 segment MAX7219
ไลบารี่เพิ่มเติม
-DHT.h (ไลบรารี่ตัวเดียวกันกับงานครั้งที่ 48 หากติดตั้งแล้วไม่ต้องทำซ้ำ)*ดูวิธีติดตั้งในงานครั้งที่ 48 (http://www.praphas.com/forum/index.php?topic=201.0)
-LedControl.h (ไลบรารี่ตัวเดียวกันกับงานครั้งที่ 20 หากติดตั้งแล้วไม่ต้องทำซ้ำ)*ดูวิธีติดตั้งในงานครั้งที่ 20 (http://www.praphas.com/forum/index.php?topic=266.0)
โจทย์การทดลอง
-เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผล 7 segment MAX7219
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)
วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino
(http://www.praphas.com/PhotoForum/arduino/Lab-50-01.png)
วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)
(http://www.praphas.com/PhotoForum/arduino/Lab-50-02.png)
ตัวอย่างโปรแกรมอุณหภูมิและความชื้นด้วย DHT22
#include "DHT.h"
#define DHTPIN 13 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
}
ตัวอย่างโปรแกรมแสดงผล 7 Segment MAX7219
#include "LedControl.h"
LedControl lc=LedControl(6,4,5,1); // Pin 11->DIN, 13->CLK, 12->CS, 1 = No.of devices
void setup()
{
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,10); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
}
void loop()
{
lc.setDigit(0,7,2,false);
lc.setDigit(0,6,3,true);
lc.setDigit(0,5,4,false);
lc.setRow(0,4,B00110111);
lc.setRow(0,3,B01001111);
lc.setRow(0,2,B00001110);
lc.setRow(0,1,B00001110);
lc.setRow(0,0,B01111110);
delay(1000);
}