這個結構簡化成包含
DHT11:Vcc --> 5V, Gnd --> Gnd, Data --> D6
Relay:Vcc --> 5V, Gnd --> Gnd, IN1 --> D4(風扇), IN2 --> D5(加熱管), 線路常開
1602LCD:Vcc --> 5V, Gnd --> Gnd, SDA --> A4, SCK --> A5
1602自訂義顯示字元可以看這邊
http://www.8051projects.net/lcd-interfacing/lcd-custom-character.php
這篇把 1602 變俄羅斯方塊動畫了
https://www.youtube.com/watch?v=ZarVHGqWNkk
#include <Wire.h>// Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 6
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2,1,0,4,5, 6,7, 3, POSITIVE);// Set the LCD I2C address
int pos[] = {0,0};
int tempSet = 25;
int heaterPin = 4, fansPin = 5;
int offset = -1;
float temperatureDHT = 25.0, tempUpperLimit = 25.5, tempLowerLimit = 24.5;
boolean ifHeating = false;
byte customChar[8] = { B00111, B00101, B00111, B00000, B00000, B00000, B00000, B00000 };
#define DEBOUNCE_DELAY 200//解決物理按鈕多次切換問題
static unsigned long lastDebounceTime;
void setup(){
bgBlink();
lcd.backlight();
lcd.setCursor(0,0);
lcd.createChar (0, customChar);
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, pgCntPlus, FALLING);
attachInterrupt(1, pgCntMinus, FALLING);
pinMode(heaterPin, OUTPUT);
digitalWrite(heaterPin, HIGH);
pinMode(fansPin, OUTPUT);
digitalWrite(fansPin, HIGH);
}
void loop(){
int chk = DHT11.read(DHT11PIN);
if(chk==DHTLIB_OK){
temperatureDHT = (int)DHT11.temperature + offset;
delay(2000);
}
showTmpr();
if(ifHeating && (float)temperatureDHT>tempUpperLimit){
ifHeating = false;
digitalWrite(heaterPin,HIGH);
digitalWrite(fansPin,HIGH);
}
if(!ifHeating && temperatureDHT<(float)tempLowerLimit){
ifHeating = true;
digitalWrite(heaterPin,LOW);
digitalWrite(fansPin,LOW);
}
}
void pgCntPlus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
tempSet++;
tempUpperLimit = tempSet + 0.5;
tempLowerLimit = tempSet - 0.5;
}
}
void pgCntMinus(){
unsigned long currentTime = millis();
if((currentTime - lastDebounceTime) > DEBOUNCE_DELAY){
lastDebounceTime = currentTime;
tempSet--;
tempUpperLimit = tempSet + 0.5;
tempLowerLimit = tempSet - 0.5;
}
}
void bgBlink(){
lcd.begin(16,2);// initialize the lcd for 20 chars 4 lines and turn on backlight
for(int i = 0; i< 3; i++){
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
}
void showTmpr(){
//(col,row)
pos[0] = 0; pos[1] = 0;
lcd.setCursor(0,0);
showMsg(pos, "Set:");
lcd.setCursor(4,0);
lcd.print(tempSet);
lcd.print(char(0));
lcd.print('C');
pos[0] = 0; pos[1] = 1;
showMsg(pos, "Now:");
lcd.print((int)temperatureDHT);
lcd.print(char(0));
lcd.print('C');
}
void showMsg(int *pos, int arrt){
//(col,row)
lcd.setCursor(pos[0],pos[1]);
lcd.print(arrt);
}
void showMsg(int *pos, float arrt){
//(col,row)
lcd.setCursor(pos[0],pos[1]);
lcd.print(arrt);
}
void showMsg(int *pos, char *c){
lcd.setCursor(pos[0],pos[1]);
while(*c){
lcd.print(*c++);
}
}
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius){
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius){
return celsius + 273.15;
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity){
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: 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*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}