PI control Implementation for Buck Converter using Arduino
Try this:

// parameters are T0..sample time[s], Kp..gain, Ti..integrating time[s]
//At init ek1, uk1 = 0

// compute coeficients at init and at each parameter change 
q0 = Kp*(1 + T0/(2*Ti));
q1 = -Kp*(1 - T0/(2*Ti));

//execute this in ISR at every sample time - timer interrupt 
// u..output
ek1 = ek;
ek = setpoint - actual;

u = q0*ek + q1*ek1 + uk1;

if u &gt; u_max then
   u = u_max;
elsif u &lt; u_min then
   u = u_min;
end_if;

uk1 = u;