Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float T_PID(){
- if(flag==1){
- //Next we calculate the error between the setpoint and the real value
- PID_error = setTemp - Tread;
- //Calculate the P value
- PID_p = kp * PID_error;
- //Calculate the I value in a range on +-3
- if(-20 < PID_error <20)
- {
- PID_i = PID_i + (ki * PID_error);
- }
- //For derivative we need real time to calculate speed change rate
- timePrev = Time; // the previous time is stored before the actual time read
- Time = millis(); // actual time read
- elapsedTime = (Time - timePrev) / 1000;
- //Now we can calculate the D calue
- PID_d = kd*((PID_error - previous_error)/elapsedTime);
- //Final total PID value is the sum of P + I + D
- PID_value = PID_p + PID_i + PID_d;
- //We define PWM range between 0 and 255
- if(PID_value < 0)
- { PID_value = 0; }
- if(PID_value > 255)
- { PID_value = 255; }
- //Now we can write the PWM signal to the mosfet on digital pin D3
- analogWrite(PWM_pin,255-PID_value);
- previous_error = PID_error; //Remember to store the previous error for next loop.
- delay(300);
- v = 255-PID_value;
- return v;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement