|
[mw_shl_code=cpp,true]#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include "DHT.h"
#define DHTPIN 9
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
#include <math.h>
int BH1750address = 0x23;//BH1750 I2C地址
byte buff[2];
#include "DS1307.h"
DS1307 clock;
int flag = 0;
int tem_h = 50;
int tem_l = 20;
int hum_h = 40;
int hum_l = 15;
int lux_h = 30;
int lux_l = 10;
void setup()
{ pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(5,INPUT);
pinMode(7,INPUT);
clock.setTime();
lcd.init();
bmp.begin();
dht.begin();
Wire.begin();
}
void loop()
{ float temp = bmp.readTemperature(); //读取温度数据 (BMP085)
long pa = bmp.readPressure(); //读取气压数据 (BMP085)
float hpa = pa /100.0 ; //将Pa换算成hPa
int hum = dht.readHumidity(); //读取湿度(DHT11)
float lux=BH1750();
int soil = analogRead(MOISTURE_SENSOR);
lcd.clear();
clock.getTime();
lcd.setCursor(0,0);
lcd.print(clock.hour, DEC);
lcd.print(":");
lcd.print(clock.minute, DEC);
lcd.print(" ");
lcd.print(clock.month, DEC);
lcd.print("/");
lcd.print(clock.dayOfMonth, DEC);
lcd.print("/");
lcd.print(clock.year+2000, DEC);
lcd.print(" ");
switch (clock.dayOfWeek)
{
case MON:
lcd.print("MON");
break;
case TUE:
lcd.print("TUE");
break;
case WED:
lcd.print("WED");
break;
case THU:
lcd.print("THU");
break;
case FRI:
lcd.print("FRI");
break;
case SAT:
lcd.print("SAT");
break;
case SUN:
lcd.print("SUN");
break;
}
lcd.print(" ");
lcd.backlight();
lcd.setCursor(0,1);
lcd.print("Hum:");
lcd.print((int)hum);
lcd.setCursor(0,2);
lcd.print("Tem:");
lcd.print((float)temp,2);
lcd.setCursor(0,3);
lcd.print("Pre:");
lcd.print((float)hpa,0);
lcd.setCursor(11,1);
lcd.print("Lux:");
lcd.print((float)lux,2);
if((flag==0)&&(digitalRead(2)))
{
flag = 1;
byte temByte_h = (int)(tem_h);
EEPROM.write(0,temByte_h);
byte temByte_l = (int)(tem_l);
EEPROM.write(1,temByte_l);
byte humByte_h = (int)(hum_h);
EEPROM.write(2,humByte_h);
byte humByte_l = (int)(hum_l);
EEPROM.write(3,humByte_l);
byte luxByte_h = (int)(lux_h);
EEPROM.write(4,luxByte_h);
byte luxByte_l = (int)(lux_l);
EEPROM.write(5,luxByte_l);
if(digitalRead(3))
{ tem_h++;
delay(100);
}
if(digitalRead(5))
{ tem_h--;
delay(100);
}
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Pr0");
lcd.setCursor(4,0);
lcd.print("H");
lcd.setCursor(7,0);
lcd.print("L");
lcd.setCursor(11,0);
lcd.print("pre");
lcd.setCursor(15,0);
lcd.print("H");
lcd.setCursor(18,0);
lcd.print("L");
lcd.setCursor(0,1);
lcd.print("Tem");
lcd.setCursor(4,1);
lcd.print(EEPROM.read(0));
lcd.setCursor(7,1);
lcd.print(EEPROM.read(1));
lcd.setCursor(0,2);
lcd.print("Hum");
lcd.setCursor(4,2);
lcd.print(EEPROM.read(2));
lcd.setCursor(7,2);
lcd.print(EEPROM.read(3));
lcd.setCursor(0,3);
lcd.print("Lux");
lcd.setCursor(4,3);
lcd.print(EEPROM.read(4));
lcd.setCursor(7,3);
lcd.print(EEPROM.read(5));
}
if((flag==1)&&(!digitalRead(2)))
{flag = 2;}
if((flag==2)&&(digitalRead(2)))
{flag=0;}
delay(5000);
}
double BH1750()
{
int i=0;
double val=0;
Wire.beginTransmission(BH1750address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
delay(200);
Wire.beginTransmission(BH1750address);
Wire.requestFrom(BH1750address, 2);
while(Wire.available()) //
{
buff = Wire.read(); // receive one byte
i++;
}
Wire.endTransmission();
if(2==i)
{
val=((buff[0]<<8)|buff[1])/1.2;
}
return val;
}[/mw_shl_code]
当我按下2号键切换到温湿度上下限调整时?主程序还在循环运行就切回到原来的温湿度显示上了,如何才能保持在参数调整的界面上,等我参数设置完了,按2号键再切回到主界面。不胜感激!另外我的这个想法用上面的程序合理吗?有没有其他更好的方法。小弟先谢谢了
|
|