#include <Wire.h> // 如果使用I2C OLED屏幕,可能需要这个库
#include <Adafruit_SSD1306.h> // OLED屏幕库
#include <TimeLib.h> // 时间库
#include <Adafruit_GFX.h>
#include <RTClib.h>
// 假设的引脚定义,你需要根据你的硬件进行调整
#define LIGHT_SENSOR_PIN A0 // 光敏电阻连接的引脚
#define PIR_SENSOR_PIN 2 // 人体红外感应传感器连接的引脚
#define LED_PIN 12 // LED灯连接的引脚
#define BUTTON_PIN1 3 // 按钮连接的引脚(假设为数字引脚3)
#define a A3
RTC_DS1307 RTC;
// OLED屏幕设置
#define SCREEN_WIDTH 128 // OLED屏幕宽度
#define SCREEN_HEIGHT 64 // OLED屏幕高度
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // 初始化OLED屏幕
// 光照强度的阈值,可以手动设置
int lightThreshold = 500; // 假设的初始值,你可能需要调整它
const int thresholdStep = 100; // 每次按钮按下时阈值变化的步长
const int thresholdMin = 0; // 阈值最小值
const int thresholdMax = 900; // 阈值最大值
// 定时器变量
unsigned long lastPersonDetected = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(BUTTON_PIN1,INPUT );
// 设置OLED屏幕
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 初始化OLED屏幕
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
Serial.begin(9600);
if (!RTC.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!RTC.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// 设置时间(这里只是一个示例,你需要根据你的时间源进行设置)
setTime(0, 0, 0, 1, 1, 2023); // 设置日期和时间(小时,分钟,秒,日,月,年)
}
void loop() {
// 读取光照强度(这里只是一个示例,你需要根据你的传感器进行调整)
int lightLevel = analogRead(LIGHT_SENSOR_PIN);
// 检查是否有人员经过
boolean personDetected = digitalRead(PIR_SENSOR_PIN) == HIGH;
//读取温度
int sensorValue = analogRead(a);
//将传感器值转换为电压
float voltage = sensorValue * (5.0 / 1023.0);
// 使用欧姆定律(V=IR)计算电阻
float resistance = (5.0 - voltage) * 1000 / voltage;
// 使用Steinhart-Hart方程计算温度
float b = 3950; // NTC热敏电阻的贝塔值
float T0 = 25; // NTC的校准温度
float T = log((resistance / 10000) + 1) / b + 1 / (25 + 273.15);
T = 1 / T - 273.15; // 将开尔文转换为摄氏度
// 显示信息
display.clearDisplay();
display.setCursor(0,0);
DateTime now = RTC.now();
display.setCursor(0,0);
display.print("Time: ");
display.print(now.hour(), DEC);
display.print(":");
display.print(now.minute(), DEC);
display.print(":");
display.print(now.second(), DEC);
display.setCursor(0,15);
display.print("Light: ");
display.print(lightLevel);
Serial.print("光照强度");
Serial.println(lightLevel);
display.setCursor(0,30);
display.print("Temperature: ");
display.print(T);
display.println(" C");
display.setCursor(0,45);
if (personDetected) {
display.print("Person Detected");
} else {
display.print("No Person");
}
display.setCursor(100, 55);
display.print(lightThreshold);
display.display();
// 根据开关状态调整光敏电阻的值
if (digitalRead(BUTTON_PIN1) == HIGH) {
lightThreshold=lightThreshold+thresholdStep; }// 增加光敏电阻的值
if (lightThreshold > thresholdMax) {
lightThreshold = 500;
}
// 逻辑处理
if (lightLevel < lightThreshold) {
if (personDetected) {
digitalWrite(LED_PIN, HIGH); // 打开灯光
lastPersonDetected = millis();
}
else if (millis() - lastPersonDetected > 10000) {
digitalWrite(LED_PIN, LOW); // 10秒后无人经过,关闭灯光
}
}
else {
digitalWrite(LED_PIN, LOW); // 光照条件好时,关闭灯光
}
delay(100); // 稍微延迟以减少CPU负载
}