#include "DHT.h" // DHT 헤더파일 불러오기
#include <LiquidCrystal_I2C.h> // I2C LCD 헤더파일 불러오기
#include <Servo.h> // 서모모터 헤더파일 불러오기
#define DHTPIN 2 // DHT 센서를 디지털 2번핀에 연결
const int red = 7; // 빨간색 LED를 디지털 7번핀에 연결
const int green = 6; // 초록색 LED를 디지털 6번핀에 연결
const int blue = 5; // 파란색 LED를 디지털 5번핀에 연결
const int relay = 11; // 릴레이를 디지털 11번핀에 연결
const int buzzer = 12; // 부저를 디지털 12번 핀에 연결
#define DHTTYPE DHT22 // DHT 타입 설정
DHT dht(DHTPIN, DHTTYPE); // DHT 객체 생성
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C 주소, 16x2 LCD 객체 생성
Servo servo; // 서보모터 객체 생성
void setup()
{
Serial.begin(115200); // 시리얼 통신 시작
Serial.println(F("DHT22 example!")); // 시리얼 모니터에 메시지 출력
dht.begin(); // DHT 센서 초기화
pinMode(red, OUTPUT); // 빨간색 LED 핀을 출력 모드로 설정
pinMode(green, OUTPUT); // 초록색 LED 핀을 출력 모드로 설정
pinMode(blue, OUTPUT); // 파란색 LED 핀을 출력 모드로 설정
pinMode(relay, OUTPUT); // 릴레이 핀을 출력 모드로 설정
servo.attach(9); // 서보모터 핀 설정
lcd.init(); // LCD 초기화
lcd.backlight(); // 백라이트 활성화
}
void loop()
{
float temperature = dht.readTemperature(); // 온도 읽기
float humidity = dht.readHumidity(); // 습도 읽기
if (isnan(temperature) || isnan(humidity)) // DHT 센서 읽기 실패 시 조건문
{
Serial.println(F("Failed to read from DHT sensor!")); // 시리얼 모니터에 에러 메시지 출력
return; // 함수 종료
}
Serial.print(F("Humidity: ")); // 시리얼 모니터에 습도 출력
Serial.print(humidity);
Serial.print(F("% Temperature: ")); // 시리얼 모니터에 온도 출력
Serial.print(temperature);
Serial.println(F("°C "));
if (temperature >= 40 && humidity >= 70) // 온도가 40°C 이상이고 습도가 70% 이상일 때의 조건문 실행
{
lcd.clear(); // LCD 화면 지우기
lcd.setCursor(0, 0); // LCD 커서 위치 설정
lcd.print("Warning"); // LCD에 경고 메시지 출력
lcd.setCursor(0, 1); // LCD 두 번째 줄로 커서 이동
lcd.print("OPEN The Door! "); // LCD에 문을 열라는 메시지 출력
digitalWrite(red, HIGH); // 빨간색 LED 켜기
digitalWrite(green, LOW); // 초록색 LED 끄기
digitalWrite(blue, LOW); // 파란색 LED 끄기
servo.write(180); // 서보모터를 180도 회전
tone(buzzer, 450); // 부저 소리 내기 (주파수: 450)
delay(500); // 500ms 대기
noTone(buzzer); // 부저 소리 멈춤
digitalWrite(relay, HIGH); // 릴레이 켜기
}
else // 온도와 습도가 안전 범위일 때의 조건문 실행
{
lcd.clear(); // LCD 화면 지우기
lcd.setCursor(0, 0); // LCD 커서 위치 설정
lcd.print("TEMP: "); // LCD에 온도 텍스트 출력
lcd.print(temperature); // 온도 값 출력
lcd.write(223); // 온도 기호 출력 (° 기호)
lcd.print("C");
lcd.setCursor(0, 1); // LCD 두 번째 줄로 커서 이동
lcd.print("HUM : "); // LCD에 습도 텍스트 출력
lcd.print(humidity); // 습도 값 출력
lcd.print("%");
digitalWrite(red, LOW); // 빨간색 LED 끄기
digitalWrite(green, HIGH); // 초록색 LED 켜기
digitalWrite(blue, LOW); // 파란색 LED 끄기
servo.write(0); // 서보모터를 0도 회전
digitalWrite(relay, LOW); // 릴레이 끄기
}
delay(1000); // 1초 대기 후 다시 측정
}