I am designing a PID temperature controler; as far as I managed to write it so far, you find the code below.
Can you please help me check if Integrator wind up and other PID elements are configured please?
When i run the code at some point close to the set point , it starts behaving like an ON and OFF code
I don't know if this is a valid question but im asking it anyways .
and if you can make any edits . i will be glad to check them out
// programing the pid
const int pin = 3; // PWM analog output
double cumerror = 0; // Initialising the cumulator of the integrator to 0
double input, setpoint, integral, proportional, piderror, pidvalue;
// Input values
double kp = 25; // seting proportional at 25
double ki = 0; // seting the integral at 0
void setup() {
setpoint = 37; // seting the setpoint at 37
Serial.begin(9600);
}
void loop() {
double inputvalue = map(analogRead(A0), 0, 1023, 0,
100); // reading the values from the analog signals
// and mapping it to a 100 deg scale
piderror = setpoint - inputvalue; // calculating the error
// calculating the integral ((Naturally ki= cumerror *dt), we do not have to
// multiply by dt, since dt = 0 because i am using the sample time delay(1000)
// every time )
cumerror += piderror;
integral = ki * cumerror;
// calculating the proportional error
proportional = kp * piderror; // calculating the proportional error
// changing the P and I parameters as the error approaches the set point
if (3 < piderror < 10) {
kp = 10;
ki = 0.5;
cumerror += (1 * piderror);
integral = ki * cumerror;
}
// Making the parameters =0 as the error approaches more 0
if (piderror < 3) {
proportional = 0;
pidvalue = 0;
integral = 0;
}
pidvalue = proportional + integral;
if (pidvalue <= 0) {
pidvalue = 0;
}
if (pidvalue > 255) {
pidvalue = 255;
}
analogWrite(pin, pidvalue); // sending the pid value to the heater
delay(1000);
cumerror=cumerror;
}