释放双眼,带上耳机,听听看~!
目录
简介
模拟浇水+光控灯光
完整程序
#include //dht11dht11 DHT11;
#include <Servo.h>//舵机
Servo servoTemp;
#define servoPin 6 //舵机数字针脚
//LCD模块
//#include
//#include
//LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 and 0x3F for a 16 chars and 2 line display #define DHT11PIN 7
//DHT11 连接的数字针脚//光照检测模块数学针脚
#define redPin 11//red
#define greenPin 10//green
#define bluePin 9//blue
#define photocellPin A0//光敏连接引针
int IlluminanceValue = 0;//光照度数值
#define relayPin 12//继电器数字引脚
int Filter_Value;//滤波值
#define FILTER_N 12
/******************函数部分****************/int Filter_Aaf(); //算术平均滤波int Get_AD();//取引脚值
void tah();//温湿度void servo();//舵机角度
void rgb();//rgb-lightvoid
Illuminance();//光照
/*A、名称:算术平均滤波法 Arithmetic average filtering methodB、
方法: 连续取N个采样值进行算术平均运算:
N值较大时:信号平滑度较高,但灵敏度较低;
N值较小时:信号平滑度较低,但灵敏度较高;
N值的选取:一般流量,N=12;压力:N=4。
C、优点: 适用于对一般具有随机干扰的信号进行滤波;
这种信号的特点是有一个平均值,信号在某一数值范围附近上下波动。
D、缺点: 对于测量速度较慢或要求数据计算速度较快的实时控制不适用;
比较浪费RAM。
E、整理:shenhaiyu 2013-11-01 https://blog.csdn.net/u012572886/article/details/78785343
F、
修改函数:LadyNine 2019-6-4*/
int Filter_Aaf(int pin)
{
int i;
int filter_sum = 0;
for(i = 0; i < FILTER_N; i++)
{
filter_sum += Get_AD(pin);
delay(1);
}
return (int)(filter_sum / FILTER_N);
}
int Get_AD(int pin)
{
return analogRead(pin);
}
/*************温度和温度*************/
void tah()//temperature and humidity 温度和湿度
{
Serial.println("n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("humidity (%): ");
Serial.println((float)DHT11.humidity, 2); //输出湿度
Serial.print("temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);//输出温度
//根据湿度调整舵机角度
if(DHT11.humidity<40) //湿度小于40%
servo(180);//浇水
else
servo(0);//不浇水
delay(100);
}
/********舵机函数**********/
void servo(unsigned char number)//舵机
{
servoTemp.write(number);//设置角度
}
/********光敏函数**********/
void Illuminance()//光照度
{
IlluminanceValue = Filter_Aaf(photocellPin);//read the value of photoresistor
Serial.print("Illuminance="); //IlluminanceValue 实为光敏电阻数值 光照愈强,阻值就愈低 光照愈弱,阻值就愈高
Serial.println(1024-IlluminanceValue); //print it in serial monitor
if(1024-IlluminanceValue >= 400) //if the value of photoreisitor is greater than 400
{
rgb(0,0,0);//RGB黑
//digitalWrite(relayPin, LOW); //disconnect the relay
// delay(1000); //wait for 1 second
}
else
{
rgb(255,255,255);//RGB白
// digitalWrite(relayPin, HIGH); //Close the relay
// delay(1000); //wait for 1 second
}
delay(100);
}
/**********rgb light********/
void rgb(unsigned char red,unsigned char green,unsigned char blue)
{
analogWrite(redPin,red);
analogWrite(bluePin,blue);
analogWrite(greenPin,green);
}
void setup()
{
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(relayPin, OUTPUT);
//pinMode(photocellPin,INPUT);
//pinMode(DHT11PIN,INPUT);
servoTemp.attach(servoPin);
Serial.begin(9600);
//randomSeed(analogRead(0)); // 产生随机种子
/*************DHT11 TEST PROGRAM**************/
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop()
{
tah();//调用温湿度函数
Illuminance();//调用光照函数
delay(1000);
}
/*库文件*/
#include //dht11dht11 DHT11;
#include //舵机
Servo servoTemp;#define servoPin 6 //舵机数字针脚
//LCD模块
//#include
//#include
//LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 and 0x3F for a 16 chars and 2 line display
#define DHT11PIN 7 //DHT11 连接的数字针脚//光照检测模块数学针脚#define redPin 11//red
#define greenPin 10//green#define bluePin 9//blue#define photocellPin A0//光敏连接引针
int IlluminanceValue = 0;//光照度数值#define relayPin 12//继电器数字引脚
int Filter_Value;//滤波值#define FILTER_N 12
/******************函数部分****************/int Filter_Aaf(); //算术平均滤波int Get_AD();//取引脚值
void tah();//温湿度void servo();//舵机角度
void rgb();//rgb-lightvoid
Illuminance();//光照
/*A、名称:算术平均滤波法 Arithmetic average filtering methodB、
方法: 连续取N个采样值进行算术平均运算:
N值较大时:信号平滑度较高,但灵敏度较低;
N值较小时:信号平滑度较低,但灵敏度较高;
N值的选取:一般流量,N=12;压力:N=4。
C、优点: 适用于对一般具有随机干扰的信号进行滤波;
这种信号的特点是有一个平均值,信号在某一数值范围附近上下波动。
D、缺点: 对于测量速度较慢或要求数据计算速度较快的实时控制不适用;
比较浪费RAM。
E、整理:shenhaiyu 2013-11-01 https://blog.csdn.net/u012572886/article/details/78785343
F、
修改函数:LadyNine 2019-6-4*/int Filter_Aaf(int pin)
{
int i;
int filter_sum = 0;
for(i = 0; i < FILTER_N; i++)
{
filter_sum += Get_AD(pin);
delay(1);
}
return (int)(filter_sum / FILTER_N);
}
int Get_AD(int pin)
{
return analogRead(pin);
}
/*************温度和温度*************/
void tah()//temperature and humidity 温度和湿度
{
Serial.println("n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("humidity (%): ");
Serial.println((float)DHT11.humidity, 2); //输出湿度
Serial.print("temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);//输出温度
//根据湿度调整舵机角度
if(DHT11.humidity<40) //湿度小于40%
servo(180);//浇水
else
servo(0);//不浇水
delay(100);
}
/********舵机函数**********/
void servo(unsigned char number)//舵机
{
servoTemp.write(number);//设置角度
}
/********光敏函数**********/
void Illuminance()//光照度
{
IlluminanceValue = Filter_Aaf(photocellPin);//read the value of photoresistor
Serial.print("Illuminance="); //IlluminanceValue 实为光敏电阻数值 光照愈强,阻值就愈低 光照愈弱,阻值就愈高
Serial.println(1024-IlluminanceValue); //print it in serial monitor
if(1024-IlluminanceValue >= 400) //if the value of photoreisitor is greater than 400
{
rgb(0,0,0);//RGB黑
//digitalWrite(relayPin, LOW); //disconnect the relay
// delay(1000); //wait for 1 second
}
else
{
rgb(255,255,255);//RGB白
// digitalWrite(relayPin, HIGH); //Close the relay
// delay(1000); //wait for 1 second
}
delay(100);
}
/**********rgb light********/
void rgb(unsigned char red,unsigned char green,unsigned char blue)
{
analogWrite(redPin,red);
analogWrite(bluePin,blue);
analogWrite(greenPin,green);
}
void setup()
{
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(relayPin, OUTPUT);
//pinMode(photocellPin,INPUT);
//pinMode(DHT11PIN,INPUT);
servoTemp.attach(servoPin);
Serial.begin(9600);
//randomSeed(analogRead(0)); // 产生随机种子
/*************DHT11 TEST PROGRAM**************/
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop()
{
tah();//调用温湿度函数
Illuminance();//调用光照函数
delay(1000);
}