一、电路连接
需要以下几个外设
- LCD1602(IIC驱动)
- DS1302
- 1-WIRE温湿度检测器
- 红外接收器
- 遥控器
- 两个LED(一红一蓝)
- 蜂鸣器
LCD1602IIC
LCD1602IIC引脚 | Arduino引脚 |
---|
VCC | 5V | GND | GND | SDA | A4 | SCL | A5 |
我这里的LCD1602是IIC的,所以只需要4根线
1-WIRE温湿度检测器
中间的线是要接5V的
红外接收器
红外接收器引脚 | Arduino引脚 |
---|
- | GND | + | 5V | S | 11 |
DS1302
DS1302引脚 | Arduino引脚 |
---|
VCC | 5V | GND | GND | RST | A0(14) | DAT | A1(15) | SCK | A2(16) |
蜂鸣器
蜂鸣器引脚 | Arduino引脚 |
---|
- | GND | + | 5V | I/O | 5 |
LED
LED引脚 | Arduino引脚 |
---|
红色LED | 6 | 蓝色LED | 7 |
二、程序
这里需要下载4个库:
- dht11
- LiquidCrystal_I2C
- IRremote
- DS1302
我把他们都传到我的Github里面了:SmartClock 程序里面的红外遥控器是按任何一个按键都可以切换模式,因为我这个遥控器的解码有点问题,可能是这个库的时序问题,大家可以自己个性化自己的按键,改一下就能用了
功能简介
LCD1602屏默认显示温度和湿度,在按下遥控器任何一个按键时,LCD1602屏显示时间,再按下显示温度和湿度。温度湿度还有时间数据都会在串口打印出来,方便调试。在温度高于一定阈值之后,亮红灯,在湿度低于一定阈值后,亮蓝灯。时钟到设定的时间后,蜂鸣器叫10声。
#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1302.h>
#include<IRremote.h>
#define dht11Pin 8
#define ledBlue 6
#define ledRed 7
#define beep 5
dht11 dht;
char buf1[50];
char buf2[50];
LiquidCrystal_I2C lcd(0x27,16,2);
DS1302 rtc(14, 15, 16);
int RECV_PIN = 11;
int Keynum=1;
IRrecv irrecv(RECV_PIN);
decode_results results;
void initRTCTime(void)
{
rtc.writeProtect(false);
rtc.halt(false);
Time t(2022, 12, 19, 16, 25, 50, 4);
rtc.time(t);
}
void printTime()
{
Time tim = rtc.time();
snprintf(buf1, sizeof(buf1), "%04d-%02d-%02d ",
tim.yr, tim.mon, tim.date
);
snprintf(buf2, sizeof(buf2), "%02d:%02d:%02d",
tim.hr, tim.min, tim.sec);
Serial.println(buf1);
Serial.println(buf2);
}
void setup()
{
Serial.begin(9600);
pinMode(dht11Pin, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(beep, OUTPUT);
lcd.init();
lcd.backlight();
initRTCTime();
irrecv.enableIRIn();
delay(2000);
}
void loop()
{
if (irrecv.decode(&results)!=0)
{
delay(500);
Keynum++;
delay(500);
lcd.clear();
if(Keynum>=3)
{
Keynum=1;
}
irrecv.resume();
}
switch(Keynum)
{
case 1: SerialTem();ther();TemJudge();alarm();break;
case 2: printTime();TemJudge();
Time tim = rtc.time();
lcd.setCursor(0,0);
lcd.print(buf1);
lcd.setCursor(0,1);
lcd.print(buf2);
alarm();
break;
default: break;
}
}
void ther()
{
int tol = dht.read(dht11Pin);
int temp = (float)dht.temperature;
int humi = (float)dht.humidity;
lcd.setCursor(0,0);
lcd.print("Tem:");
lcd.setCursor(4,0);
lcd.print(temp);
lcd.setCursor(6,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Hum:");
lcd.setCursor(4,1);
lcd.print(humi);
lcd.setCursor(6,1);
lcd.print("%");
}
void TemJudge()
{
int tol = dht.read(dht11Pin);
int temp = (float)dht.temperature;
int humi = (float)dht.humidity;
if(temp>=20)
{
digitalWrite(ledBlue,HIGH);
}
else digitalWrite(ledBlue,LOW);
if(humi>=34)
{
digitalWrite(ledRed,HIGH);
}
else digitalWrite(ledRed,LOW);
}
void alarm()
{
digitalWrite(beep,HIGH);
int alarm_hr=16;
int alarm_min=26;
int alarm_sec=0;
Time tim = rtc.time();
snprintf(buf2, sizeof(buf2), "%02d:%02d:%02d",
tim.hr, tim.min, tim.sec);
if((tim.hr==alarm_hr)&&(tim.min==alarm_min)&&(tim.sec==alarm_sec))
{
for(int i=0;i<20;i++)
{
digitalWrite(beep,LOW);
delay(100);
digitalWrite(beep,HIGH);
delay(100);
}
}
}
void SerialTem()
{
int tol = dht.read(dht11Pin);
int temp = (float)dht.temperature;
int humi = (float)dht.humidity;
Serial.print("Temperature:");
Serial.print(temp);
Serial.println(".C");
Serial.print("Humidity:");
Serial.print(humi);
Serial.println("%");
}
三、效果
|