案例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脚
image.png

2.示例程序

  1. /*南京市金陵中学高一年级选修课
  2. 测试功能:自动浇花系统
  3. 测试时间:2020年10月
  4. */
  5. #include <dht11.h>
  6. #include <Wire.h>
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_SSD1306.h>
  9. #define waterPump 9
  10. #define DHT11PIN 8
  11. dht11 DHT11;
  12. #define OLED_RESET 4
  13. Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
  14. static const unsigned char PROGMEM shi[] = {0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
  15. 0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00};/*"湿"*/
  16. static const unsigned char PROGMEM du[] = {0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
  17. 0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E};/*"度"*/
  18. static const unsigned char PROGMEM tu[] = {0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,
  19. 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00};/*"土"*/
  20. static const unsigned char PROGMEM rang[] = {0x20,0x40,0x27,0xFC,0x20,0x00,0x23,0xB8,0xF2,0xA8,0x23,0xB8,0x21,0x10,0x27,0xFC,
  21. 0x21,0x10,0x27,0xFC,0x31,0x10,0xEF,0xFE,0x41,0x28,0x03,0x10,0x0D,0x48,0x01,0x86};/*"壤"*/
  22. static const unsigned char PROGMEM dot[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23. 0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00};/*":"*/
  24. static const unsigned char PROGMEM temp[] = {0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
  25. 0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00};/*"℃"*/
  26. void setup() {
  27. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  28. display.setTextColor(WHITE);//开像素点发光
  29. pinMode(waterPump,OUTPUT);
  30. }
  31. void loop() {
  32. int humidtyRaw = analogRead(A0);//1023 to 0 ==>0 to 100%
  33. int humidtyReal = map(humidtyRaw,1023,0,0,100);
  34. int chk = DHT11.read(DHT11PIN);
  35. float h = (float)DHT11.humidity;
  36. float t = (float)DHT11.temperature;
  37. display.clearDisplay();//清屏
  38. if(humidtyReal > 30)
  39. {
  40. digitalWrite(waterPump,HIGH);
  41. display.setTextSize(1); //设置字体大小
  42. display.setCursor(24, 0);//设置显示位置
  43. display.println("Good Humidty!");//输出字符
  44. }else{
  45. digitalWrite(waterPump,LOW);
  46. }
  47. /*显示环境湿度*/
  48. display.setTextSize(2); //设置字体大小
  49. display.setCursor(80, 32);//设置显示位置
  50. display.print(h,0);//显示环境湿度值
  51. display.setCursor(112, 32);//设置显示位置
  52. display.println("%");//显示百分比
  53. /*显示土壤湿度*/
  54. display.drawBitmap(0, 48, tu, 16, 16, 1);//土
  55. display.drawBitmap(16, 48, rang, 16, 16, 1);//壤
  56. display.drawBitmap(32, 48, shi, 16, 16, 1);//湿
  57. display.drawBitmap(48, 48, du, 16, 16, 1);//度
  58. display.drawBitmap(64, 48, dot, 16, 16, 1);//:
  59. display.setTextSize(2); //设置字体大小
  60. display.setCursor(80, 48);//设置显示位置
  61. display.print(humidtyReal);//显示土壤湿度值
  62. display.setCursor(112, 48);//设置显示位置
  63. display.println("%");//显示百分比
  64. display.display();//开显示
  65. delay(500);
  66. }

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行的代码,打印“环境温度”、“环境湿度”两组汉字,并显示相应的传感器值。
image.png