#include <DHT.h>
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
// 传感器引脚定义
#define DHTPIN 2 // DHT22数据引脚
#define DHTTYPE DHT22 // DHT22类型
const int ldrPin = A0; // LDR光照传感器
const int soilPotPin = A1; // 土壤湿度模拟电位器
// 执行器引脚定义
const int lightBulbPin = 9; // 补光灯
const int buzzerPin = 10; // 蜂鸣器
const int servoPin = 11; // 舵机
const int alarmLedPin = 12; // 报警LED
// TFT显示屏引脚(ST7735,1.8英寸)
#define TFT_CS 7 // 片选引脚
#define TFT_DC 6 // 数据/命令引脚
#define TFT_RST 5 // 复位引脚
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// 控制按钮
const int armButtonPin = 3; // 机械臂按钮
// 阈值设置
const int lightThreshold = 500;
const int temperatureThreshold = 30;
const int humidityThreshold = 80;
const int soilMoistureThreshold = 300;
// 全局变量
float temperatureValue = 0;
float humidityValue = 0;
int lightValue = 0;
int soilMoistureValue = 0;
bool lastButtonState = HIGH;
bool systemStatus = true;
bool tempAlarm = false;
bool humiAlarm = false;
bool soilAlarm = false;
// 初始化对象
DHT dht(DHTPIN, DHTTYPE);
Servo myServo;
void setup() {
Serial.begin(9600);
// 初始化传感器和执行器
dht.begin();
myServo.attach(servoPin);
pinMode(lightBulbPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(alarmLedPin, OUTPUT);
pinMode(armButtonPin, INPUT_PULLUP);
// 初始化显示屏(针对Arduino Uno优化)
tft.initR(INITR_MINI160x80); // 1.8寸屏幕初始化
tft.setRotation(3); // 设置正确的显示方向
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(1);
tft.setCursor(0, 0);
tft.print("农业监测系统启动中...");
delay(1000);
}
void loop() {
if (systemStatus) {
collectEnvironmentData();
analyzeAndControl();
handleAlarmLight();
checkArmButton();
printSensorData();
updateLCDDisplay();
}
delay(2000); // 主循环延时
}
// 数据采集
void collectEnvironmentData() {
temperatureValue = dht.readTemperature();
humidityValue = dht.readHumidity();
lightValue = analogRead(ldrPin);
soilMoistureValue = analogRead(soilPotPin);
if (isnan(temperatureValue) || isnan(humidityValue)) {
temperatureValue = 0;
humidityValue = 0;
}
}
// 数据分析与控制
void analyzeAndControl() {
// 更新报警状态
tempAlarm = (temperatureValue > temperatureThreshold);
humiAlarm = (humidityValue > humidityThreshold);
soilAlarm = (soilMoistureValue < soilMoistureThreshold);
// 报警处理
if (tempAlarm || humiAlarm || soilAlarm) {
digitalWrite(buzzerPin, HIGH);
delay(100); // 短暂报警
digitalWrite(buzzerPin, LOW);
}
// 控制补光灯
digitalWrite(lightBulbPin, lightValue > lightThreshold ? HIGH : LOW);
}
// 报警灯控制
void handleAlarmLight() {
static unsigned long lastBlink = 0;
static bool ledState = LOW;
if (tempAlarm || humiAlarm || soilAlarm) {
if (millis() - lastBlink > 500) {
ledState = !ledState;
digitalWrite(alarmLedPin, ledState);
lastBlink = millis();
}
} else {
digitalWrite(alarmLedPin, LOW);
}
}
// 按钮检测
void checkArmButton() {
int buttonState = digitalRead(armButtonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
delay(50); // 消抖
if (digitalRead(armButtonPin) == LOW) {
performProductDetection();
}
}
lastButtonState = buttonState;
}
// 产品检测功能
void performProductDetection() {
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("开始检测...");
// 控制机械臂
myServo.write(0);
delay(500);
for (int angle = 0; angle <= 180; angle += 60) {
myServo.write(angle);
delay(300);
}
tft.setCursor(0, 20);
tft.print("分析中...");
delay(1500);
// 生成随机检测结果
int quality = random(70, 101);
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.print("质量评分: ");
tft.print(quality);
tft.print("/100");
// 根据评分显示不同颜色和文字
if (quality >= 90) {
tft.setTextColor(ST7735_GREEN);
tft.setCursor(0, 20);
tft.print("状态: 优秀");
} else if (quality >= 75) {
tft.setTextColor(ST7735_YELLOW);
tft.setCursor(0, 20);
tft.print("状态: 良好");
} else if (quality >= 60) {
tft.setTextColor(ST7735_ORANGE);
tft.setCursor(0, 20);
tft.print("状态: 一般");
} else {
tft.setTextColor(ST7735_RED);
tft.setCursor(0, 20);
tft.print("状态: 较差");
// 质量差时报警
for (int i = 0; i < 3; i++) {
digitalWrite(buzzerPin, HIGH);
digitalWrite(alarmLedPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
digitalWrite(alarmLedPin, LOW);
delay(200);
}
}
myServo.write(0); // 机械臂归位
delay(2000);
}
// 串口打印数据
void printSensorData() {
Serial.println("----- environmental data -----");
Serial.print("temperature: ");
Serial.print(temperatureValue);
Serial.println("°C");
Serial.print("humidity: ");
Serial.print(humidityValue);
Serial.println("%");
Serial.print("soil moisture: ");
Serial.println(soilMoistureValue);
Serial.print("intensity of illumination: ");
Serial.println(lightValue);
Serial.println("------------------");
}
// 更新LCD显示
void updateLCDDisplay() {
tft.fillScreen(ST7735_BLACK);
// 显示温度
tft.setTextColor(tempAlarm ? ST7735_RED : ST7735_WHITE);
tft.setCursor(0, 0);
tft.print("温度: ");
tft.print(temperatureValue);
tft.print("°C");
// 显示湿度
tft.setTextColor(humiAlarm ? ST7735_RED : ST7735_WHITE);
tft.setCursor(0, 15);
tft.print("湿度: ");
tft.print(humidityValue);
tft.print("%");
// 显示土壤湿度
tft.setTextColor(soilAlarm ? ST7735_RED : ST7735_WHITE);
tft.setCursor(0, 30);
tft.print("土壤湿度: ");
tft.print(soilMoistureValue);
// 显示光照强度
tft.setTextColor(lightValue > lightThreshold ? ST7735_YELLOW : ST7735_WHITE);
tft.setCursor(0, 45);
tft.print("光照: ");
tft.print(lightValue > lightThreshold ? "弱" : "正常");
// 显示系统状态
tft.setTextColor(ST7735_WHITE);
tft.setCursor(0, 60);
tft.print("状态: ");
tft.print(systemStatus ? "运行中" : "已暂停");
// 显示操作提示
tft.setCursor(0, 75);
tft.print("按按钮检测产品");
}