ok
This commit is contained in:
35
src/LightControl.cpp
Normal file
35
src/LightControl.cpp
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user