Snippets

GDS Pioneering Light Oven Controller

Created by Robb Gosset last modified Mike Jeskins
  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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702

#include <SPI.h>
#include <stdlib.h>

/* Settings Descriptions
 *  Temps are in deg C and can be accurate to 0.25 degrees
 *  Ramp and Hold times are in minutes
 *  
 *  Ramp times are time taken to reach temperature, eg
 *  with the following settings 
 * int   PorfileASteps   =  5;
 * float ProfileATemps[] = {10.1, 20.2, 30.3, 20.2, 10.1};
 * int   ProfileARamps[] = {5,    2,    1,    2,    5};
 * int   ProfileAHolds[] = {1,    2,    1,    1,    1};
 * 
 * Arduino will start by taking 5 minutes to ramp to 10.1 degrees, and hold that temp
 * for 1 minute, then it will take 2 minutes to ramp to 20.2 and hold for 2 minutes, etc
 * 
 * Arduino will wait untill oven temperature matches controller temperature before ending cycle       
 * 
 */

// Profile A Settings - Bottom Button - First Cure - green
int   ProfileASteps   =  1;
float ProfileATemps[] = { 60};
float ProfileARamps[] = {  5};
int   ProfileAHolds[] = {130};
#define ProfileAFinish 60
#define buttonA 6
#define ledA 10

// Profile B Settings - Top button - blue
int   ProfileBSteps   =  3;
float ProfileBTemps[] = {20, 40, 80};
float ProfileBRamps[] = {1, 5, 15};
int   ProfileBHolds[] = {30, 10, 60};
#define ProfileBFinish 80
#define buttonB 7
#define ledB 11

// Other Settings
#define fanRelay  8
#define heatRelay 9

#define fanOn()   digitalWrite(fanRelay, LOW)
#define fanOff()  digitalWrite(fanRelay, HIGH);

#define heatOn()  digitalWrite(heatRelay, LOW);
#define heatOff() digitalWrite(heatRelay, HIGH);

// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:

#define MAXDO   3
#define MAXCS   4
#define MAXCLK  5

class Adafruit_MAX31855 {
 public:
  Adafruit_MAX31855(int8_t SCLK, int8_t CS, int8_t MISO);
  Adafruit_MAX31855(int8_t CS);

  double readInternal(void);
  double readCelsius(void);
  double readFarenheit(void);
  double readFahrenheit(void);
  uint8_t readError();

 private:
  int8_t sclk, miso, cs, hSPI;
  uint32_t spiread32(void);
  uint32_t hspiread32(void);
};

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

int stage = 0;   // Stage of profile
int runTimeMinute = 0; // Time spent ramping in minutes
int runTimeSecond = 0;
double ovenTemp = 0;
long lastMillisMinute = 0;
long lastMillisSecond = 0;

enum {
  Idle,
  RunARamping,
  RunAHolding,
  RunAEnding,
  RunBRamping,
  RunBHolding,
  RunBEnding
} ovenStatus;

int refresh = 1000;

//#define DEBUG
//#define DEBUGTiming
//#define DEBUGCalc

float calculateTarget (float prevTemp, int runTimeMinute, float rampTime, float targetTemp)
{
  float delta = targetTemp - prevTemp ;
  float interpolation = (runTimeMinute+1)/rampTime;
  float target = (delta*interpolation) + prevTemp;
#ifdef DEBUGCalc
  Serial.print(" target - ");
  Serial.print(target);
  Serial.print(" delta - ");
  Serial.print(delta);
  Serial.print(" interpolation - ");
  Serial.print(interpolation);
#endif
  return target;
}

void checkHeater (float targetTemp)
{
  if (ovenTemp <= targetTemp)
  {
    if ((lastMillisSecond + 1000) < millis())
    {
      runTimeSecond++;
      lastMillisSecond = millis();
    }
    Serial.print(" runTimeSecond: ");
    Serial.print(runTimeSecond);
    float delta = targetTemp-(ovenTemp-1);
    delta = (delta / 10); // If greater that 10deg difference then just have it on all the time
    delta = delta * 30; // 30 second modulation, as we approach our target the time the heater will be on goes down
    Serial.print(" checkHeater delta: ");
    Serial.print(delta);
    if ((delta > runTimeSecond) && (30 >= runTimeSecond))
    {
      Serial.print(" heat on");
        heatOn();
    }
    else if (30 > runTimeSecond) // if runTimeSecond is between delta and 30 seconds
    {
      Serial.print(" heat off");
      heatOff();
    }
    else
    {
      Serial.print(" heat off - Reset");
      heatOff();
      runTimeSecond = 0;
    }
    
  }
  else
  {
    Serial.print(" heat off");
    heatOff();
    runTimeSecond = 0;
  } 
}

void setup() {
  Serial.begin(115200);
  
  Serial.println("ArcLamp Curing Oven Controller");

  ovenStatus = Idle;
    
  // Initialise Ports
  Serial.println("Initialising Ports");
  pinMode(buttonA, INPUT);
  pinMode(buttonB, INPUT);
  pinMode(fanRelay, OUTPUT);
  pinMode(heatRelay, OUTPUT);
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);

  digitalWrite(fanRelay, HIGH);
  digitalWrite(heatRelay, HIGH);
  digitalWrite(buttonA, HIGH);
  digitalWrite(buttonB, HIGH);
  digitalWrite(ledA, HIGH);
  digitalWrite(ledB, HIGH);
  
  Serial.println("Current Profiles: ");
  Serial.print("Profile A - Stages: ");
  Serial.println(ProfileASteps);

  for(int i = 0; i < ProfileASteps; i++)
  {
    Serial.print("Stage ");
    Serial.print(i);
    Serial.print(" - Temp: ");
    Serial.print(ProfileATemps[i]);
    Serial.print(" Ramp Time: ");
    Serial.print(ProfileARamps[i]);
    Serial.print(" Hold Time: ");
    Serial.println(ProfileAHolds[i]);
  }

  Serial.print("Profile B - Stages: ");
  Serial.println(ProfileBSteps);

  for(int i = 0; i < ProfileBSteps; i++)
  {
    Serial.print("Stage ");
    Serial.print(i);
    Serial.print(" - Temp: ");
    Serial.print(ProfileBTemps[i]);
    Serial.print(" Ramp Time: ");
    Serial.print(ProfileBRamps[i]);
    Serial.print(" Hold Time: ");
    Serial.println(ProfileBHolds[i]);
  }

  lastMillisMinute = millis();
}

bool checkCancel (void)
{
  if (digitalRead(buttonB))
      {
        heatOff();
        fanOff();
        ovenStatus=Idle;
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        while(digitalRead(buttonB))
        {
          // Do nothing
        }
        return true;
      }
      if (digitalRead(buttonA))
      {
        heatOff();
        fanOff();
        ovenStatus=Idle;
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        while(digitalRead(buttonA))
        {
          // Do nothing
        }
        return true;
      }
      return false;
}

