I am working on a firmware code, I set interrupt time to 10us but now the loop is running every 5ms, which is much much slower. Any idea on how to speed up the loop is highly appreciate!
unsigned long lastTime;
double BAT_I_FB, Output, Setpoint= 8 ;
double errSum=0, lastErr,error=99;
double kp=0.1, ki=15, kd=0;
double KdVal,KpVal,KiVal,PID,BUS_V;
double SampleTime = 10;
void loop() {
unsigned long now = micros();
int timeChange = (now-lastTime);
if(timeChange >= SampleTime)
{
/*Compute all the working error variables*/
BAT_I_FB = analogRead(pins[15].arduinoPinNumber);
BAT_I_FB = float(BAT_I_FB * (5.0/1024)) * pins[15].multiplier;
error = Setpoint - BAT_I_FB;
errSum += error;
double dErr = (error - lastErr);
BUS_V = analogRead(pins[18].arduinoPinNumber);
BUS_V = float(BUS_V * (5.0/1024)) * pins[18].multiplier;
/*compute PID Output*/
PID = kp * error +ki/10000 * errSum + kd * 1000 * dErr;
Output = (PID-100) * (-2.5);
analogWrite(2, Output);
/*Remember some variable for next time*/
lastErr = error;
lastTime = now;
}