-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBRC.ino
More file actions
446 lines (349 loc) · 10.4 KB
/
PBRC.ino
File metadata and controls
446 lines (349 loc) · 10.4 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
#include <SPI.h>
#include "Adafruit_MAX31855.h"
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define DEBUG 1
int thermocoupleSOPin = 12;
int thermocoupleCLKPin = 10;
int thermocoupleCS0Pin = 11; //changed from 11 to 1
//int thermocoupleCS1Pin = 12;
//int thermocoupleCS2Pin = 13;
int heat1Pin = A1;// heating element
int fanPin = 3;
int lcdRsPin = 8;
int lcdEPin = 9;
int lcdD4Pin = 4;
int lcdD5Pin = 5;
int lcdD6Pin = 6;
int lcdD7Pin = 7;
//int ledRedPin = 10;
int switch1Pin = A0;//the "Right" Switch
int switchAVal = 0;//analog read value for the switch
int fanPot = A5 ;// Potentionmeter for the fan
bool resetLCD = 0;
int debounceTime = 50;
//switches
static int nullVal = 600;
static int leftVal = 260;
static int rightVal = 80;
bool switchRelease =0;
const int rollIndx = 5; //index for rolling average
int readIndx = 0; //reading index
double circ_c[rollIndx];
double tempAvg = 0;
double loopTemp = 0;// debug
float deltaTempCurr = 22.0;
float deltaTempPrev = 22.0;
float deltaTempDelta = 0;
bool deltaLoop = 0;
struct MinSec {
unsigned int Min;
unsigned int Sec;
};
MinSec roastTime = {0,0};
unsigned long elapsedTime = 0;
typedef enum ROAST_STATUS
{
S_IDLE,
S_ROAST,
S_ROAST_LOW,
} roastStatus_t;
roastStatus_t roastStatus = S_IDLE;
typedef enum SWITCH_STATE
{
NULL_VAL,
LEFT_VAL,
RIGHT_VAL,
} switchState_t;
switchState_t switchStateCurr = NULL_VAL;
switchState_t switchStatePrev = NULL_VAL;
LiquidCrystal lcd(lcdRsPin, lcdEPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin);
Adafruit_MAX31855 thermocouple0(thermocoupleCLKPin, thermocoupleCS0Pin, thermocoupleSOPin);
MinSec MinSecfromMillis(unsigned long x){
unsigned long runSec = x / 1000;
MinSec Result;
Result.Min = runSec / 60;
Result.Sec = runSec % 60;
return Result;
}
switchState_t switchResult(int x ){
switchState_t swState;
if (x <= rightVal){
swState = RIGHT_VAL;
}
else if( x >= leftVal && x <= nullVal){
swState = LEFT_VAL;
}
else {
swState = NULL_VAL;
}
return swState;
}
int confinePot(int x){
return map(x, 0, 1023, 0, 255);
}
int confinePotHeat(int x){
int y;
y = map(x, 0, 1023, 0, 255);
if(y < 77){//disallow setting the fan too low when there is heat
y = 77;
}
return y;
}
double avgTemp(){
double tempSum = 0;
double temp_c1 = thermocouple0.readCelsius();
#ifdef DEBUG
Serial.print("READ: ");
Serial.print(temp_c1);
Serial.print(" ");
#endif
if (isnan(temp_c1)){
circ_c[readIndx]=tempAvg;
}
else{
circ_c[readIndx]=temp_c1;
}
for (int i = rollIndx; i>0; i--){
tempSum += circ_c[i-1];
#ifdef DEBUG
Serial.print(i-1);
Serial.print(": ");
Serial.print(circ_c[i-1], 2);
Serial.print(" ");
#endif
}
tempAvg = (tempSum / rollIndx);
if (readIndx >= rollIndx){
readIndx = 0;
}
else{
readIndx ++;
}
#ifdef DEBUG
Serial.print("AVG: ");
Serial.print(tempAvg, 2);
Serial.print("\n");
#endif
return tempAvg;
}
void setup() {
// put your setup code here, to run once:
#ifdef DEBUG
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#endif
lcd.begin(16, 2);
//lcd.createChar(0, degree);
lcd.clear();
lcd.print("Roast");
lcd.setCursor(0, 1);
lcd.print("Control 0.2");
// SSR pin initialization to ensure reflow oven is off
pinMode(heat1Pin, OUTPUT);
pinMode(fanPin, OUTPUT);
digitalWrite(heat1Pin, LOW);
analogWrite(fanPin, 0);
pinMode(switch1Pin, INPUT);
// Start-up splash
for(int i = rollIndx; i == 0; i--){
circ_c[i]=thermocouple0.readCelsius();
}
delay(1000);
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant writes
//Why there is no native function for this idk
}
void loop() {
// Serial communication at 9600 bps
//************ROAST***********************************************************************************
switch(roastStatus) {
case S_ROAST:
digitalWrite(heat1Pin, HIGH);//set fan and heat to on
analogWrite(fanPin, confinePotHeat(analogRead(fanPot)));//This gross. It reads the POT, maps to the right valuespace and writes to an anal out
loopTemp = avgTemp();
roastTime = MinSecfromMillis(millis()-elapsedTime);//-elapsedTime
#ifdef DEBUG
Serial.println(roastTime.Min);
Serial.println(roastTime.Sec, 2);
// Serial.println(millis);
Serial.println(elapsedTime);
Serial.println(loopTemp);
Serial.println();
#endif
if(roastTime.Sec % 10 == 0 && resetLCD ==0){
lcd.begin(8,2);
resetLCD = 1;
}
else if(roastTime.Sec % 10 != 0){
resetLCD=0;
}
if(roastTime.Sec % 2 == 0 && deltaLoop == 0){
deltaLoop = 1;
deltaTempCurr = loopTemp;
deltaTempDelta = (deltaTempCurr - deltaTempPrev);
deltaTempPrev=deltaTempCurr;
}
else if(roastTime.Sec % 2 != 0)
deltaLoop=0;
lcd.setCursor(0,0);
lcd.print("Heat ");
lcd.setCursor(5, 0);
lcd.print(roastTime.Min);
lcd.setCursor(7,0);
lcd.print(":");
lcd.setCursor(8,0);
if (roastTime.Sec >9){//This is the easiest way I could find of making the seconds place always print two digits
lcd.print(roastTime.Sec);
}
else{
lcd.print(0);
lcd.print(roastTime.Sec);
}
lcd.setCursor(10,0);
lcd.print(" D");
lcd.setCursor(12,0);
lcd.print(deltaTempDelta , 1);
lcd.setCursor(0, 1);
lcd.print(loopTemp,1);
lcd.setCursor(7, 1);
lcd.print(thermocouple0.readInternal(),1);
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
switchAVal = analogRead(switch1Pin); //Read buttons
switchStateCurr = switchResult(switchAVal);
#ifdef DEBUG
Serial.flush();
Serial.print(" Switch Curr: ");
Serial.print(switchStateCurr);
Serial.print(" Switch Prev: ");
Serial.println(switchStatePrev);
#endif
if(switchStateCurr == 0 && switchStatePrev != 0){
if (switchStatePrev == 2){
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
roastStatus = S_IDLE;
}
else if (switchStatePrev == 1){
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
roastStatus = S_ROAST_LOW;
}
}
else {
roastStatus = S_ROAST;
}
switchStatePrev=switchStateCurr;
break;
//***********************IDLE******************************************************************
case S_IDLE:
digitalWrite(heat1Pin, LOW);//set fan and heat to off
digitalWrite(fanPin, LOW); //set fan and heat to off
loopTemp=avgTemp();
lcd.setCursor(0,0);
lcd.print("Idle");
lcd.setCursor(0, 1);
lcd.print(loopTemp,1);
lcd.setCursor(7, 1);
lcd.print(thermocouple0.readInternal(),1);
digitalWrite(lcdRsPin, LOW);//Stop errant writes
// Going analog
switchAVal = analogRead(switch1Pin);
switchStateCurr = switchResult(switchAVal);
#ifdef DEBUG
Serial.print("Switch Curr: ");
Serial.print(switchStateCurr);
Serial.print(" Switch Prev: ");
Serial.println(switchStatePrev);
#endif
if (switchStateCurr == 0 && switchStatePrev == 2){
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
elapsedTime=millis();
roastStatus = S_ROAST;
}
else {
roastStatus = S_IDLE;
}
switchStatePrev=switchStateCurr;
break;
//*****************************ROAST**LOW*************************************************************
case S_ROAST_LOW:
digitalWrite(heat1Pin, LOW);//set fan and heat to off
analogWrite(fanPin, confinePot(analogRead(fanPot)));//This gross. It reads the POT, maps to the right valuespace and writes to an anal out
roastTime = MinSecfromMillis(millis()-elapsedTime);
loopTemp = avgTemp();
if(roastTime.Sec % 10 == 0 && resetLCD ==0){
lcd.begin(8,2);
resetLCD = 1;
}
else if(roastTime.Sec % 10 !=0) {
resetLCD=0;
}
if(roastTime.Sec % 2 == 0 && deltaLoop == 0){
deltaLoop = 1;
deltaTempCurr = loopTemp;
deltaTempDelta = (deltaTempCurr - deltaTempPrev);
deltaTempPrev=deltaTempCurr;
}
else if(roastTime.Sec % 2 != 0)
deltaLoop=0;
lcd.setCursor (0, 0);
lcd.print("Low ");
lcd.setCursor(5, 0);
lcd.print(roastTime.Min);
lcd.setCursor(7,0);
lcd.print(":");
lcd.setCursor(8,0);
if (roastTime.Sec >9){
lcd.print(roastTime.Sec);
}
else{
lcd.print(0);
lcd.print(roastTime.Sec);
}
lcd.setCursor(10,0);
lcd.print(" D");
lcd.setCursor(12,0);
lcd.print(deltaTempDelta , 1);
lcd.setCursor(0, 1);
lcd.print(loopTemp,1);
lcd.setCursor(7, 1);
lcd.print(thermocouple0.readInternal(),1);
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
switchAVal = analogRead(switch1Pin);
switchStateCurr = switchResult(switchAVal);
#ifdef DEBUG
//Serial.flush();
Serial.print("Switch Curr: ");
Serial.print(switchStateCurr);
Serial.print(" Switch Prev: ");
Serial.println(switchStatePrev);
#endif
if(switchStateCurr == 0 && switchStatePrev != 0){
if (switchStatePrev == 2){
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
roastStatus = S_IDLE;
}
else if (switchStatePrev == 1){
lcd.clear();
digitalWrite(lcdRsPin, LOW);//Stop errant lcd writes
roastStatus = S_ROAST;
}
else {
roastStatus = S_ROAST_LOW;
}
}
switchStatePrev=switchStateCurr;
break;
//------------DEFAULT
default:
digitalWrite(heat1Pin, LOW);//set fan and heat to off
digitalWrite(fanPin, LOW);
roastStatus = S_IDLE;
break;
}
}