Advertisement
UnaClocker

Hotplate controller

Dec 1st, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. // Hotplate Controller
  2. // Designed, assembled and programed by
  3. // Brian Schulteis for work
  4. // (c) 2013
  5. // Ver 1.2
  6.  
  7. // Below are the Libraries used by this firmware
  8. #include <PID_v1.h> // Library to Control Temperature
  9. #include <Encoder.h> // Library to read rotary encoder
  10. #include <Adafruit_GFX.h>  // LCD Graphics/Font Library
  11. #include <Adafruit_PCD8544.h>  // LCD SPI protocol library
  12. #include <math.h>
  13. #include <EEPROM.h>
  14.  
  15. #define relayPin 4  // Digital Pin 4 controls solid state relay
  16. #define timerCTL 8
  17. double        Setpoint, degreesF, Output, target; // Used by PID
  18. int           WindowSize = 500; // Half second minimum between turning SSR on/off.
  19. unsigned long windowStartTime, lastDisplayUpdate,timeMath, timeCounter,lastTempChange; // Time tracking variables
  20. boolean timerRunning=false; //
  21. byte eepromTemp, tempTemp, originalTemp; // Probably got more than I need, but they're just bytes.
  22.  
  23.  
  24. Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 9, 10, 7);  // "display" will be the instance of LCD library
  25. Encoder controlKnob(2,3);  // "controlKnob" will be the instance of this library
  26. PID hotplatePID(&degreesF, &Output, &Setpoint,2,5,1, DIRECT); // "hotplatePID" will be the instance of this library
  27.  
  28.  
  29.  
  30. void setup()
  31. {
  32.   pinMode(relayPin, OUTPUT);
  33.   pinMode(timerCTL, INPUT);
  34.   digitalWrite(timerCTL, HIGH);
  35.   display.begin();
  36.   delay(5);
  37.   display.setContrast(50);
  38.   delay(5);
  39.   eepromTemp=EEPROM.read(10);
  40.   if ((eepromTemp<60) or (eepromTemp>150)) eepromTemp=120;
  41.   target= (double) eepromTemp;
  42.   originalTemp=target;
  43.   lastTempChange=millis();
  44.   //  delay(2000);
  45.   windowStartTime = millis();
  46.  
  47.   //initialize the variables we're linked to
  48.   Setpoint = target;
  49.  
  50.   //tell the PID to range between 0 and the full window size
  51.   hotplatePID.SetOutputLimits(0, WindowSize);
  52.   display.setTextSize(1);
  53.   display.setTextColor(BLACK);
  54.   display.setCursor(13,0);
  55.   display.println("--------");
  56.   display.display();
  57.  
  58.   //turn the PID on
  59.   hotplatePID.SetSampleTime(200);
  60.   hotplatePID.SetMode(AUTOMATIC);
  61. }
  62.  
  63. void updateScreen() {
  64.   display.clearDisplay();
  65.   display.setTextSize(1);
  66.   display.setCursor(13,0);
  67.   display.println("--------");
  68.   display.print("Target: ");
  69.   display.println(target,1);
  70.   display.print("Actual: ");
  71.   display.println(degreesF,1);
  72.   if (timerRunning) {
  73.     display.println("Timer:");
  74.     display.setTextSize(2);
  75.     int totalTime = (millis() - timeCounter)/1000;
  76.     if (totalTime>1800) timerRunning=false; // Don't need to run for more than 30 minutes.
  77.     if (totalTime>59) {
  78.       display.print(totalTime/60);
  79.       display.print(':');
  80.       totalTime=totalTime-(60* (totalTime/60));
  81.     }
  82.     if (totalTime<10) display.print('0');
  83.     display.print(totalTime);
  84.   }
  85.   display.display();
  86.   lastDisplayUpdate=millis();
  87. }
  88.  
  89. void readThermister() {
  90.   double Temp; // Variable that returns as a temperature value
  91.   Temp = log(((12240000/analogRead(A0)) - 10000)); // Fancy math stuff
  92.   Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Even fancier
  93.   Temp = Temp - 273.15;           // Convert Kelvin to Celcius
  94.   degreesF = (Temp * 1.8) + 32.0;    // Convert to degrees F
  95. }
  96.  
  97.  
  98. void loop()
  99. {
  100.   if (target>160) target=160;
  101.   Setpoint=target;
  102.   readThermister();    
  103.   hotplatePID.Compute();
  104.   if (millis()-lastDisplayUpdate>250) updateScreen();
  105.   target=originalTemp+(controlKnob.read()/4);
  106.   if ((millis()-timeMath)>250) {
  107.     if (!digitalRead(timerCTL)) {
  108.       timerRunning=!timerRunning;
  109.       timeCounter=millis();
  110.       timeMath=millis();
  111.     }
  112.   }
  113.   if (millis()-lastTempChange>30000) {
  114.     tempTemp= (byte) target;
  115.     if (tempTemp != ((byte) EEPROM.read(10))) {
  116.       EEPROM.write(10, tempTemp);
  117.       lastTempChange=millis();
  118.     } else lastTempChange=millis();
  119.   }
  120. // PID Relay Control below
  121.   if(millis() - windowStartTime>WindowSize)
  122.   { //time to shift the Relay Window
  123.     windowStartTime += WindowSize;
  124.   }
  125.   if (Output > millis() - windowStartTime) {
  126.     if (degreesF<target) digitalWrite(relayPin, HIGH);
  127.   }
  128.   else digitalWrite(relayPin, LOW);
  129.   if (degreesF>target) digitalWrite(relayPin, LOW);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement