#include <LiquidCrystal.h>
#include <DHT.h>
// 初始化 LCD 引脚
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
// 定义传感器引脚
#define DHTPIN 8 // DHT22 数据引脚
#define DHTTYPE DHT22 // 传感器类型
DHT dht(DHTPIN, DHTTYPE); // 初始化 DHT22
const int potPin = A0; // 滑动变阻器引脚
void setup() {
lcd.begin(16, 2); // 初始化 LCD
dht.begin(); // 启动 DHT22
lcd.print("Loading...");// 初始显示
}
void loop() {
delay(2000); // DHT22 需要至少 2 秒的读取间隔
// 读取温湿度
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
// 读取滑动变阻器值(范围0-100,用于示例控制)
int potValue = analogRead(potPin);
int threshold = map(potValue, 0, 1023, 0, 100);
// 显示温度和湿度
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempC);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(humidity);
lcd.print("%");
// 可选:显示滑动变阻器阈值(例如第二行右侧)
// lcd.setCursor(10, 1);
//lcd.print("T:");
// lcd.print(threshold);
}