How's this look for control code so far?
/* Code for thermal controller
Turn on relay to turn on heat element, off for off.
> LED on relay shows relay commanded state and that transistor is working
AD595 with Type K thermocouple
LCD screen for visual feedback. (not added yet)
LED on D13 for status, attention getter? "Go read the screen"
3 buttons for user input - only Start implemented so far
CP2102 module to Rx/Tx for code download, or feedback to PC for future datalogging
*/
// define IO used
byte button0 = 2; // low = on Start/Select
byte button1 = 3; // low = on Temp Up
byte button2 = 4; // low = on Temp Down
byte LCDpins[8] = {
5, 6, 7, 8, 9, 10, 11, 12}; // array of data pins, 5 = LSB, 12 = MSB
byte statusLED = 13; // high = on
byte LCD_E = 14;
byte LCD_RW = 15;
byte LCD_RS = 16;
byte relay = 17; // high = on
byte free18 = 18;
byte temperaturePin = A5; // AD595 output
// define variables
int AD595output; // mV, from 0.003 to 3.022
// resulting ADC reading are from 1 to 618
// correlation: ADCreading/2 = tempC below readings <160
// correlation: (ADCreading/2)-5 =tempC for reading >=160
// define the the stages to go thru:
int temperature;
byte thermalStage = 0; // state machine variables
byte ovenoff = 0;
byte preheatRamp = 1;
byte preheat = 2;
byte preflowRamp=3;
byte reflow=4;
byte cooldown = 5;
byte running = 0;
// time variables
unsigned long preheatRampStartTime; // display times, not added yet
unsigned long lastTempTime;
unsigned long preheatStartTime;
unsigned long preheatDuration = 60000; // make longer, up to 120 seconds
unsigned long reflowStartTime;
unsigned long reflowDuration = 30000;
void setup(){
pinMode (button0, INPUT); // input with internal pullup enabled
digitalWrite (button0, HIGH);
pinMode (button1, INPUT); // input with internal pullup enabled
digitalWrite (button1, HIGH);
pinMode (button2, INPUT); // input with internal pullup enabled
digitalWrite (button2, HIGH);
for (byte x = 0; x<8; x=x+1){
pinMode (LCDpins[x], OUTPUT); // LCD data pins as outputs
}
pinMode (statusLED, OUTPUT);
pinMode (LCD_E, OUTPUT); // set hi or low to start?
pinMode (LCD_RW, OUTPUT); // set hi or low to start?
pinMode (LCD_RS, OUTPUT); // set hi or low to start?
pinMode (relay, OUTPUT); // leave low to start
pinMode (free18, INPUT); // input with pullup for now - turn into door open test?
digitalWrite (free18, HIGH);
Serial.begin(9600); // serial output for debugging
AD595output = analogRead (temperaturePin);
if (AD595output<160){
temperature = AD595output>>1; // divide by 2
}
else {
temperature = (AD595output>>1)-5; // divide by 2, subtract 5
}
Serial.print ("Start Temperature = ");
Serial.println (temperature);
} // end setup
void loop(){
/* start with running predetermined profile
Go from Room Temp to Preheat, 150-180C
Hold Preheat for 30-60 seconds
Go to Liquid/Flow temperature, 210-225C.. DO NOT EXCEED 255
Hold Liquid/Flow temperature 30 seconds
Ramp down
*/
// not running, not started, show ambient temp
if (running == 0 && digitalRead (button0 == 1)){
AD595output = analogRead (temperaturePin);
if (AD595output<160){
temperature = AD595output>>1;
} // divide by 2
else {
temperature = (AD595output>>1)-5;
} // divide by 2, subtract 5
if ( (millis() - lastTempTime) >=500){
lastTempTime = millis();
Serial.print ("static temperature = ");
Serial.print (temperature);
}
}
// startup if Not running & Start button is pushed
if (running == 0 && digitalRead(button0) == 0){
preheatRampStartTime = preheatRampStartTime + 500;
thermalStage = preheatRamp;
running = 1;
digitalWrite (statusLED, HIGH);
Serial.println ("preheat Ramp starting ");
}
if (running == 1){
// record the temperature
AD595output = analogRead (temperaturePin);
if (AD595output<160){
temperature = AD595output>>1;
} // divide by 2
else {
temperature = (AD595output>>1)-5;
} // divide by 2, subtract 5
if ( (millis() - lastTempTime) >=500){
lastTempTime = lastTempTime+500;
Serial.print ("loop temperature = ");
Serial.print (temperature);
}
}
// ********************************
switch(thermalStage){
/*
thermalStage's:
ovenoff = 0;
preheatRamp = 1;
preheat = 2;
preflowRamp=3;
reflow=4;
cooldown = 5;
*/
case 0: //ovenoff:
digitalWrite (relay, 0);
break;
// ********************************
case 1: // preheatRamp:
if (temperature <=145){
// keep ramping up
digitalWrite (relay, 1);
}
if (temperature >145 && temperature <170){
preheatStartTime = millis();
preheatRampStartTime = preheatStartTime;
Serial.println("preheat starting ");
thermalStage = preheat;
digitalWrite (relay, 0); // let next stage turn on if needed
break;
// ********************************
case 2: // preheat:
if (millis() - preheatStartTime <=preheatDuration){
if (temperature <170){
digitalWrite (relay, 1);
}
else {
digitalWrite (relay, 0);
}
}
else {
thermalStage = preflowRamp;
digitalWrite (relay, 1); // ramping up!
Serial.println("preflow rampup starting ");
}
break;
// ********************************
case 3: // preflowRamp:
if (temperature <=200){
// keep ramping up
digitalWrite (relay, 1); // ramping up!
}
if (temperature >200 && temperature <220){
reflowStartTime = millis();
thermalStage = reflow;
digitalWrite (relay, 0); // let next stage turn on if needed
Serial.println("Reflow starting ");
}
break;
// ********************************
case 4: // reflow:
if (millis() - reflowStartTime <=reflowDuration){
if (temperature <220){
digitalWrite (relay, 1);
}
else {
digitalWrite (relay, 0);
}
}
else {
thermalStage = cooldown;
digitalWrite (relay, 0); // cooling down!
Serial.println("cooldown starting ");
}
break;
// ********************************
case 5: // cooldown:
digitalWrite (relay, 0);
if (temperature <180){
Serial.println ("Safe to open door - Caution! HOT!");
thermalStage = ovenoff;
running = 0;
digitalWrite (statusLED, LOW);
}
break;
} // end switch thermalstage
} // end if running
} // end void loop