案例11:自动浇水系统(综合性任务A)
1.电路连接
温湿度传感器:
VCC(+)->电源正极(5V)
GND(-)->电源负极(0V)
OUT->Arduino 8脚
OLED显示屏:
VCC(+)->电源正极(5V)
GND(-)->电源负极(0V)
SCL->Arduino A5脚
SDA->Arduino A4脚
电磁继电器:
蓝色座子区:
常开->水泵红线(水泵的正极)
公共端->电源正极(5V)
常闭->不接
小排针区:
VCC(+)->电源正极(5V)
GND(-)->电源负极(0V)
IN->Arduino 9脚
2.示例程序
/*南京市金陵中学高一年级选修课
测试功能:自动浇花系统
测试时间:2020年10月
*/
#include <dht11.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define waterPump 9
#define DHT11PIN 8
dht11 DHT11;
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
static const unsigned char PROGMEM shi[] = {0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00};/*"湿"*/
static const unsigned char PROGMEM du[] = {0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E};/*"度"*/
static const unsigned char PROGMEM tu[] = {0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00};/*"土"*/
static const unsigned char PROGMEM rang[] = {0x20,0x40,0x27,0xFC,0x20,0x00,0x23,0xB8,0xF2,0xA8,0x23,0xB8,0x21,0x10,0x27,0xFC,
0x21,0x10,0x27,0xFC,0x31,0x10,0xEF,0xFE,0x41,0x28,0x03,0x10,0x0D,0x48,0x01,0x86};/*"壤"*/
static const unsigned char PROGMEM dot[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00};/*":"*/
static const unsigned char PROGMEM temp[] = {0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00};/*"℃"*/
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(WHITE);//开像素点发光
pinMode(waterPump,OUTPUT);
}
void loop() {
int humidtyRaw = analogRead(A0);//1023 to 0 ==>0 to 100%
int humidtyReal = map(humidtyRaw,1023,0,0,100);
int chk = DHT11.read(DHT11PIN);
float h = (float)DHT11.humidity;
float t = (float)DHT11.temperature;
display.clearDisplay();//清屏
if(humidtyReal > 30)
{
digitalWrite(waterPump,HIGH);
display.setTextSize(1); //设置字体大小
display.setCursor(24, 0);//设置显示位置
display.println("Good Humidty!");//输出字符
}else{
digitalWrite(waterPump,LOW);
}
/*显示环境湿度*/
display.setTextSize(2); //设置字体大小
display.setCursor(80, 32);//设置显示位置
display.print(h,0);//显示环境湿度值
display.setCursor(112, 32);//设置显示位置
display.println("%");//显示百分比
/*显示土壤湿度*/
display.drawBitmap(0, 48, tu, 16, 16, 1);//土
display.drawBitmap(16, 48, rang, 16, 16, 1);//壤
display.drawBitmap(32, 48, shi, 16, 16, 1);//湿
display.drawBitmap(48, 48, du, 16, 16, 1);//度
display.drawBitmap(64, 48, dot, 16, 16, 1);//:
display.setTextSize(2); //设置字体大小
display.setCursor(80, 48);//设置显示位置
display.print(humidtyReal);//显示土壤湿度值
display.setCursor(112, 48);//设置显示位置
display.println("%");//显示百分比
display.display();//开显示
delay(500);
}
3.任务要求
要求1.土壤干燥时,水泵不工作;土壤潮湿时,水泵工作。
要求2.通过显示屏实时显示土壤的干燥程度(土壤湿度传感器数值获取;OLED显示)
要求3.通过显示屏实时显示水泵的工作状态
要求4.通过显示屏实时显示其他传感器的工作状态(如环境温湿度传感器)
4.任务提示
1.示例程序已经完全可以满足要求1,要求1对应的代码在54-62行,是一个if…else结构。如果需要回顾什么是if…else结构,请点击if声明条件
2.示例程序已经可以完全满足要求2,对应的代码在72-82行,其中“土壤湿度”这几个汉字对应的屏幕点阵由取模软件生成,在17-34行。如果忘记了取模软件如何生成汉字,请点击取模软件显示汉字
3.示例程序可以部分满足要求3,对应的代码在54-62行,是一个if…else结构,在土壤湿度传感器感受到“潮湿”时,水泵的工作状态为“不工作”,通过OLED屏幕打印的文字为“Good Humidty!”。现在还需要在另一种状态下打印文字,意为“正在灌溉”。
4.示例程序可以部分满足要求4,对应的代码在49-51行,但需要注意的是,示例代码仅仅将变量h、变量t赋予了正确的传感器数值,但没有将其显示处理,需要认真研读任务提示2,先通过取模软件生成汉字对应的数组,再模仿72-82行的代码,打印“环境温度”、“环境湿度”两组汉字,并显示相应的传感器值。