forked from r-downing/AutoPID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicTempControl.ino
73 lines (56 loc) · 2.1 KB
/
BasicTempControl.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
AutoPID BasicTempControl Example Sketch
This program reads a dallas temperature probe as input, potentiometer as setpoint, drives an analog output.
It lights an LED when the temperature has reached the setpoint.
*/
#include <AutoPID.h>
#include <DallasTemperature.h>
#include <OneWire.h>
//pins
#define POT_PIN A0
#define OUTPUT_PIN A1
#define TEMP_PROBE_PIN 5
#define LED_PIN 6
#define TEMP_READ_DELAY 800 //can only read digital temp sensor every ~750ms
//pid settings and gains
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP .12
#define KI .0003
#define KD 0
double temperature, setPoint, outputVal;
OneWire oneWire(TEMP_PROBE_PIN);
DallasTemperature temperatureSensors(&oneWire);
//input/output variables passed by reference, so they are updated automatically
AutoPID myPID(&temperature, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
unsigned long lastTempUpdate; //tracks clock time of last temp update
//call repeatedly in loop, only updates after a certain time interval
//returns true if update happened
bool updateTemperature() {
if ((millis() - lastTempUpdate) > TEMP_READ_DELAY) {
temperature = temperatureSensors.getTempFByIndex(0); //get temp reading
lastTempUpdate = millis();
temperatureSensors.requestTemperatures(); //request reading for next time
return true;
}
return false;
}//void updateTemperature
void setup() {
pinMode(POT_PIN, INPUT);
pinMode(OUTPUT_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
temperatureSensors.begin();
temperatureSensors.requestTemperatures();
while (!updateTemperature()) {} //wait until temp sensor updated
//if temperature is more than 4 degrees below or above setpoint, OUTPUT will be set to min or max respectively
myPID.setBangBang(4);
//set PID update interval to 4000ms
myPID.setTimeStep(4000);
}//void setup
void loop() {
updateTemperature();
setPoint = analogRead(POT_PIN);
myPID.run(); //call every loop, updates automatically at certain time interval
analogWrite(OUTPUT_PIN, outputVal);
digitalWrite(LED_PIN, myPID.atSetPoint(1)); //light up LED when we're at setpoint +-1 degree
}//void loop