เพิ่งหัดเล่นนะครับ ไม่มีความรู้ทางนี้เลย เอาข้อมูลจากหลายๆ ที่มารวมกัน
เข้าเรื่องเลยนะครับ ผมจะทำตัววัดอุณหภูมิและความชื้น โดยใช้ DS18B20 วัดอุณหภูมิ และใช้ DHT22 วัดความชื้น
แต่ตอนนี้ติดปัญหาคือมันไม่แสดงค่าความชื้นออกจอLCD นะครับ แต่ค่าอุณหภูมิแสดง โดยขา DHT22 ต่อเข้ากับขา 11
โค้ดนี้อาจไม่ถูกต้องตามหลักมากนัก อันไหนพอเอาออกได้ แนะนำด้วยนะครับ
อันนี้โค้ดครับ
//Libraries
//Humidity
#include <dht.h>
#include <OneWire.h>
#include <Wire.h>
#define ONE_WIRE_BUS 6
#include <LiquidCrystal_I2C.h> // initialize the library with the numbers of the interface pins
#include <DallasTemperature.h>
dht DHT;
#define DHT22_PIN 11
#define DHTPIN 11 // what pin we're connected to 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)DHT dht(DHTPIN, DHTTYPE);
#define DHT22_PIN 11 // what pin we're connected to 11
#define DHT11PIN 11 //dht11 signal pin connected to 11
#define DHT21PIN 11
#define DHT22PIN 11
//define variables for temp data
float h, t;
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is 11
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
const int dhtPin = 11; //Data pin of DHT-22 to Arduino digital pin 11
//define DHT22_PIN 11
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//Variables
float cswpin;
float hutarget;
int rept = 0;
int rl=0;
int heater = 8;
int huter =5;
int motor = 9;
int motorf = 10;
int hum = 11;
void setup()
{
lcd.init(); // initialize the lcd
// lcd.begin (20,4); // <
lcd.begin (16,2); // set up the LCD's number of columns and rows:
byte tfor[8] = {
B01101,
B01111,
B00001,
B10001,
B10101,
B10101,
B11011,
B11011
};
byte tkok[8] = {
B00000,
B00000,
B00000,
B01110,
B10001,
B01001,
B01001,
B01001
};
byte ti[8] = {
B10110,
B01010,
B00010,
B00010,
B00010,
B00010,
B00010,
B00011
};
byte tkorkai[8] = {
B00010,
B00010,
B00000,
B11010,
B01010,
B01010,
B01010,
B01110
};
byte b[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte v1[8] = {
B00000,
B00000,
B00000,
B10001,
B10001,
B10001,
B01010,
B00100
};
byte v2[8] = {
B00000,
B00000,
B00000,
B00100,
B01100,
B00100,
B00100,
B10101
};
lcd.setBacklight(HIGH);
lcd.clear();
lcd.createChar(0, tfor);
lcd.createChar(1, tkok);
lcd.createChar(2, ti);
lcd.createChar(3, tkorkai);
lcd.createChar(4, b);
lcd.createChar(5, v1);
lcd.createChar(6, v2);
lcd.setCursor(4,0);
lcd.print("Loading...");
lcd.setCursor(0,1);
lcd.print("Loading...");
for(int n = 0; n < 16; n++)
{
lcd.setCursor(n,1);
lcd.write(4);
delay(200);
}
lcd.clear();
lcd.setCursor(4,0);
lcd.write(0);
lcd.write(1);
lcd.write(2);
lcd.write(3);
lcd.print(" v1.0");
pinMode(heater, OUTPUT); // sets the digital pin as output
pinMode(huter, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(motorf, OUTPUT);
pinMode(7, INPUT_PULLUP);
digitalWrite(heater, HIGH);
digitalWrite(huter, HIGH);
sensors.begin();
lcd.setBacklight(LOW); // Backlight off
delay(100);
lcd.setBacklight(HIGH); // Backlight on
delay(100);
lcd.setBacklight(LOW); // Backlight off
delay(100);
lcd.setBacklight(HIGH); // Backlight on
delay(100);
lcd.setBacklight(LOW); // Backlight off
delay(100);
lcd.setBacklight(HIGH); // Backlight on
delay(100);
lcd.setBacklight(LOW); // Backlight off
delay(100);
lcd.setBacklight(HIGH); // Backlight on
delay(100);
lcd.setBacklight(LOW); // Backlight off
delay(100);
lcd.setBacklight(HIGH); // Backlight on
delay(100);
lcd.clear();
}
void loop()
{
Serial.println("Failed to read from DHT");
delay(2000);
lcd.setCursor(0,0); // set the cursor to column 0, line 0
// (note: line 1 is the second row, since counting begins with 0):
lcd.write(0);
lcd.write(1);
lcd.write(2);
lcd.write(3);
lcd.write(5);
lcd.write(6);
rept++;
if (rept>100)
{
if (rl==1)
{rl=0; digitalWrite(motorf, HIGH); delay(2000); rept=0; digitalWrite(motorf, LOW);}
else {rl=1; digitalWrite(motor, HIGH); delay(2000); rept=0; digitalWrite(motor, LOW);}
}
if (digitalRead(7) == 1) { cswpin=37.6; hutarget=60; } else{cswpin=36.9; hutarget=72.5;} //cswpin=36.9;37.6
sensors.requestTemperatures(); // Send the command to get temperatures
//Display
lcd.setCursor(9,0);
lcd.print("TG=");
lcd.print(cswpin);
lcd.setCursor(0,1);
lcd.print("TM=");
sensors.requestTemperatures();
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(9,1);
int err;
float humi;
humi=(DHT.humidity,1);
lcd.print("H=");
lcd.print(humi); //แสดงข้อความออกจอ
if (sensors.getTempCByIndex(0)>cswpin){digitalWrite(heater, LOW);}
if (sensors.getTempCByIndex(0)<(cswpin-0.1)){digitalWrite(heater, HIGH);}
if (humi>hutarget){digitalWrite(huter, LOW);}
if (humi<hutarget){digitalWrite(huter, HIGH);}
}
ขอแนะนำแนวทางดังนี้ครับ
ให้ทำความเข้าใจอุปกรณ์แต่ละตัวก่อน พอทำออกแล้วค่อยเอามารวมกัน
1. DHT22 ลองทำตามตัวอย่างในบทความ จะได้ค่า อุณหภูมิและความชื้น ออกมา ผมว่าน่าจะได้แล้ว
2. จอ LCD มีวิธีต่อในบทความเหมือนกัน ผมว่าน่าจะทำออกแล้วเหมือนกัน
3. เสร็จแล้วเอามาต่อรวมกันครับ ลองดูซักหน่อย ข้อมูลครบแล้วไม่น่าเกินความสามารถครับ : )