This commit is contained in:
2025-05-13 16:55:49 +08:00
parent 1c2201afc9
commit bf5270f337
16 changed files with 463 additions and 384 deletions

35
src/LightControl.cpp Normal file
View File

@ -0,0 +1,35 @@
// 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();
}
void LightControl::adjustWiper() {
float currentLight = getCurrentLight();
float error = targetLightLevel - currentLight;
// 计算输出变化量(比例控制)
int delta = (int)(abs(error) * Kp);
if (delta < 1) delta = 1; // 最小调节步长
if (error > 0) {
// 需要增加亮度 -> 减小电位器阻值
lastWiperValue = max(0, lastWiperValue - delta);
} else if (error < 0) {
// 需要减少亮度 -> 增大电位器阻值
lastWiperValue = min(127, lastWiperValue + delta);
}
digitalPot.setWiper(lastWiperValue);
Serial.printf("Current Light: %.0f lux | Wiper Value: %d\n", currentLight, lastWiperValue);
}