提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、器件
- 二、代码
- 成果图![初次检测](https://i-blog.csdnimg.cn/blog_migrate/0d8b190ea9a1c649240056d86a09c261.jpeg#pic_center)![显示温湿度](https://i-blog.csdnimg.cn/blog_migrate/0cca3d450ae74670dd20335202bf8b3d.jpeg#pic_center)![最终效果](https://i-blog.csdnimg.cn/blog_migrate/4ba96cc62d3394413ca597dc7d56dc4f.jpeg#pic_center)
- 总结
前言
今天带来的是基于arduino实现的智能窗户,可用于实现自动开关窗、检测温湿度、检测光强、检测瓦斯并实施报警功能。
一、器件
1.arduino开发板一块
2.dht11温湿度传感器一块
3.无源蜂鸣器一块
4.光强传感器一块
5.雨滴传感器一块
6.9g舵机一块
7.杜邦线若干
8.5v电源线一根
二、代码
1.引入库
代码如下(示例):
#include <dht.h>//温湿度传感器库文件
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);
dht DHT;
Servo myservo;
const int DHT11_PIN=7;//定义引脚
const int analogPin=A3;
const int digitalPin=7;
const int analogPin1=A2;
const int digitalPin1=8;
const int ledPin=13;
const int photocellPin = A5;
int buzzerPin =6;//设置初始值
int Astate=0;
boolean Dstate=0;
int Astate1=0;
boolean Dstate1=0;
int outputValue = 0;
char array2[]="This is my job. "; //the string to print on the LCD
char array1[]="Hello everybody. "; //the string to print on the LCD
void setup()
{
myservo.attach(9);//舵机引脚
myservo.write(0);
delay(1000);
Serial.begin(9600);//波特率
lcd.init();
lcd.backlight();
pinMode(ledPin,OUTPUT);
pinMode(digitalPin,INPUT);
pinMode (digitalPin1,INPUT);
pinMode(buzzerPin,OUTPUT);
digitalWrite(buzzerPin,LOW);
}
void loop()
{
Astate=analogRead(analogPin);
Dstate=digitalRead(digitalPin);
Astate1=analogRead(analogPin1);
Dstate1=digitalRead(digitalPin1);
outputValue = analogRead(photocellPin);
D: int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
break;
case DHTLIB_ERROR_CHECKSUM:
break;
case DHTLIB_ERROR_TIMEOUT:
goto D;
break;
default:
break;
}
lcd.setCursor(15,0); // set the cursor to column 15, line 0
for (int positionCounter1 = 0; positionCounter1 < 16; positionCounter1++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array1[positionCounter1]); // Print a message to the LCD.
delay(400); //wait for 250 microseconds
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
lcd.setCursor(15,1); // set the cursor to column 15, line 1
for (int positionCounter = 0; positionCounter < 26; positionCounter++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array2[positionCounter]); // Print a message to the LCD.
delay(400); //wait for 250 microseconds
}
lcd.clear();
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Tem:");
lcd.print(DHT.temperature,1);
lcd.print(char(223));
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum:");
lcd.print(DHT.humidity,1);
lcd.print(" %");
delay(2000); //wait a while
Serial.print(Dstate);
Serial.println(outputValue);
if(DHT.temperature<20.0 || Dstate==LOW || Dstate1==LOW||outputValue <= 400)//判断条件
{
myservo.write(90);
digitalWrite(ledPin,HIGH);
digitalWrite(buzzerPin,HIGH);
delay(10000);
digitalWrite(ledPin,LOW);
digitalWrite(buzzerPin,LOW);
}
else
{
myservo.write(0);
digitalWrite(ledPin,LOW);
digitalWrite(buzzerPin,LOW);
}
}
成果图![初次检测](https://i-blog.csdnimg.cn/blog_migrate/0d8b190ea9a1c649240056d86a09c261.jpeg#pic_center)
![显示温湿度](https://i-blog.csdnimg.cn/blog_migrate/0cca3d450ae74670dd20335202bf8b3d.jpeg#pic_center)
![最终效果](https://i-blog.csdnimg.cn/blog_migrate/4ba96cc62d3394413ca597dc7d56dc4f.jpeg#pic_center)
总结
基本实现了预期功能,还可以进一步改进,例如提高检测精度,加上gsm通信模块实现报警感知。