void loop() {
  // basic readout test, just print the current temp
  float ambientTemp = thermocouple.readInternal();
  Serial.print("Ambient: ");
  Serial.print(ambientTemp);
  Serial.print(" dC  ");

  ovenTemp = thermocouple.readCelsius();
  if (isnan(ovenTemp)) {
    Serial.println("Something wrong with thermocouple!");
    fanOff();
    heatOff();
  } else {
    Serial.print("Oven: "); 
    Serial.print(ovenTemp);
    Serial.print(" dC  ");
  }

#ifdef DEBUGTiming
  Serial.print("lastMillisMinute: ");
  Serial.print(lastMillisMinute);
#endif
#ifdef DEBUG
  if ((lastMillisMinute + 2000) < millis())
#else
  if ((lastMillisMinute + 60000) < millis())
#endif
  {
    runTimeMinute++;
    lastMillisMinute = millis();
  }

  switch (ovenStatus)
  {
    case Idle:
      heatOff();
      fanOff();
      Serial.print(" Idle");
      digitalWrite(ledA, HIGH);
      digitalWrite(ledB, HIGH);
      if (digitalRead(buttonA))
      {
        fanOn();
        // Button A pushed, we need to start
        ovenStatus = RunARamping;
        // First stage is to ramp from ambient to stage 0 so
        stage = 0;
        runTimeMinute = 0;
        digitalWrite(ledA, LOW);
        while(digitalRead(buttonA))
        {
          // Do nothing
        }
      }
      if (digitalRead(buttonB))
      {
        fanOn();
        // Button B pushed, we need to start
        ovenStatus = RunBRamping;
        // First stage is to ramp from ambient to stage 0 so
        stage = 0;
        runTimeMinute = 0;
        digitalWrite(ledB, LOW);
        while(digitalRead(buttonB))
        {
          // Do nothing
        }
      }
      break;

    case RunARamping:
      
      Serial.print("RampA runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" stage: ");
      Serial.print(stage);
      if(checkCancel())
      {
        break;
      }
      if(0 == stage)
      {
        float target = calculateTarget(ambientTemp, runTimeMinute, ProfileARamps[stage], ProfileATemps[stage]);
        Serial.print(" Target: ");
        Serial.print(target);
        checkHeater(target);
      }
      else
      {
        
        float target = calculateTarget(ProfileATemps[stage-1], runTimeMinute, ProfileARamps[stage], ProfileATemps[stage]);
        Serial.print(" Target: ");
        Serial.print(target);
        checkHeater(target);
      }
      if(ProfileARamps[stage] <= (runTimeMinute-1))
      {
        ovenStatus = RunAHolding;
        runTimeMinute = 0;
      }
      break;

    case RunAHolding:
      Serial.print("HoldA runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" stage: ");
      Serial.print(stage);
      Serial.print(" Target: ");
      Serial.print(ProfileATemps[stage]);
      if(checkCancel())
      {
        break;
      }
      checkHeater(ProfileATemps[stage]);
    
      if(ProfileAHolds[stage] < runTimeMinute)
      {
        ovenStatus = RunARamping;
        stage++;
        if( ProfileASteps <= stage)
        {
          stage = 0;
          ovenStatus = RunAEnding;
        }
        runTimeMinute = 0;
      }
      break;

    case RunAEnding:
      Serial.print("End A runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" Target: ");
      Serial.print(ambientTemp+10);
      if (ovenTemp < ProfileAFinish)
      {
        if(checkCancel())
        {
          break;
        }
        if ((lastMillisSecond + 1000) < millis())
        {
          runTimeSecond++;
          lastMillisSecond = millis();
        }
        Serial.print(" runTimeSecond: ");
        Serial.print(runTimeSecond);
        if (1 > runTimeSecond) // if runTimeSecond is between delta and 30 seconds
        {
          fanOn();
          digitalWrite(ledA, LOW);
        }
        else if (2 > runTimeSecond)
        {
          fanOff();
          digitalWrite(ledA, HIGH);
        }
        else
        {
          fanOn();
          digitalWrite(ledA, LOW);
          runTimeSecond = 0;
        }
        heatOff();
      }
      else
      {
        fanOn();
        digitalWrite(ledA, LOW);
        heatOff();
      }
      if(ovenTemp < (ambientTemp+10))
      {
        fanOff();
        heatOff();
        digitalWrite(ledA, HIGH);
        ovenStatus = Idle;
      }
      break;
      
    case RunBRamping:
   
      Serial.print("RampB runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" stage: ");
      Serial.print(stage);
      if(checkCancel())
      {
        break;
      }
      if(0 == stage)
      {
        float target = calculateTarget(ambientTemp, runTimeMinute, ProfileBRamps[stage], ProfileBTemps[stage]);
        Serial.print(" Target: ");
        Serial.print(target);
        checkHeater(target);
      }
      else
      {
        
        float target = calculateTarget(ProfileBTemps[stage-1], runTimeMinute, ProfileBRamps[stage], ProfileBTemps[stage]);
        Serial.print(" Target: ");
        Serial.print(target);
        checkHeater(target);
      }
      if(ProfileBRamps[stage] <= runTimeMinute)
      {
        ovenStatus = RunBHolding;
        runTimeMinute = 0;
      }
      break;
      
    case RunBHolding:
      Serial.print("HoldB runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" stage: ");
      Serial.print(stage);
      Serial.print(" Target: ");
      Serial.print(ProfileBTemps[stage]);
      if(checkCancel())
      {
        break;
      }
      checkHeater(ProfileBTemps[stage]);
      
      if(ProfileBHolds[stage] < runTimeMinute)
      {
        ovenStatus = RunBRamping;
        stage++;
        if( ProfileBSteps <= stage)
        {
          stage = 0;
          ovenStatus = RunBEnding;
        }
        runTimeMinute = 0;
      }
      break;
      
    case RunBEnding:
      Serial.print("End B runTimeMinute: ");
      Serial.print(runTimeMinute);
      Serial.print(" Target: ");
      Serial.print(ambientTemp+10);
      if (ovenTemp < ProfileBFinish)
      {
        if(checkCancel())
        {
          break;
        }
        if ((lastMillisSecond + 1000) < millis())
        {
          runTimeSecond++;
          lastMillisSecond = millis();
        }
        Serial.print(" runTimeSecond: ");
        Serial.print(runTimeSecond);
        if (1 > runTimeSecond) // if runTimeSecond is between delta and 30 seconds
        {
          fanOn();
          digitalWrite(ledB, LOW);
        }
        else if (2 > runTimeSecond)
        {
          fanOff();
          digitalWrite(ledB, HIGH);
        }
        else
        {
          fanOn();
          digitalWrite(ledB, LOW);
          runTimeSecond = 0;
        }
        heatOff();
      }
      else
      {
        fanOn();
        digitalWrite(ledB, LOW);
        heatOff();
      }
      if(ovenTemp < (ambientTemp+10))
      {
        fanOff();
        heatOff();
        digitalWrite(ledB, HIGH);
        ovenStatus = Idle;
      }
      break;

    default:
      fanOff();
      heatOff();
      break;
  }
  


   
   Serial.println();
}

Adafruit_MAX31855::Adafruit_MAX31855(int8_t SCLK, int8_t CS, int8_t MISO) {
  sclk = SCLK;
  cs = CS;
  miso = MISO;
  hSPI = 0;

  //define pin modes
  pinMode(cs, OUTPUT);
  pinMode(sclk, OUTPUT); 
  pinMode(miso, INPUT);

  digitalWrite(cs, HIGH);
}

Adafruit_MAX31855::Adafruit_MAX31855(int8_t CS) {
  cs = CS;
  hSPI = 1;

  //define pin modes
  pinMode(cs, OUTPUT);
  
  //start and configure hardware SPI
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  
  digitalWrite(cs, HIGH);
}

double Adafruit_MAX31855::readInternal(void) {
  uint32_t v;

  v = spiread32();

  // ignore bottom 4 bits - they're just thermocouple data
  v >>= 4;

  // pull the bottom 11 bits off
  float internal = v & 0x7FF;
  // check sign bit!
  if (v & 0x800) {
    // Convert to negative value by extending sign and casting to signed type.
    int16_t tmp = 0xF800 | (v & 0x7FF);
    internal = tmp;
  }
  internal *= 0.0625; // LSB = 0.0625 degrees
  //Serial.print("\tInternal Temp: "); Serial.println(internal);
  return internal;
}

double Adafruit_MAX31855::readCelsius(void) {

  int32_t v;

  v = spiread32();

  //Serial.print("0x"); Serial.println(v, HEX);

  /*
  float internal = (v >> 4) & 0x7FF;
  internal *= 0.0625;
  if ((v >> 4) & 0x800) 
    internal *= -1;
  Serial.print("\tInternal Temp: "); Serial.println(internal);
  */

  if (v & 0x7) {
    // uh oh, a serious problem!
    return NAN; 
  }

  if (v & 0x80000000) {
    // Negative value, drop the lower 18 bits and explicitly extend sign bits.
    v = 0xFFFFC000 | ((v >> 18) & 0x00003FFFF);
  }
  else {
    // Positive value, just drop the lower 18 bits.
    v >>= 18;
  }
  //Serial.println(v, HEX);
  
  double centigrade = v;

  // LSB = 0.25 degrees C
  centigrade *= 0.25;
  return centigrade;
}

uint8_t Adafruit_MAX31855::readError() {
  return spiread32() & 0x7;
}

double Adafruit_MAX31855::readFahrenheit(void) {  //'fahrenheit' is spelled right.
  return readFarenheit();
}

double Adafruit_MAX31855::readFarenheit(void) {  //'farenheit' is spelled wrong.
  float f = readCelsius();
  f *= 9.0;
  f /= 5.0;
  f += 32;
  return f;
}

uint32_t Adafruit_MAX31855::spiread32(void) { 
  int i;
  uint32_t d = 0;

  if(hSPI) {
    return hspiread32();
  }

  digitalWrite(sclk, LOW);
  delay(1);
  digitalWrite(cs, LOW);
  delay(1);

  for (i=31; i>=0; i--)
  {
    digitalWrite(sclk, LOW);
    delay(1);
    d <<= 1;
    if (digitalRead(miso)) {
      d |= 1;
    }

    digitalWrite(sclk, HIGH);
    delay(1);
  }

  digitalWrite(cs, HIGH);
  //Serial.println(d, HEX);
  return d;
}

uint32_t Adafruit_MAX31855::hspiread32(void) {
  int i;
  // easy conversion of four uint8_ts to uint32_t
  union bytes_to_uint32 {
    uint8_t bytes[4];
    uint32_t integer;
  } buffer;
  
  digitalWrite(cs, LOW);
  delay(1);
  
  for (i=3;i>=0;i--) {
    buffer.bytes[i] = SPI.transfer(0x00);
  }
  
  digitalWrite(cs, HIGH);
  
  return buffer.integer;
  
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.