Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. //Soldering station Software using PID
  2. //Thank you Alex from https://geektimes.ru/ for help with led array function
  3. //AllAboutCircuits.com
  4. //epilepsynerd.wordpress.com
  5.  
  6. #include <PID_v1.h>
  7.  
  8. //This array contains what segments need to be turned on to display numbers 0-9
  9. byte const digits[] = {
  10. B00111111, B00000110, B01011011, B01001111, B01100110, B01101101, B01111101, B00000111, B01111111, B01101111
  11. };
  12.  
  13. int digit_common_pins[] = {8, 9, 10}; //Common pins for the triple 7-Segment LED display
  14. int max_digits = 3;
  15. int current_digit = max_digits - 1;
  16.  
  17. unsigned long updaterate = 1200; //Change how fast the display updates. No lower than 500
  18. unsigned long lastupdate;
  19.  
  20. int temperature = 0;
  21.  
  22. //Define Variables we'll be connecting to
  23. double Setpoint, Input, Output;
  24.  
  25. //Define the aggressive and conservative Tuning Parameters
  26. double aggKp = 4, aggKi = 0.2, aggKd = 1;
  27. double consKp = 1, consKi = 0.05, consKd = 0.25;
  28.  
  29. //Specify the links and initial tuning parameters
  30. PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
  31.  
  32. void setup()
  33. {
  34. DDRD = B11111111; // sets Arduino pins 0 to 7 as outputs
  35. for (int y = 0; y < max_digits; y++)
  36. {
  37. pinMode(digit_common_pins[y], OUTPUT);
  38. }
  39. //We do not want to drive the soldering iron at 100% because it may burn, so we set it to about 85% (220/255)
  40. myPID.SetOutputLimits(0, 220);
  41. myPID.SetMode(AUTOMATIC);
  42. lastupdate = millis();
  43. Setpoint = 0;
  44. }
  45.  
  46.  
  47. void loop() {
  48. //Read temperature
  49. Input = analogRead(0);
  50. //Transform the 10bit reading into degrees celsius
  51. Input = map(Input, 0, 590, 25, 350);
  52. //Display temperature
  53. if (millis() - lastupdate > updaterate) {
  54. lastupdate = millis();
  55. temperature = Input;
  56. }
  57. //Read setpoint and transform it into degrees celsius(minimum 150, maximum 350)
  58. double newSetpoint = analogRead(1);
  59. newSetpoint = map(newSetpoint, 0, 1023, 150, 350);
  60. //Display setpoint
  61. if (abs(newSetpoint - Setpoint) > 3) {
  62. Setpoint = newSetpoint;
  63. temperature = newSetpoint;
  64. lastupdate = millis();
  65. }
  66.  
  67. double gap = abs(Setpoint - Input); //distance away from setpoint
  68.  
  69. if (gap < 10)
  70. { //we're close to setpoint, use conservative tuning parameters
  71. myPID.SetTunings(consKp, consKi, consKd);
  72. }
  73. else
  74. {
  75. //we're far from setpoint, use aggressive tuning parameters
  76. myPID.SetTunings(aggKp, aggKi, aggKd);
  77. }
  78.  
  79. myPID.Compute();
  80. //Drive the output
  81. analogWrite(11, Output);
  82. //Display the temperature
  83. show(temperature);
  84. }
  85.  
  86. void show(int value) {
  87. int digits_array[] = {};
  88. boolean empty_most_significant = true;
  89. for (int z = max_digits - 1; z >= 0; z--) //Cycle through each digit
  90. {
  91. digits_array[z] = value / pow(10, z); //We now take each digit from the number
  92. if (digits_array[z] != 0 ) empty_most_significant = false; //Do not display leading zeros
  93. value = value - digits_array[z] * pow(10, z);
  94. if (z == current_digit)
  95. {
  96. if (!empty_most_significant || z == 0) { //Check to see that we do not have leading zeros and display the current digit
  97. PORTD = ~digits[digits_array[z]]; //Remove ~ for common cathode
  98. }
  99. else
  100. {
  101. PORTD = B11111111;
  102. }
  103. digitalWrite(digit_common_pins[z], LOW);//Change to LOW for common cathode
  104. } else {
  105. digitalWrite(digit_common_pins[z], HIGH);//Change to HIGH for common cathode
  106. }
  107.  
  108. }
  109. current_digit--;
  110. if (current_digit < 0)
  111. {
  112. current_digit = max_digits; //Start over
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement