아두이노 알람시계 코드 수정
밑에 코드를 수정해야 하는데 어떻게 해야할지 막히네요
✅최고의 답변✅
요청하신 내용에 기반한 아두이노 알람 시계 코드입니다.
코드에는 DHT11 또는 DHT22와 같은 온습도 센서를 사용하고
푸쉬 버튼을 통해 LCD에 온습도를 표시하도록 구현되어 있습니다.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <swRTC.h>
#include <DHT.h> // DHT 라이브러리를 추가
#define DHT_PIN 7 // 온습도 센서 핀 설정
#define DHT_TYPE DHT22 // DHT11 또는 DHT22를 사용
LiquidCrystal_I2C lcd(0x3f, 16, 2);
swRTC rtc;
const int piezo = 6;
const int switchPin = 9;
int alarmTime = 0;
bool bAlarmRun = false;
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
rtc.stopRTC();
rtc.setDate(1, 4, 2021);
rtc.setTime(9, 48, 0);
rtc.startRTC();
pinMode(piezo, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
Serial.begin(115200);
dht.begin(); // DHT 센서 초기화
}
void Set_LowThanTen(const int time) {
if (time < 10) {
lcd.print(0);
}
lcd.print(time);
}
void loop() {
lcd.setCursor(0, 0);
Set_LowThanTen(rtc.getMonth());
lcd.print("/");
Set_LowThanTen(rtc.getDay());
lcd.print(" ");
if (rtc.getHours() >= 12) {
lcd.print("PM");
} else {
lcd.print("AM");
}
Set_LowThanTen(rtc.getHours());
lcd.print(":");
Set_LowThanTen(rtc.getMinutes());
lcd.print(":");
Set_LowThanTen(rtc.getSeconds());
// 온습도 센서 값을 LCD에 표시
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(dht.readTemperature());
lcd.print(" C, Humidity: ");
lcd.print(dht.readHumidity());
lcd.print("%");
Set_LowThanTen2(rtc.getMonth());
Serial.print("/");
Set_LowThanTen2(rtc.getDay());
Serial.print(" ");
if (rtc.getHours() >= 12) {
Serial.print("PM");
} else {
Serial.print("AM");
}
Set_LowThanTen2(rtc.getHours());
Serial.print(":");
Set_LowThanTen2(rtc.getMinutes());
Serial.print(":");
Set_LowThanTen2(rtc.getSeconds());
Serial.println("");
if (Serial.available()) {
char theTimes[5] = {0};
char ch;
int i = 0;
while (Serial.available()) {
ch = Serial.read();
if (i < 5) {
theTimes[i] = ch;
}
i++;
}
theTimes[i] = 0;
int tempTime = atoi(theTimes);
if (tempTime <= 2359 && tempTime / 100 < 24 && tempTime % 100 < 60) {
alarmTime = tempTime;
Serial.println("Success Time!!!");
} else {
Serial.println("Failed Time !!!");
}
}
lcd.setCursor(0, 1);
lcd.print("Alarm: ");
if (alarmTime / 100 >= 12) {
lcd.print("PM");
} else {
lcd.print("AM");
}
Set_LowThanTen(alarmTime / 100);
lcd.print(":");
Set_LowThanTen(alarmTime % 100);
if (rtc.getHours() == alarmTime / 100 && rtc.getMinutes() == alarmTime % 100) {
analogWrite(piezo, 128);
bAlarmRun = true;
}
const int switchState = digitalRead(switchPin);
if (0 == switchState && bAlarmRun == true) {
bAlarmRun = false;
alarmTime = 0;
analogWrite(piezo, 0);
Serial.println("Alarm clock Initialize");
Serial.println("AM00:00");
}
}
알람 시간도 설정하고, 알람 시간이 되면 부저가 울리게 됩니다.
푸쉬버튼이 눌린 경우에만 온습도 센서 값을 표시하도록 설정했습니다.