function write_volt(v) global ser; p = volts_to_pwm(v); fprintf(ser,'%04X\n', p); % send a pwm value to controller drawnow; end function p = volts_to_pwm(v) global V_max % volts_to_pwm - convert volts to PWM value p = v * (32767/V_max) + 32768; p = round(p); end