-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchiller_main.ino
141 lines (114 loc) · 3.11 KB
/
chiller_main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
//define the potentiometer pin on analog 1
#define POTPIN A1
//define the relay pin on digital 12
#define RELAY_PIN 11
//Sensor on digital pin 2, backup sensor on pin 6, and bottle probe on pin 9
#define DHTPIN 2
#define INT_PROBE 6
#define EXT_PROBE 9
//Sensor is a DHT22 sensor
#define DHTTYPE DHT22
//No real OLED pin on this board, but we need it for the library to work properly
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
//define the temp setpoint limits, margin, and delay before chiller can change states
#define MAX_TEMP 70
#define MIN_TEMP 50
#define MARGIN 2
#define CHILLER_DELAY 10000
//define the time to switch display cycles in milliseconds
#define DISPLAY_CYCLE 2000
//Initialize the dht object
DHT dht(DHTPIN, DHTTYPE);
//Initialize our variables
int humid;
int tempF;
int tempSelect;
int currentTime;
int displayTime = 0;
int chillerTime = 0;
bool dispTemp = true;
bool chillerState = false;
void setup()
{
//set digital pinmode
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
//Initialize the I2C interface and sensor
Wire.begin();
dht.begin();
//Initialize the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop()
{
getSensors();
currentTime = millis();
if (currentTime - chillerTime > CHILLER_DELAY) {
if (tempF > tempSelect + MARGIN && chillerState == false) {
chillerState = true;
digitalWrite(RELAY_PIN, HIGH);
}
else if (tempF < tempSelect - MARGIN && chillerState == true) {
chillerState = false;
digitalWrite(RELAY_PIN, LOW);
chillerTime = currentTime;
}
}
updateDisp(dispTemp);
display.display();
if (currentTime - displayTime > DISPLAY_CYCLE) {
if (dispTemp == true) {
dispTemp = false;
}
else {
dispTemp = true;
}
displayTime = currentTime;
}
delay(1);
}
void getSensors()
{
tempSelect = map(analogRead(POTPIN), 1020, 0, MIN_TEMP, MAX_TEMP);
tempF = (int)dht.readTemperature(true);
humid = (int)dht.readHumidity();
if (isnan(tempF) || isnan(humid)) {
}
}
void updateDisp(bool dataSelect)
{
if (isnan(humid) || isnan(tempF)) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5,0);
display.print("Failed to read temperature!");
return;
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 1);
display.print("Set: ");
display.print(tempSelect);
display.print(" F");
if (dataSelect == true) {
display.setTextSize(5);
display.setCursor(30,25);
display.print(tempF);
display.setTextSize(3);
display.print((char)247);
}
else {
display.setTextSize(5);
display.setCursor(20,25);
display.print(humid);
display.print("%\t");
}
}