-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReheatable_Metal_Containerk.ino
More file actions
237 lines (197 loc) · 5.67 KB
/
Reheatable_Metal_Containerk.ino
File metadata and controls
237 lines (197 loc) · 5.67 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Reheatable_Metal_Container.ino
//
// The Reheatable Metal Container is a container that reheats food served within the canteen which heats
// them using PTC heating element up to 165 Fahrenheit in the time frame [10, 15, 20].
//
// Version YY//MM/DD Comments
// ======= ========= ==============================================================================
// 1.00 25/02/24 Created the foundation of the coding base of the RMC
// 1.10 25/02/25 Changed the temp sensor to the LM35 instead of the DHT11
// 1.20 25/03/02 Converted the Celcius and translated it into Fahrenheit
// 1.31 25/03/03 Fixed some codes that causes the timer to be slightly slower
// 1.41 25/03/09 Added the Debouncing function within the buttons to remove multiple readings
// 1.43 25/03/10 Slight changes to the assigned pins of modules
// 1.44 25/03/12 Birthday fixing
// 1.54 25/03/15 Added code to shut of the relay when temperature reaches 165 Fahrenheit
// 1.55 25/03/25 Code bug fix for the hysteresis shit
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <ezButton.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHT_TYPE DHT11 // Change to DHT11 when using DHT11 comp.
#define DHT_PIN 8 // DHT Sensor Pin
#define RELAY_PIN 9 // Relay Module Pin
#define temp_max 165
#define temp_min 157
const int reed = 10;
int reedstate;
ezButton button1(7);
ezButton button2(6);
ezButton button3(5);
ezButton button4(4);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHT_PIN, DHT_TYPE);
int heatingTime = 0;
bool heating = false;
unsigned long startTime = 0;
void setup() {
// Testing the accuracy of the LM35 sensor Celcius to Faren conversion
Serial.begin(9600); // Wow so many
// Initiate relay module
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
pinMode(reed, INPUT_PULLUP); // Mode for reed
// Initiate DHT and LCD
dht.begin();
lcd.begin(16, 2);
lcd.backlight();
// Debounce for the Buttons
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
}
void loop() {
// Reads the current temperature of the system
int sensorValue = analogRead(A3);
float voltage = sensorValue * (5.0 / 1023.0);
int temperatureC = voltage * 100;
int temperatureF = (temperatureC * 9 / 5) + 32;
button1.loop();
button2.loop();
button3.loop();
button4.loop();
// Codes for the Buttons
if (button1.isPressed()) {
stopHeating();
}
else if (button2.isPressed()) {
heatingTime = 10;
startHeating();
}
else if (button3.isPressed()) {
heatingTime = 15;
startHeating();
}
else if (button4.isPressed()) {
heatingTime = 20;
startHeating();
}
// Temperature-based control of the Relay
// Uses a Hysteresis-based control code to prevent rapid relay switching
// by creating a buffer zone between the temperatures
if(heating) {
if(temperatureF >= temp_max) {
digitalWrite(RELAY_PIN, LOW);
}
else if(temperatureF <= temp_min) {
digitalWrite(RELAY_PIN, HIGH);
}
}
if (heating) {
unsigned long elapsedTime = millis() - startTime;
unsigned long remainingTime = (heatingTime * 60000) - elapsedTime;
printTime(remainingTime);
printTH();
if (elapsedTime >= (heatingTime * 60000)) {
stopHeating();
}
}
// Calls forth thy debugging bs
debugSensors();
delay(200);
reedstate = digitalRead(reed);
if(digitalRead(reed) == HIGH) {
reedon();
}
else {
reedoff();
}
}
void printTH() {
// Code for LM35 Temperature sensor
int sensorvalue = analogRead(A3);
// Converts the analog reading of the LM35 to a readable version in voltage
float voltage = sensorvalue * (5.0 / 1023.0);
// Converts the voltage to temperature
// Also converts the float function to an integer function to remove decimal temperatures 00.xx C
int temperatureC = voltage * 100;
int temperatureF = temperatureC * 9 / 5 + 32;
// Code for the dht11 Sensor
int hum = dht.readHumidity();
// Print format: T: <Temperature>F H: <Humidity>%
lcd.setCursor(0, 0);
lcd.print("T: ");
lcd.print(temperatureF);
lcd.print((char)223);
lcd.print("F ");
lcd.print("H: ");
lcd.print(hum);
lcd.print("% ");
delay(200);
}
// Code for time function in the RMC
void printTime(unsigned long ms) {
int minutes = (ms / 60000);
int seconds = (ms / 1000) % 60;
// Print format: Time: <mm:ss>
lcd.setCursor(0, 1);
lcd.print("Time: ");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
lcd.print(" ");
}
// Uses the relay to turn ON the heating
void startHeating() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heating...");
digitalWrite(RELAY_PIN, HIGH);
heating = true;
startTime = millis();
}
// Uses the relay to turn OFF the heating
void stopHeating() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heating Done!");
digitalWrite(RELAY_PIN, LOW);
heating = false;
delay(2000);
reedon();
}
void reedoff() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lid Open!");
lcd.setCursor(0, 1);
lcd.print("Please Close!");
digitalWrite(RELAY_PIN, LOW);
heating = false;
}
void reedon() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Time:");
}
// Debugging Code
void debugSensors() {
int sensorValue = analogRead(A3);
float voltage = sensorValue * (5.0 / 1023.0);
int temperatureC = voltage * 100;
int temperatureF = temperatureC * 9 / 5 + 32;
Serial.print("LM35 Temp: ");
Serial.print(temperatureC);
Serial.print("°C / ");
Serial.print(temperatureF);
Serial.print("°F");
}