Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hotplate Controller
- // Designed, assembled and programed by
- // Brian Schulteis for work
- // (c) 2013
- // Ver 1.2
- // Below are the Libraries used by this firmware
- #include <PID_v1.h> // Library to Control Temperature
- #include <Encoder.h> // Library to read rotary encoder
- #include <Adafruit_GFX.h> // LCD Graphics/Font Library
- #include <Adafruit_PCD8544.h> // LCD SPI protocol library
- #include <math.h>
- #include <EEPROM.h>
- #define relayPin 4 // Digital Pin 4 controls solid state relay
- #define timerCTL 8
- double Setpoint, degreesF, Output, target; // Used by PID
- int WindowSize = 500; // Half second minimum between turning SSR on/off.
- unsigned long windowStartTime, lastDisplayUpdate,timeMath, timeCounter,lastTempChange; // Time tracking variables
- boolean timerRunning=false; //
- byte eepromTemp, tempTemp, originalTemp; // Probably got more than I need, but they're just bytes.
- Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 9, 10, 7); // "display" will be the instance of LCD library
- Encoder controlKnob(2,3); // "controlKnob" will be the instance of this library
- PID hotplatePID(°reesF, &Output, &Setpoint,2,5,1, DIRECT); // "hotplatePID" will be the instance of this library
- void setup()
- {
- pinMode(relayPin, OUTPUT);
- pinMode(timerCTL, INPUT);
- digitalWrite(timerCTL, HIGH);
- display.begin();
- delay(5);
- display.setContrast(50);
- delay(5);
- eepromTemp=EEPROM.read(10);
- if ((eepromTemp<60) or (eepromTemp>150)) eepromTemp=120;
- target= (double) eepromTemp;
- originalTemp=target;
- lastTempChange=millis();
- // delay(2000);
- windowStartTime = millis();
- //initialize the variables we're linked to
- Setpoint = target;
- //tell the PID to range between 0 and the full window size
- hotplatePID.SetOutputLimits(0, WindowSize);
- display.setTextSize(1);
- display.setTextColor(BLACK);
- display.setCursor(13,0);
- display.println("--------");
- display.display();
- //turn the PID on
- hotplatePID.SetSampleTime(200);
- hotplatePID.SetMode(AUTOMATIC);
- }
- void updateScreen() {
- display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(13,0);
- display.println("--------");
- display.print("Target: ");
- display.println(target,1);
- display.print("Actual: ");
- display.println(degreesF,1);
- if (timerRunning) {
- display.println("Timer:");
- display.setTextSize(2);
- int totalTime = (millis() - timeCounter)/1000;
- if (totalTime>1800) timerRunning=false; // Don't need to run for more than 30 minutes.
- if (totalTime>59) {
- display.print(totalTime/60);
- display.print(':');
- totalTime=totalTime-(60* (totalTime/60));
- }
- if (totalTime<10) display.print('0');
- display.print(totalTime);
- }
- display.display();
- lastDisplayUpdate=millis();
- }
- void readThermister() {
- double Temp; // Variable that returns as a temperature value
- Temp = log(((12240000/analogRead(A0)) - 10000)); // Fancy math stuff
- Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Even fancier
- Temp = Temp - 273.15; // Convert Kelvin to Celcius
- degreesF = (Temp * 1.8) + 32.0; // Convert to degrees F
- }
- void loop()
- {
- if (target>160) target=160;
- Setpoint=target;
- readThermister();
- hotplatePID.Compute();
- if (millis()-lastDisplayUpdate>250) updateScreen();
- target=originalTemp+(controlKnob.read()/4);
- if ((millis()-timeMath)>250) {
- if (!digitalRead(timerCTL)) {
- timerRunning=!timerRunning;
- timeCounter=millis();
- timeMath=millis();
- }
- }
- if (millis()-lastTempChange>30000) {
- tempTemp= (byte) target;
- if (tempTemp != ((byte) EEPROM.read(10))) {
- EEPROM.write(10, tempTemp);
- lastTempChange=millis();
- } else lastTempChange=millis();
- }
- // PID Relay Control below
- if(millis() - windowStartTime>WindowSize)
- { //time to shift the Relay Window
- windowStartTime += WindowSize;
- }
- if (Output > millis() - windowStartTime) {
- if (degreesF<target) digitalWrite(relayPin, HIGH);
- }
- else digitalWrite(relayPin, LOW);
- if (degreesF>target) digitalWrite(relayPin, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement