2025-05-13 16:55:49 +08:00
|
|
|
|
// LightControl.cpp
|
|
|
|
|
|
|
|
|
|
|
|
#include "LightControl.h"
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
|
|
|
LightControl::LightControl(BH1750& sensor, DS3502& pot)
|
|
|
|
|
|
: lightSensor(sensor), digitalPot(pot) {}
|
|
|
|
|
|
|
|
|
|
|
|
void LightControl::setTargetLight(float target) {
|
|
|
|
|
|
targetLightLevel = target;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float LightControl::getCurrentLight() {
|
|
|
|
|
|
return lightSensor.readLightLevel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 14:31:37 +08:00
|
|
|
|
// 新增:持续调节函数,每次只调节一步
|
|
|
|
|
|
void LightControl::adjustToTarget() {
|
|
|
|
|
|
static unsigned long lastAdjustTime = 0;
|
|
|
|
|
|
const unsigned long adjustInterval = 300; // 每300ms调节一次
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long currentTime = millis();
|
|
|
|
|
|
if (currentTime - lastAdjustTime < adjustInterval) {
|
|
|
|
|
|
return; // 还没到调节时间
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 17:24:26 +08:00
|
|
|
|
const float tolerance = 100.0f; // 容许误差范围(±100 lux)
|
2025-07-01 14:31:37 +08:00
|
|
|
|
float currentLight = getCurrentLight();
|
|
|
|
|
|
float res = targetLightLevel - currentLight;
|
|
|
|
|
|
|
|
|
|
|
|
if (abs(res) <= tolerance) {
|
|
|
|
|
|
// 已达到目标,但不退出,继续监控
|
|
|
|
|
|
lastAdjustTime = currentTime;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lastWiperValue = digitalPot.getWiper();
|
|
|
|
|
|
|
|
|
|
|
|
if (res > 0) {
|
|
|
|
|
|
// 需要增加亮度 -> 增加电位器阻值
|
|
|
|
|
|
if (lastWiperValue < 127) {
|
|
|
|
|
|
lastWiperValue++;
|
|
|
|
|
|
digitalPot.setWiper(lastWiperValue);
|
|
|
|
|
|
Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (res < 0) {
|
|
|
|
|
|
// 需要减少亮度 -> 减少电位器阻值
|
|
|
|
|
|
if (lastWiperValue > 0) {
|
|
|
|
|
|
lastWiperValue--;
|
|
|
|
|
|
digitalPot.setWiper(lastWiperValue);
|
|
|
|
|
|
Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lastAdjustTime = currentTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LightControl::runUntilTargetReached(float target, int maxAttempts) {
|
|
|
|
|
|
// 保留原有函数,但现在主要用于设置目标值
|
|
|
|
|
|
setTargetLight(target);
|
|
|
|
|
|
Serial.printf("Target light set to: %.0f\n", target);
|
|
|
|
|
|
|
|
|
|
|
|
// 可选:立即进行一次快速调节
|
|
|
|
|
|
const float tolerance = 100.0f;
|
2025-06-17 17:24:26 +08:00
|
|
|
|
float currentLight;
|
|
|
|
|
|
float res;
|
|
|
|
|
|
|
|
|
|
|
|
int attempt = 0;
|
2025-07-01 14:31:37 +08:00
|
|
|
|
const int max_retries = min(maxAttempts, 10); // 限制快速调节次数
|
2025-06-17 17:24:26 +08:00
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
currentLight = getCurrentLight();
|
|
|
|
|
|
res = target - currentLight;
|
|
|
|
|
|
|
|
|
|
|
|
if (abs(res) <= tolerance) {
|
2025-07-01 14:31:37 +08:00
|
|
|
|
Serial.println("Initial target reached.");
|
2025-06-17 17:24:26 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lastWiperValue = digitalPot.getWiper();
|
|
|
|
|
|
|
|
|
|
|
|
if (res > 0) {
|
|
|
|
|
|
if (lastWiperValue <= 127) {
|
|
|
|
|
|
lastWiperValue++;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (res < 0) {
|
|
|
|
|
|
if (lastWiperValue > 0) {
|
|
|
|
|
|
lastWiperValue--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
digitalPot.setWiper(lastWiperValue);
|
2025-07-01 14:31:37 +08:00
|
|
|
|
Serial.printf("Quick adjust: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, target, lastWiperValue);
|
|
|
|
|
|
delay(200);
|
2025-06-17 17:24:26 +08:00
|
|
|
|
|
|
|
|
|
|
attempt++;
|
|
|
|
|
|
if (attempt >= max_retries) {
|
2025-07-01 14:31:37 +08:00
|
|
|
|
Serial.println("Quick adjust completed, continuing with auto mode.");
|
2025-06-17 17:24:26 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} while (true);
|
2025-05-13 16:55:49 +08:00
|
|
|
|
}
|