forked from rkeithryan/Arduino-Brewery-Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrewerycontroller
More file actions
576 lines (480 loc) · 17.2 KB
/
Copy pathbrewerycontroller
File metadata and controls
576 lines (480 loc) · 17.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <LiquidCrystal.h>
#include <OneWire.h> // Library for Temp sensor
#include <DallasTemperature.h> // Library for Temp conversion
#define ONE_WIRE_BUS A0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
int resolution = 12;
unsigned long lastTempRequest = 0;
int delayInMillis = 0;
float Temperature = 0.0;
int idle = 0;
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
const int LED_RED = 10; //Red LED
const int LED_GREEN = 11; //Green LED
const int RELAY = 13; //Relay
static int pinA = 3; // Our first hardware interrupt pin is digital pin 2
static int pinB = 2; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
const int selectSwitch = 12;
unsigned int mashSet = 60;
unsigned int mashTemp;
unsigned int timeSet = 60;
unsigned int mashTime;
unsigned int boilTime;
int seconds = 59;
int secondsCount;
unsigned long time_countdown = 0;
int menuValue = 0;
void setup() {
sensors.begin();
sensors.getAddress(tempDeviceAddress, 0);
sensors.setResolution(tempDeviceAddress, resolution);
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(RELAY, OUTPUT);
pinMode(selectSwitch, INPUT);
pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
Serial.begin(115200);
digitalWrite (selectSwitch, HIGH);
lcd.begin(16, 2);
digitalWrite(LED_GREEN, HIGH); //Green LED ON
digitalWrite(LED_RED, LOW); //Red LED OFF
digitalWrite(RELAY, LOW); //Turn off Relay
lcd.setCursor (3, 0);
lcd.print("Welcome To");
lcd.setCursor (3, 1);
lcd.print("BEERduino!");
delay(2500);
lcd.clear();
}
void PinA() {
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos ++; //decrement the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
sei(); //restart interrupts
}
void PinB() {
cli(); //stop interrupts happening before we read pin values
reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
encoderPos --; //increment the encoder's position count
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
sei(); //restart interrupts
}
void loop() {
//============================================================================================//
// Get user input for mash temp setpoint //
//============================================================================================//
encoderPos = 60;
while (menuValue == 0)
{
//Display Mash Set point on LCD
lcd.setCursor(1, 0);
lcd.print("Set Mash Temp:");
lcd.setCursor(6, 1);
lcd.print(encoderPos);
lcd.print(" C");
if (digitalRead(selectSwitch) == LOW)
{
mashTemp = encoderPos;
menuValue++;
lcd.clear();
}
delay (1);
}
if (menuValue == 1)
{
delay (500);
}
//============================================================================================//
// Get user input for mash time setpoint //
//============================================================================================//
encoderPos = 60;
while (menuValue == 1)
{
//Display Mash Time Set point on LCD
lcd.setCursor(1, 0);
lcd.print("Set Mash Time:");
lcd.setCursor(4, 1);
lcd.print(encoderPos);
lcd.print(" Mins");
if (digitalRead(selectSwitch) == LOW)
{
mashTime = encoderPos;
menuValue ++;
lcd.clear();
}
delay(1);
if (menuValue == 2)
{
delay (500);
}
}
//============================================================================================//
// Display user selected set points before starting for user to confirm //
//============================================================================================//
while (menuValue == 2)
{
//Display Set Mash Time and Set Mash Temp point on LCD
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(mashTemp);
lcd.print(" Time:");
lcd.print(mashTime);
lcd.setCursor(1, 1);
lcd.print("PRESS TO START");
if (digitalRead(selectSwitch) == LOW)
{
menuValue ++;
lcd.clear();
}
delay(100);
}
while (menuValue == 3)
{
delay (500);
menuValue ++;
}
//============================================================================================//
// Heating up to temp for mash. User to confirm once ready //
//============================================================================================//
while (menuValue == 4)
{
lcd.setCursor(1, 0);
lcd.print("Mash Temp: ");
lcd.print(Temperature);
lcd.setCursor(3, 1);
lcd.print("HEATING UP");
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
Temperature = sensors.getTempCByIndex(0);
idle = 0;
// immediately after fetching the temperature we request a new sample
// in the async modus
// for the demo we let the resolution change to show differences
resolution++;
if (resolution > 12) resolution = 9;
sensors.setResolution(tempDeviceAddress, resolution);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
}
if (Temperature > mashTemp)
{
digitalWrite(RELAY, LOW); //Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
for (int i = 0; i < 5; i++) {
tone (A1, 500);
delay (500);
noTone (A1);
delay (500);
}
menuValue ++;
}
if (Temperature < mashTemp)
{
digitalWrite(RELAY, HIGH); //Turn on heater
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
}
lcd.clear();
delay (500);
while (menuValue == 5) {
lcd.setCursor(0, 0);
lcd.print("Temp Reached: ");
lcd.print(Temperature);
lcd.setCursor(1, 1);
lcd.print("PRESS TO START");
if (digitalRead(selectSwitch) == LOW)
{
menuValue ++;
}
}
delay(500);
//-------------------------- MASH PROCESS ----------------------------------------------------//
//============================================================================================//
// Mash timer countdown //
//============================================================================================//
time_countdown = millis();
int minsCount = mashTime;
mashTime = mashTime - 1;
while (minsCount > 0 && seconds >= 0 && menuValue == 6)
{
//Display Mash Temp and Time on LCD
lcd.setCursor(0, 0);
lcd.print("Mash Temp: ");
lcd.print(Temperature);
lcd.setCursor(0, 1);
lcd.print("Mash Time: ");
if (mashTime < 10) {
lcd.print ("0");
lcd.print (mashTime);
} else {
lcd.print(mashTime);
}
lcd.print(":");
if (seconds < 10) {
lcd.print ("0");
lcd.print (seconds);
} else {
lcd.print (seconds);
}
if (millis() - time_countdown >= 999) { //The while loop ends when 'minsCount' become zero
seconds = seconds - 1;
secondsCount ++;
if (secondsCount == 60) {
mashTime = mashTime - 1;
secondsCount = 0;
seconds = 59;
minsCount = minsCount - 1;
}
time_countdown = millis();
}
//============================================================================================//
// Reading temp and cotrolling heater //
//============================================================================================//
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
Temperature = sensors.getTempCByIndex(0);
idle = 0;
// immediately after fetching the temperature we request a new sample
// in the async modus
// for the demo we let the resolution change to show differences
resolution++;
if (resolution > 12) resolution = 9;
sensors.setResolution(tempDeviceAddress, resolution);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
}
if (Temperature > mashTemp)
{
digitalWrite(RELAY, LOW); //Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
else
{
digitalWrite(RELAY, HIGH); //Turn on heater
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
//delay(100); //Update at every 100mSeconds
}
//============================================================================================//
// Mash finished. User has option to continue to boil //
//============================================================================================//
menuValue++;
while (menuValue == 7) {
digitalWrite(RELAY, LOW); //Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Finished Mash");
for (int i = 0; i < 5; i++) {
tone (A1, 500);
delay (500);
noTone (A1);
delay (500);
}
menuValue++;
}
while (menuValue == 8) {
lcd.setCursor (1, 1);
lcd.print ("PRESS FOR BOIL");
if (digitalRead(selectSwitch) == LOW)
{
lcd.clear();
menuValue ++;
delay (1000);
}
}
//--------------------------------- BOIL PROCESS ---------------------------------------------//
//============================================================================================//
// Getting user defined boil time //
//============================================================================================//
encoderPos = 60;
while (menuValue == 9) {
lcd.setCursor(3, 0);
lcd.print("Boil Time: ");
lcd.setCursor(5, 1);
lcd.print(encoderPos);
lcd.print(" Mins");
if (digitalRead(selectSwitch) == LOW)
{
boilTime = encoderPos;
menuValue ++;
lcd.clear();
}
delay(1);
if (menuValue == 10)
{
delay (500);
}
}
//============================================================================================//
// Heating to boling point (98C). User to confirm start //
//============================================================================================//
while (menuValue == 10)
{
lcd.setCursor(0, 0);
lcd.print("Wort Temp: ");
lcd.print(Temperature);
lcd.setCursor(0, 1);
lcd.print("HEATING TO BOIL");
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
Temperature = sensors.getTempCByIndex(0);
idle = 0;
// immediately after fetching the temperature we request a new sample
// in the async modus
// for the demo we let the resolution change to show differences
resolution++;
if (resolution > 12) resolution = 9;
sensors.setResolution(tempDeviceAddress, resolution);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
}
if (Temperature > 97 )
{
for (int i = 0; i < 5; i++) {
tone (A1, 500);
delay (500);
noTone (A1);
delay (500);
}
menuValue ++;
}
if (Temperature < 100)
{
digitalWrite(RELAY, HIGH); //Turn on heater
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
}
lcd.clear();
delay (500);
while (menuValue == 11) {
lcd.setCursor(0, 0);
lcd.print("Temp Reached: ");
lcd.print(Temperature);
lcd.setCursor(0, 1);
lcd.print("PRESS TO START");
if (digitalRead(selectSwitch) == LOW)
{
menuValue ++;
}
}
delay(500);
//============================================================================================//
// Boil timer countdown //
//============================================================================================//
time_countdown = millis();
int boilminsCount = boilTime;
boilTime = boilTime - 1;
while (boilminsCount > 0 && seconds >= 0 && menuValue == 12)
{
lcd.setCursor(0, 0);
lcd.print("Boil Temp: ");
lcd.print(Temperature);
lcd.setCursor(0, 1);
lcd.print("Boil Time: ");
if (boilTime < 10) {
lcd.print ("0");
lcd.print (boilTime);
} else {
lcd.print(boilTime);
}
lcd.print(":");
if (seconds < 10) {
lcd.print ("0");
lcd.print (seconds);
} else {
lcd.print (seconds);
}
if (millis() - time_countdown >= 999) { //The while loop ends when 'minsCount' become zero
seconds = seconds - 1;
secondsCount ++;
if (secondsCount == 60) {
boilTime = boilTime - 1;
secondsCount = 0;
seconds = 59;
boilminsCount = boilminsCount - 1;
}
time_countdown = millis();
}
//============================================================================================//
// Reading temp and controlling heater //
//============================================================================================//
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
Temperature = sensors.getTempCByIndex(0);
idle = 0;
// immediately after fetching the temperature we request a new sample
// in the async modus
// for the demo we let the resolution change to show differences
resolution++;
if (resolution > 12) resolution = 9;
sensors.setResolution(tempDeviceAddress, resolution);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
}
if (Temperature > 100)
{
digitalWrite(RELAY, LOW); //Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
}
else
{
digitalWrite(RELAY, HIGH); //Turn on heater
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH); //Turn on RED LED
}
//delay(100); //Update at every 100mSeconds
}
menuValue++;
//============================================================================================//
// Boil finished //
//============================================================================================//
while (menuValue == 13) {
digitalWrite(RELAY, LOW); //Turn off heater
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH); //Turn on Green LED
lcd.clear ();
lcd.setCursor (1, 0);
lcd.print ("FINISHED BOIL");
for (int i = 0; i < 5; i++) {
tone (A1, 500);
delay (500);
noTone (A1);
delay (500);
}
}
}