Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. float T_PID(){
  2. if(flag==1){
  3. //Next we calculate the error between the setpoint and the real value
  4. PID_error = setTemp - Tread;
  5. //Calculate the P value
  6. PID_p = kp * PID_error;
  7. //Calculate the I value in a range on +-3
  8. if(-20 < PID_error <20)
  9. {
  10. PID_i = PID_i + (ki * PID_error);
  11. }
  12. //For derivative we need real time to calculate speed change rate
  13. timePrev = Time; // the previous time is stored before the actual time read
  14. Time = millis(); // actual time read
  15. elapsedTime = (Time - timePrev) / 1000;
  16. //Now we can calculate the D calue
  17. PID_d = kd*((PID_error - previous_error)/elapsedTime);
  18. //Final total PID value is the sum of P + I + D
  19. PID_value = PID_p + PID_i + PID_d;
  20. //We define PWM range between 0 and 255
  21. if(PID_value < 0)
  22. { PID_value = 0; }
  23. if(PID_value > 255)
  24. { PID_value = 255; }
  25. //Now we can write the PWM signal to the mosfet on digital pin D3
  26. analogWrite(PWM_pin,255-PID_value);
  27. previous_error = PID_error; //Remember to store the previous error for next loop.
  28.  
  29. delay(300);
  30. v = 255-PID_value;
  31. return v;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement