代码链接:
链接:https://pan.baidu.com/s/18d3Ir5z23if6hAobTlm_wg?pwd=get
提取码:ghet
效果展示:
https://www.bilibili.com/video/BV1Ue4y1u72n/?spm_id_from=333.999.0.0&vd_source=b4fe873cf504d579f6fe38abcb5c52c5
这是一个智能窗户的开窗系统,能够定时开关窗,根据外部环境自动调节,外部环境的参数主要有:温度,湿度,空气污染指数,是否下雨。
思维流程图
根据系统的功能,确定传感器,单片机的选型。
思维导图
代码
/*
实验内容:智能窗户系统
作者:红石榴汽水儿
时间:20-7-20,20-7-22,20-9-23
*/
/*
20-7-22,
程序更加模块化,注意程序的结构,循环有序,输出也更加简洁明了,增加蓝牙模块
*/
#include <Arduino.h>
#include <MQ135.h> //烟雾传感器
#include <dht11.h> //温湿度
#include "DS1302.h" //时钟模块
#include <Servo.h> //舵机
#include <String.h>
#include <LiquidCrystal.h>//LCD液晶屏
#define DHT11_PIN 4 //温湿度传感器
#define MQ_PIN A3 //定义烟雾传感器端口
#define PIR_PIN 3 //红外检测端口,中断引脚
#define WATER_PIN A2 //雨滴传感器数据引脚WATER_PIN
#define WINDOW_UP_BUTTON 9 //手动控制窗户开关的按钮
#define WINDOW_DOWN_BUTTON 10
#define SwitchB_D12PIN 12 //手动模式切换按钮 为12
unsigned int LED=8; //手动模式指示灯 为13
//boolean buttonUP=true; //flag 之前被按下过为true
Servo myservo; //舵机
dht11 DHT11; //温湿度传感器函数
MQ135 gasSensor = MQ135(MQ_PIN); //定义烟雾传感器的函数
LiquidCrystal lcd(22,24,26,28,30,32);//LCD1602的引脚,rs,en,d4,d5,d6,d7
//电机的驱动设置
const int MOTOR1_DirPin=44; //电机旋转方向,高电平逆时针,低电平顺时针
const int MOTOR1_StepPin=46; //步进
const int MOTOR1_EnPin=48; //使能引脚
const int MOTOR1_STEP_PER_REV=200; //全步进旋转一周
//电机二
const int MOTOR2_DirPin=45; //电机旋转方向,高电平逆时针,低电平顺时针
const int MOTOR2_StepPin=47 ; //步进
const int MOTOR2_EnPin=49; //使能引脚
const int MOTOR2_STEP_PER_REV=200; //全步进旋转一周
unsigned int MQflag,Pirflag,Dhtflag,Timeflag,Motorflag=0,Waterflag,Runflag=0; //变量标志,窗户开始处于关闭状态
unsigned int waterflag=800;
unsigned int motorSpeed=1000; //电机的运转速度
float ppmflag=400; //空气质量标准
float humflag=90, tempflag_1=10,tempflag_2=38; //空气湿度的临界值,温度的临界值
float rzero,ppm,temp,hum;
unsigned int water;//变量
unsigned char Alarm_Hour=7; //定时
unsigned char Alarm_Minute=30;
unsigned char hour,minute;
bool Always=false;
String comdata;
unsigned int comdata_clock_hour;
unsigned int comdata_clock_min;
unsigned char BUTTONState;
//舵机角度参数
unsigned int frist_pos=0;
unsigned int final_pos=180;
void Display_RTCC();
void Display_Begin();
void Display_HC08();
void Init();
void Choose();
void Motor_run();
void Motor_CLOCK();
void Motor_ANTICLOCK();
void Motor_STOP();
void sensor_Time();
void sensor_DHT11();
void sensor_MQ135();
void sensor_Water();
void sensor_Pir();
void Scan_Input();
void Set_Time(String &command);
void help();
void setup() {
Serial.begin(9600); //设置波特率;
Serial3.begin(9600);
Serial.print("Temp Test Code:");
Serial.println("OK ");
Serial.println("Intelligence Window");
//时钟初始化
DS1302_Init();
delay(200);
DS1302_GetTime(&DS1302Buffer);
/*舵机初始化*/
myservo.attach(50);
myservo.write(frist_pos);
delay(20);
/*LCD1602模块初始化*/
lcd.clear();
lcd.begin(16,2); //屏幕的高度和宽度
lcd.setCursor(0,0);
lcd.print("Intelligent");
lcd.setCursor(0,1);
lcd.print("Window System ");
for(int i=0;i<3;i++)
{
lcd.setCursor(14,0);
lcd.print(i);
delay(1000);
}
lcd.clear();
delay(20);
写代码及调试过程
中间真的出现过很多问题,让我意识到了电器基本元件的重要性。写代码的过程,基本就是边写边调试的过程,其实对于我一个小白来说,还是有点问题的。
最后,作为一个新手写代码,中间会有很多问题,希望能够多多交流。
如果想要代码,评论区留言即可。
既然看到了这里,那就点个赞再走吧(感谢)!!!