- // FIXME: running average?
- if (2*adcval > 5*adc_exp) { // >2.5x expected, lower significantly
- new_pwm = 2*old_pwm/3;
- } else if (3*adcval > 4*adc_exp) { // >1.33x expected, lower a bit
- new_pwm = old_pwm - 1;
- } else if (4*adcval < 3*adc_exp) { // 0.75x expected, raise a bit
- new_pwm = old_pwm + 1;
- }
+ differences[n] += adcval;
+ differences[n] -= adc_exp;
+
+ if (differences[n] > 16)
+ new_pwm -= 2;
+ else if (differences[n] > 4)
+ new_pwm--;
+ else if (differences[n] < -16)
+ new_pwm += 2;
+ else if (differences[n] < -4)
+ new_pwm++;
+ // new_pwm -= differences[n] >> 3;
+
+ if (new_pwm == old_pwm)
+ return;
+
+ differences[n] = 0;