ใช้ Arduino mega 2560 กับ DHT22,sound detector
เพื่อแสดงค่าออก TFT LCD3.5" ครับ
แต่ DHT22 ส่งค่า Temp เป็น 1 ตลอด
ส่วนค่า Humid ส่งค่าเป็น 1 หรือ 2-3 บ้าง บางเวลา
ต้องแก้ตรงไหนครับ
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h> // Core graphics library
#include "DHT.h"
DHT dht;
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// Assign human-readable names to some common 16-bit color values:
#define WHITE 0x0000
#define BLACK 0xFFFF
#define YELLOW 0x001F
#define SKY 0xF800
#define PURPLE 0x07E0
#define RED 0x07FF
#define GREEN 0xF81F
#define BLUE 0xFFE0
MCUFRIEND_kbv tft;
int x = 1;
int sensorPin = A7;
float sensorValue = 0.0;
void setup(void) {
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
dht.setup(23); // data pin 23
}
void loop(void) {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
//float humidity = 60;
//float temperature = 30;
sensorValue = analogRead(sensorPin);
sensorValue = 0.25*sensorValue;
sensorValue = sensorValue+46;
//Serial.println(sensorValue);
//Serial.println(temperature);
//Serial.println(humidity);
if(x==4){
x=1;
}
tft.fillScreen(BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
if(x==1){
if(sensorValue < 80.0 || sensorValue == 80.0){
tft.setTextColor(GREEN);
}
if(sensorValue > 80.0 && sensorValue < 85.0){
tft.setTextColor(YELLOW);
}
if(sensorValue > 85.0 || sensorValue == 85.0){
tft.setTextColor(RED);
}
tft.setTextSize(8);
tft.println(" ");
tft.println("SOUND(dB)");
tft.setTextSize(4);
tft.println(" ");
tft.setTextSize(12);
tft.println(sensorValue);
}
if(x==2){
if(temperature < 30.0 || temperature == 30.0){
tft.setTextColor(GREEN);
}
if(temperature > 30.0 && temperature < 34.0){
tft.setTextColor(YELLOW);
}
if(temperature > 34.0 || temperature == 34.0){
tft.setTextColor(RED);
}
tft.setTextSize(8);
tft.println(" ");
tft.println(" TEMP(C)");
tft.setTextSize(4);
tft.println(" ");
tft.setTextSize(12);
tft.println(temperature);
}
if(x==3){
if(humidity < 67.0 || humidity == 67.0){
tft.setTextColor(GREEN);
}
if(humidity > 67.0){
tft.setTextColor(YELLOW);
}
tft.setTextSize(8);
tft.println(" ");
tft.println(" HUMID(%)");
tft.setTextSize(4);
tft.println(" ");
tft.setTextSize(12);
tft.println(humidity);
}
x = x+1;
delay(3000);
}
ใช้ไลบารีตัวนี้ ใช้ได้ทันทีครับ