if(VLED<5&&(Vm<blue_Vrev||Vm_diff_log[count]>0))//this line was modified so that Int is only calculated when the output has not reached its maximum (5V) and either hyperpolarization is needed (red ch) or depolarization is needed (blue ch) and Vm is more negative than blue_Vrev.
{Int=Int+Vm_diff_log[count];}
// This statement is entered whenever the instruction to correct the AP has
// been given.
// ************************************
// * Calculate the proportional error *
// ************************************
Vm_diff_log[count]=Vm-ideal_AP[count];// Log the errors
// ***************************************
// * Calculate the integral of the error *
// ***************************************
if(VLED<5&&(Vm<blue_Vrev||Vm_diff_log[count]>0))
{
// Int is only calculated when the voltage LED output has not reached
// its maximum (5V) and on eof two conditiosn is satisfied: depolarization
// is needed (blue ch) and Vm is more negative than blue_Vrev, or
// hyperpolarization is needed (red ch).
// This step was taken such that the Int term cannot amass further when
// the system can not react to it.
Int=Int+Vm_diff_log[count];
}
// *****************************************
// * Calculate the derivative of the error *
// *****************************************
// Calculate the numerator for a linear regression between the last "length" amount of points.
// This larger amount of points is chosen to cut out the noise that is intrinsically present