Arduino PWM and ramping

I have a problem with PWM hunting/oscillating.

Example : testing Juiian Ilett’s excellent PWM5 pv-solar controller, copied below.

Please note : this has nothing to with PWM frequency, and changing it has no effect.

I find that there is some hunting/oscillation; i.e. battery volts oscillates about 1Hz despite adding caps on input, output and feedback points.

I suspect that the problem can be resolved with slower increment in the steps in “pulseWidth += sizeSize” and similarly “pulseWidth -= stepSize”.

What declares how fast this happens?

And how could I slow that down?

Any ideas how I can resolve this?

Thanks.

void setup() {
  TCCR2A = TCCR2A | 0x30;
  TCCR2B = TCCR2B & 0xF8 | 0x01;
  analogWrite(11, 117);
  analogWrite(3, 137);
//  Serial.begin(9600);
}

const int setPoint = 13.5 * 20 / (20+82) * 1024 / 5;
int measurement = 0;
int pulseWidth = 0;
int difference = 0;
int stepSize = 0;

void loop() {
  measurement = analogRead(A0);
  difference = abs(setPoint - measurement);
  stepSize = difference;
  
  if (measurement < setPoint)
  {
    pulseWidth += stepSize;
    if (pulseWidth > 255) pulseWidth = 255;
  }
  if (measurement > setPoint)
  {
    pulseWidth -= stepSize;
    if (pulseWidth < 0) pulseWidth = 0;
  }
//  Serial.println(pulseWidth);
  analogWrite(9, pulseWidth);
  analogWrite(13, 255 - pulseWidth); // pwm to LED
  delay(10);
}

stepSize is always set equal to difference. If you want to slow it down you could set it to a fraction of it with a scaling factor. But keep in mind that pulsewidth is an integer, and not a float. Essentially this is a proportional controlling scheme (read up on PID controllers)

Asside from that there is the delay function which governs the loop-rate.