function [test_results,legend_string_cl,ls_index_cl,l_p_cl,plt_index_cl]=closed_loop(value,kp,ki,target,max_time,legend_string_cl,ls_index_cl,plt_index_cl,test_results,volt_rpm)
    format longG
    result_volt_ol=[];
    result_rpm_ol=[];
    time_const_ol=[];
    ss_error_ol=[];
    for i=1:length(kp)
        write_volt(value,0);
        arr_volt=[];
        arr_rpm=[];
        time=[];
        index=1;
        target_volt=target;
        pause(10);
        t=tic;
        integral1=0;
        for o=1:5
            init_volt=read_sig(value);
            write_volt(value,0);
        end
        
        disp('Please Wait....')
        
        while (toc(t)<=max_time)
            t1=tic;
            arr_volt(index)=read_sig(value); %Read voltage
            arr_rpm(index)=volt_to_rpm(volt_rpm,arr_volt(index)); %Convert the voltage read into rpm 
            err=(target_volt-arr_volt(index)); %Calculate the differenc between the feedback and the target
            dt=toc(t1); %Compute  delta t
            integral1=(integral1+err*dt); %Compute the integral
            fun_ki=ki*integral1; %Compute the contribution of the integral to the final drive voltage
            drive_volt=kp(i)*err+fun_ki; %Compute the final drive voltage
            time(index)=toc(t); %Note the time into an array for plotting
            if(drive_volt>7)
                drive_volt=7;
            else if(drive_volt<-6)
                drive_volt=-6;
                end
            end

            index=index+1;
            write_volt(value,drive_volt);
            
        end
        result_volt_ol(i)=arr_volt(length(arr_volt));
        result_rpm_ol(i)=arr_rpm(length(arr_rpm));
        out1=arr_volt;
        out2=time;
        out3=arr_rpm;
        test_data=out_data(out2,out1);
        [time_const_ol(i),voltage]=calc_rise_time_cl(out1,test_data);
        ss_error_ol(i)= target-arr_volt(length(arr_volt));
        str0=[strcat('Ref.Volt: ',num2str(target), ' volt')];
        str1=[(strcat('Kp:',num2str(kp(i)),' | Ki:',num2str(ki),' | Tacho Output:',num2str(result_volt_ol(i)),' | RPM:',num2str(result_rpm_ol(i)),' | Rise Time:',num2str(time_const_ol(i))))];
        legend_string_cl{ls_index_cl}=str1;
        ls_index_cl=ls_index_cl+1;
        [legend_string_cl,ls_index_cl,l_p_cl,plt_index_cl]=plot_fig_cl(out1,out2,kp,ki,target,legend_string_cl,max_time,(ls_index_cl-1),str0,plt_index_cl,volt_rpm); %Plot the data stored in tim array and degree array through this function
        txt0=sprintf('Rise Time: %0.3f sec',time_const_ol(i));
        txt1=sprintf('Steady State Error:%.3f volt',ss_error_ol(i));
        txt2=sprintf('Stady State Speed N, rpm: %.0f rpm ',result_rpm_ol(i));
        txt3=sprintf('Steady State Tacho Output, Vt: %.3f Volts ',result_volt_ol(i)); 
        str=sprintf('DC Speed Control Closed Loop Response with Kp:%.2f Ki:%0.2f',kp(i),ki);
        plot_result_cl(out1,out2,time_const_ol(i),voltage,str,txt0,txt1,txt2,txt3,max_time,target,volt_rpm);
        write_volt(value,0);
        input('Press Enter');
    end
    for i=1:length(kp)
    test_results=final_result_cl(kp(i),ki,result_volt_ol(i),result_rpm_ol(i),time_const_ol(i),ss_error_ol(i),test_results);
    end
    
    
end