คือผมจะใช้ DHT sensor รับค่าอุณหภูมิและความชื้นมา จากนั้นผมจะใช้ buzzer เตือน โดยมีเงื่อนไข เช่น
ถ้าอุณหภูมิ ต่ำกว่า 25 และ ความชื้นต่ำกว่า 60 ให้ดัง 1 วิ ดับ 1 วิ ทำวนไป
ถ้าอุณภูมิมมากกว่า 35 และ ความชื้นมากกว่า 80 ให้ดัง 5 วิ ดับ 1 วิ ทำวนไป
ผมควรทำอย่าไรดีครับ
ทำได้ไม่ยากครับ มีตัวอย่างโค้ดในคอร์สสอน Arduino Starter ที่หน้าเว็บครับ
เบื้องต้นได้ทำตามที่วีดีโอสอนแล้วครับ แต่ติดปัญหาคือ buzzer ดังตลอดไม่มีเว้นช่วงเลยครับ
ทั้งที่สั่งให้ LOW แล้ว delay ไว้ครับ
โค้ด :
#include<Wire.h>
#include<DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int buzzer = 13;
void setup()
{
dht.begin();
lcd.begin();
lcd.backlight();
pinMode(buzzer, OUTPUT);
}
void loop()
{
lcd.setCursor(1,0);
lcd.print("Heat Stroke Meter");
lcd.setCursor(0,1);
lcd.print("RISK : ");
lcd.setCursor(0,2);
lcd.print("--------------------");
float h = dht.readHumidity();
float t = dht.readTemperature();
if(isnan(t)||isnan(h)){
lcd.println("Failed to read from DHT");
}
//lcd.clear();
lcd.setCursor(0,3);
lcd.print("H:");
lcd.print(h);
lcd.print("%");
lcd.setCursor(8,3);
lcd.print("T:");
lcd.print(t);
lcd.print("C");
delay(500);
if(((t<=27)&&(h<=40))){
//lcd.clear();
lcd.setCursor(7,1);
lcd.println("Not Hot");
delay(500);}
if((t<=27)&&((h>=55)&&(h<=59))){
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);
}
if(((t>=27)&&((h>=40)&&(h<=100))) && ((t>=28)&&((h>=40)&&(h<=89))) && ((t>=29)&&((h>=40)&&(h<=74))) &&
((t>=30)&&((h>=40)&&(h<=59))) && ((t>=31)&&((h>=40)&&(h<=49)))){
//lcd.clear();
lcd.setCursor(7,1);
lcd.println("Normal");
delay(500);}
while( ((t>=27)&&(t<=32)) &&((h>=60)&&(h<=64)) ){
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(2000);
}
if(((t>=28)&&((h>=90)&&(h<=100))) && ((t>=29)&&((h>=75)&&(h<=100))) && ((t>=30)&&((h>=60)&&(h<=89))) &&
((t>=31)&&((h>=50)&&(h<=79))) && ((t>=32)&&((h>=40)&&(h<=69))) && ((t>=33)&&((h>=40)&&(h<=59))) &&
((t>=34)&&((h>=40)&&(h<=54))) && ((t>=36)&&((h>=40)&&(h<=49)))){
// lcd.clear();
lcd.setCursor(7,1);
lcd.println("Caution");
delay(500);}
while( ((t>=33)&&(t<=40)) &&((h>=65)&&(h<=70)) ){
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(3000);
}
if(((t>=30)&&((h>=90)&&(h<=100))) && ((t>=31)&&((h>=80)&&(h<=100))) && ((t>=32)&&((h>=70)&&(h<=99))) &&
((t>=33)&&((h>=60)&&(h<=89))) && ((t>=34)&&((h>=55)&&(h<=79))) && ((t>=36)&&((h>=50)&&(h<=74))) &&
((t>=37)&&((h>=40)&&(h<=64))) && ((t>=38)&&((h>=40)&&(h<=59))) && ((t>=39)&&((h>=40)&&(h<=54))) &&
((t>=40)&&((h>=40)&&(h<=44))) && ((t>=41)&&(h>=40))){
// lcd.clear();
lcd.setCursor(7,1);
lcd.println("Danger");
delay(500);}
while( ((t>=41)&&(t<=54)) &&((h>=70)&&(h<=75)) ){
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(4000);
}
if(((t>=32)&&(h<=100)) && ((t>=33)&&(h>=90)) && ((t>=34)&&(h>=90)) && ((t>=36)&&(h>=75)) &&
((t>=37)&&(h>=65)) && ((t>=38)&&(h>=60)) && ((t>=39)&&(h>=55)) && ((t>=40)&&(h>=50)) && ((t>=41)&&(h>=45)) &&
((t>=42)&&(h>=40))) {
// lcd.clear();
lcd.setCursor(7,1);
lcd.println("HAZARD!!!");
delay(500);}
while(((t>54)&&(h>75))) {
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(5000);
}
}
ต้องทดสอบทีละจุด ลองเปลี่ยน buzzer เป็นตำแหน่งขาอื่น หรือทดลองเฉพาะ buzzer ครับ