Advertisement
Guest User

Untitled

a guest
Jun 29th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <max6675.h>
  2.  
  3. #define DO 4
  4. #define CS 5
  5. #define CLK 6
  6. #define PWM 11
  7.  
  8. MAX6675 thermo(CLK,CS,DO);
  9.  
  10. float kp=0.5;
  11. float kd=70;
  12. float ki=0.001;
  13.  
  14. int milliOld;
  15. int milliNew;
  16. int dt;
  17.  
  18. float desTemp = 0; //setpoint temperature
  19. float mesTemp = 0; //actual temperature
  20. float tempError = 0;
  21. float tempErrorOld;
  22. float tempErrorChange;
  23. float tempErrorSlope = 0;
  24. float tempErrorArea = 0;
  25.  
  26. float PID_val = 0;
  27.  
  28. void setup() {
  29. TCCR2B = TCCR2B & B11111000 | 0x03; //sets pin 3 and 11 to PWM freq of 928.5Hz
  30. pinMode(PWM,OUTPUT);
  31. milliNew = millis();
  32. Serial.begin(9600);
  33. delay(100);
  34. Serial.println("LABEL, Time, Temperature"); //PLX-DAQ stuff
  35. mesTemp = thermo.readCelsius();
  36.  
  37. }
  38.  
  39. void loop() {
  40. desTemp = desired_temperature(milliNew / 3.6e6); //updating the setpoint based on time passed
  41. mesTemp = thermo.readCelsius(); //reading the temperature
  42.  
  43. milliOld = milliNew;
  44. milliNew = millis();
  45. dt = milliNew-milliOld;
  46.  
  47. tempErrorOld = tempError;
  48. tempError = desTemp-mesTemp;
  49. tempErrorChange = tempError-tempErrorOld;
  50. tempErrorSlope = tempErrorChange/dt;
  51. tempErrorArea = tempErrorArea + tempError*dt;
  52.  
  53. PID_val = kp*tempError + kd*tempErrorSlope + ki*tempErrorArea;
  54.  
  55. if(PID_val < 0){
  56. PID_val = 0;
  57. }
  58. if(PID_val > 255){
  59. PID_val = 255;
  60. }
  61. analogWrite(PWM,255-PID_val);
  62. Serial.print("DATA,TIME,"); //more PLX-DAQ stuff
  63. Serial.println(mesTemp);
  64. delay(1000);
  65.  
  66.  
  67. }
  68.  
  69. float desired_temperature (float time_hours)
  70. {
  71. if (time_hours < 1.0)
  72. return 20 + 80 * time_hours ; // ramp from 20 to 100 over 1 hour
  73. if (time_hours < 3.0)
  74. return 100; // hold at 100 between hours 1 and 3
  75. if (time_hours < 4.0)
  76. return 20 + 80 * (4.0 - time_hours) ; // then ramp down from 100 to 20 over 1 hour
  77. return 0; // otherwise inactive
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement