PCB做出来了
下面是效果图:
原理图:
PCB layout:
进度慢,没辙,课程压力太大了(作者菜)
PCB版本的改进了按键检测机制:添加了外部中断
下面是代码:
#include <Arduino.h>
#include <SimpleDHT.h>
#include "U8glib.h"
//#include <U2g2lib.h>
#define DHTpin 4 //自己按需求修改
#define Red_led 8
#define key_1 2
#define key_2 3
#define relay 5
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);// 初始化i2c接口
SimpleDHT11 dht11(DHTpin);//初始化DHT11对象
String tmp;
String hmp;
String KHS = "60";//显示阈值
int KH=60; //实际阈值
void draw_hello(void) {
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 22, "Hello Baby!");
}
void draw_data(void) {
//具体显示屏显示内容
u8g.setFont(u8g_font_unifont);//设置字体
u8g.drawStr( 0, 16, tmp.c_str());//写入字符串
u8g.drawStr( 0, 32, hmp.c_str());
u8g.drawStr( 15, 48, KHS.c_str());
}
void setup() {
// put your setup code here, to run once:
/*GPIO初始化*/
pinMode(Red_led,OUTPUT);
//pinMode(8, OUTPUT);//不知道干啥的
pinMode(key_1,INPUT);
pinMode(key_2,INPUT);
pinMode(relay,OUTPUT);
attachInterrupt( digitalPinToInterrupt(key_1), KEY1DOWN, CHANGE);
attachInterrupt( digitalPinToInterrupt(key_2), KEY2DOWN, CHANGE);
/*串口初始化*/
Serial.begin(115200);
draw_hello();
delay(1500);
/*Here is nothing!!*/
/*这是一个空行*/
// flip screen, if required
// u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop() {
/*led指示灯闪烁*/
digitalWrite(Red_led,HIGH);
delay(500);
digitalWrite(Red_led,LOW);
delay(500);
//draw_hello();
//delay(1200);
/*DHT11读取并串口打印显示*/
byte temperature = 0;
byte humidity = 0;
byte humidity_last = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
humidity_last = humidity;
//字符串拼接
tmp += String("Temperature:");
tmp += String(temperature);
tmp += String("*C");
hmp += String("Humidity: ");
hmp += String(humidity);
hmp += String("RH%");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
if(humidity<KH){
digitalWrite(relay,HIGH);
//digitalWrite(A0,LOW);
Serial.println("Warnning!");
}
if(humidity>=KH){
//digitalWrite(A0,HIGH);
digitalWrite(relay,LOW);
}
/*oled显示屏显示程序*/
u8g.firstPage();
do {
if(humidity != humidity_last){
u8g.drawStr( 0, 16, tmp.c_str());
u8g.drawStr( 0, 32, " ");
delay(1000);
}
draw_data();
} while( u8g.nextPage() );
}
void KEY1DOWN(){
KH++;
KHS=String(KH);
}
void KEY2DOWN(){
KH--;
KHS=String(KH);
}