嗯嗯,谢谢!
顺带问一个问题,我的BH1750和DS3231同时在显示屏上显示时就白屏,是怎么回事。能帮我改一下吗,我是新手,不会用I2C。代码是找人家的,然后拼在一起的
#include <UTFT.h>
#include <dht11.h>
#include "SR04.h"
#include <DS3231.h>
#include <Wire.h>
#include <math.h>
int BH1750address = 0x23;
int DS3231address = 0x68;
byte buff[2];
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
UTFT myGLCD(QD220A,A2,A1,A5,A4,A3); // Remember to change the model parameter to suit your display module!
dht11 DHT11;
int ledpin=(5,6);
#define DHT11PIN 2
#define TRIG_PIN 3
#define ECHO_PIN 4
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
} //摄氏温度度转化为华氏温度
double Kelvin(double celsius)
{
return celsius + 273.15;
} //摄氏温度转化为开氏温度
// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// 快速计算露点,速度是5倍dewPoint()
// 参考: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
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;
}
void setup()
{
randomSeed(analogRead(0));
myGLCD.InitLCD();
myGLCD.clrScr();
pinMode(ledpin,OUTPUT);
Wire.begin();
Clock.setSecond(50);//Set the second
Clock.setMinute(20);//Set the minute
Clock.setHour(17); //Set the hour
Clock.setDoW(5); //Set the day of the week
Clock.setDate(19); //Set the date of the month
Clock.setMonth(6); //Set the month of the year
Clock.setYear(15); //Set the year (Last two
}
void ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
}
void loop()
{
int buf[218];
int x, x2;
int y, y2;
int r;
int i;
uint16_t val=0;
ReadDS3231();
int chk = DHT11.read(DHT11PIN);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.setColor(0, 255, 0);
myGLCD.print("20", 2, 6);
myGLCD.printNumI(Clock.getYear(), 35, 6);
myGLCD.print("/", 65, 6);
myGLCD.setColor(150, 150, 0);
myGLCD.printNumI(Clock.getMonth(Century), 95, 6);
myGLCD.print("/", 115, 6);
myGLCD.setColor(200, 200, 200);
myGLCD.printNumI(Clock.getDate(), 135, 6);
myGLCD.setColor(VGA_FUCHSIA);
myGLCD.printNumI(Clock.getHour(h12, PM), 30, 33);
myGLCD.print(":", 63, 33);
myGLCD.setColor(VGA_TEAL);
myGLCD.printNumI(Clock.getMinute(), 75, 33);
myGLCD.print(":", 105, 33);
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(Clock.getDoW(), 184, 2);
myGLCD.setFont(SmallFont);
myGLCD.setColor(150, 10, 0);
myGLCD.printNumI(Clock.getSecond(), 121, 37);
myGLCD.setColor(200, 100, 20);
myGLCD.print("Illuminance", 2, 102);
myGLCD.printNumI(val, 103, 102);
myGLCD.setColor(30, 10, 102);
myGLCD.print("Lx", 118, 102);
a=sr04.Distance();
myGLCD.setColor(255, 255, 0);
myGLCD.print("Distance", 2, 122);
myGLCD.printNumI(a, 103, 122);
myGLCD.setColor(255, 0, 0);
myGLCD.print("cm", 118, 122);
myGLCD.setColor(0, 255, 0);
myGLCD.print("Humidity", 2, 142);
myGLCD.printNumI(DHT11.humidity, 103, 142);
myGLCD.setColor(0, 0, 255);
myGLCD.print("%", 118, 142);
if ( DHT11.humidity<=40)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("(Dry)", 160, 142);
}
else if( DHT11.humidity<=60)
{
myGLCD.setColor(200, 200, 0);
myGLCD.print("(Comefort)", 137, 142);
}
else
{
myGLCD.setColor(0, 0, 200);
myGLCD.print("(Moist)", 160, 142);
}
myGLCD.setColor(255, 0, 255);
myGLCD.print("Temperature", 2, 162);
myGLCD.printNumI(DHT11.temperature, 103, 162);
myGLCD.setColor(200, 255, 50);
myGLCD.print("oC", 118, 162);
if ( DHT11.temperature<=25)
{
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
myGLCD.setColor(0, 0, 255);
myGLCD.print("(Cold)", 160, 162);
}
else if( DHT11.temperature<=30)
{
myGLCD.setColor(200, 200, 0);
myGLCD.print("(Comfort)", 137, 162);
}
else
{
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
myGLCD.setColor(255, 0, 0);
myGLCD.print("(Hot)", 160, 162);
}
myGLCD.setColor(0, 0, 255);
myGLCD.drawRect(0, 0, 218, 174); //外框
myGLCD.drawLine(90, 95, 90, 172); //传感器名称右侧竖线
myGLCD.drawLine(136, 95, 136, 172); //传感器显示数值右侧竖线
myGLCD.drawLine(180, 2, 180, 52); //星期左侧竖线
myGLCD.drawLine(180, 52, 219, 52); //星期下面横线
myGLCD.drawLine(1, 28, 181, 28); //日期下面横线
myGLCD.drawLine(1, 52, 181, 52); //时间下面横线
myGLCD.drawLine(1, 95, 219, 95); //光照度上面横线
myGLCD.drawLine(1, 118, 219, 118); //距离上面横线
myGLCD.drawLine(1, 138, 219, 138); //湿度上面横线
myGLCD.drawLine(1, 158, 219, 158); //温度上面横线
delay (1000);
}
|