сделал так.
Код:
struct LIGHT_STRUCT
{
uint16_t in_med;
uint16_t current;
uint16_t target;
const uint8_t P = 5;
uint16_t in = 0;
};
LIGHT_STRUCT TFT_BL;
void backlightadjust() {
if (millis() % 2 == 0)
{
if (readIndex < 10)
{
TFT_BL.in += 4095 - analogRead(LIGHT_PIN); //набор данных с сенсора света для усреднения
fuel.in += analogRead(FUEL_PIN);
readIndex++;
}
else
{
readIndex = 0;
TFT_BL.in_med = TFT_BL.in / 10; //усредняем
fuel.in_med = fuel.in / 10;
TFT_BL.target = map(TFT_BL.in_med, 0, 4095, 0, 65535); //масштабируем
fuel.in = 0;
TFT_BL.in = 0;
}
if (TFT_BL.current - TFT_BL.P > TFT_BL.target) //+- 20 чтобы яркость не прыгала
TFT_BL.current -= TFT_BL.P; //увеличиваем яркость с шагом 20
if (TFT_BL.current + TFT_BL.P < TFT_BL.target) // +- 20 чтобы яркость не прыгала
TFT_BL.current += TFT_BL.P; //уменьшаем яркость с шагом 20
if (TFT_BL.current > 62000) TFT_BL.current = 62000; //минимальная яркость 62000
if (TFT_BL.in_med < 400) TFT_BL.current = 200; //минимальная яркость 62000
pwmWrite(TFT_LED, TFT_BL.current);
}
}