void ControlHeat() {
// Control heat output based on PID calculations
// MLT takes preference over HLT
PID_WINDOW_SIZE = 5000; // this lives in the variable declarations, not in the main loop.
unsigned long NOW = millis();
// If the HLT is enabled, calculate the PID output
if HLT_HEAT_ENABLED && ((HLT_TEMP_SETPOINT - HLT_TEMP_CURRENT) > 5.0) { // HLT's heating but we're more than 5deg below setpoint
HLT_PID_OUTPUT = PID_WINDOW_SIZE; // = window size
}
else if HLT_HEAT_ENABLED && ((HLT_TEMP_SETPOINT - HLT_TEMP_CURRENT) < 5.0) { // HLT's heating but we're LESS than 5deg below setpoint
HLT_PID.Calculate();
}
// else HLT PID isn't enabled
end if
// Do the same as above for the MLT
// If the MLT is enabled, calculate the PID output
if MLT_HEAT_ENABLED && ((MLT_TEMP_SETPOINT - MLT_TEMP_CURRENT) > 5.0) { // MLT's heating but we're more than 5deg below setpoint
MLT_PID_OUTPUT = PID_WINDOW_SIZE; // = window size
}
else if MLT_HEAT_ENABLED && ((MLT_TEMP_SETPOINT - MLT_TEMP_CURRENT) < 5.0) { // MLT's heating but we're LESS than 5deg below setpoint
MLT_PID.Calculate();
}
// else MLT PID isn't enabled
end if
if (NOW - PID_WINDOW_START_TIME) > PID_WINDOW_SIZE)) { // If we've past the end of a window, roll it over to start again
PID_WINDOW_START_TIME += PID_WINDOW_SIZE; // Put a debug in here to log window increment
}
if (MLT_HEAT_ENABLED && ! HLT_HEAT_ENABLED) { // MLT is ON, but HLT is OFF
if (MLT_PID_OUTPUT > (NOW - PID_WINDOW_START_TIME)) { // Output within window
digitalWrite (HLT_HEAT,LOW); // switch HLT off...
digitalWrite (MLT_HEAT,HIGH); // then switch MLT on.
}
else
digitalWrite (MLT_HEAT,LOW); // switch MLT OFF.
end if;
else if (HLT_HEAT_ENABLED && ! MLT_HEAT_ENABLED) { // HLT is ON, but MLT is OFF
if (HLT_PID_OUTPUT > (NOW - PID_WINDOW_START_TIME)) { // Output within window
digitalWrite (MLT_HEAT,LOW); // switch MLT off...
digitalWrite (HLT_HEAT,HIGH); // then switch HLT on.
}
else
digitalWrite (HLT_HEAT,LOW); // switch HLT OFF.
end if;
else if (HLT_HEAT_ENABLED && MLT_HEAT_ENABLED) { // Both enabled: HLT is ON, AND MLT is ON
if (MLT_PID_OUTPUT > (NOW - PID_WINDOW_START_TIME)) { // Output within window
digitalWrite (HLT_HEAT,LOW); // switch HLT off...
digitalWrite (MLT_HEAT,HIGH); // then switch MLT on.
}
else if ((MLT_PID_OUTPUT + HLT_PID_OUTPUT) > (NOW - PID_WINDOW_START_TIME)) { // Output within window
digitalWrite (MLT_HEAT,LOW); // switch MLT off...
digitalWrite (HLT_HEAT,HIGH); // then switch HLT on.
}
else
digitalWrite (HLT_HEAT,LOW); // switch HLT OFF.
digitalWrite (MLT_HEAT,LOW); // switch MLT OFF.
end if;
else // Neither HLT nor MLT enabled
digitalWrite (HLT_HEAT,LOW); // switch HLT OFF.
digitalWrite (MLT_HEAT,LOW); // switch MLT OFF.
end if;
}