{//--- read ADC input and calculate next PWM value.
str = "ousb -r adc 0" ;
Vavg = cpp_do_ousb_command(&str) ;
Vgen = Vext - (Vavg - Vsat*pwm/100) / ( 1 - pwm/100) ;
pwm += gain*(Vgen_goal - Vgen) ;
if (!isnormal(pwm)) pwm = 0 ; // if maths error set motor to stop.
if (pwm>100) pwm = 100 ;
if (pwm<0) pwm = 0 ;
//--- form PWM command and send to board.
stst.str("") ;
stst << pwm ;
str = "ousb pwm 1 " + stst.str() ;
cpp_do_ousb_command(&str) ;
}
do a loop 60 times of the code above then
cout << " pwm%= " << (int)pwm << ", ADC0= " << Vavg <<endl;