# How to calculate temperature on/off constants

Please consider the following as pseudo code for my question

    double K1 = 50.0 / 300; // Warm up, 5 min, 20 - 70. Subject to change.
double K2 = -50.0 / 120; // Cool down, 2 min, 20 - 70. Subject to change.

double T = 20;
const int ON_TIME = 7; // seconds. this must be calculated
const int OFF_TIME = 2; // seconds. this must be calculated
int onCounter = ON_TIME;
int offCounter = 0;
for (int t = 0; t < 1800; t++)
{
if (onCounter > 0)
{
onCounter--;
T += K1;
}
else if(offCounter == 0)
{
onCounter = ON_TIME;
// switch relay off here
}

if (offCounter > 0)
{
T += K2;
offCounter--;
}
else if(onCounter == 0)
{
offCounter = OFF_TIME;
// switch relay on here
}
Console.WriteLine("t: {0}, T: {1:F2}", t, T);
}


What this function does is simulation of oven temperature during 30 minutes(Tt) linearly up to 70C. Of course it doesn't work as expected. The input parameters are two linear slopes.

1. Oven temperature will increase from 20C to 70C during 5 minutes (Tw)
2. Oven will cool down from 70C to 20C during 2 minutes (Tc)
3. The step should be 1C (Ts)

So the function will control relay, which will either turn on the oven or turn off. I have tried to just search those ON/OFF_TIME constans but it looks like I need more serious approach.

The question is - how to calculate ON_TIME and OFF_TIME?

-
On the temp increase side, the temp increases by 50 degrees C over 5 minutes, that is linearly 10 degrees/min. On the cool down side, it cools down 50 degrees over 2 minutes, that is, cools by 25 degrees/min. What are you representing by Ts being 1C? –  Amzoti Jan 8 '13 at 21:34
@Amzoti: it's the maximum temperature that can drop in the oven down from the exact liner value, due to temperature adjustment. In other words it's precision. I don't think it's possible to get really a line during this adjustments, so I imagine this as sines around the line, with amplitude of 1C. If not clear let me know I will try to draw as much as I can :) –  Pablo Jan 8 '13 at 21:38
Without getting into a long discussion (against MSE rules), can you gather oven statistics and fit the data points to a curve for a more realistic oven warm and cool down period? Then, you can determine which side of the cool-up or cool-down you are on and use this curve with time as a component to make the decision? Some ovens even provide curve data to help in this regard. –  Amzoti Jan 8 '13 at 21:42
@Amzoti: I can collect that data later, but for now even linear function for warm up and cool down should be fine. I will just create a function now, which will give temperature value for given time. Now it's linear, later it can be exponential. But this should not affect the way I calculate on/off timer... –  Pablo Jan 8 '13 at 21:46

Added: it sounded like the oven was constantly cycling between the hot and cold limits. Now it sounds like you want to stay at one level, more common in ovens. If you assume that the heating and cooling is linear (and you don't have any better data) and you want to hold accuracy of Ts, you can afford to be on for Ts/warming rate and off for Ts/cooling rate. With the constants you give that would be on $\frac {1C}{10\ C/min}=\frac 1{10} min = 6 sec$ With the faster cooling time, the off time will be $\frac 52$ times less or $2.4 sec$. You can divide by your timestep to get the number of cycles on and off. If the timestep is 0.2 sec, it would be $30$ cycles on, $12$ cycles off. The problem with this is that the temperature will tend to walk away if your constants are not right. That is why one usually measures the temperature and switches the relay in response to the measurement instead of forecasting the behavior.
Now that's very close to what I want. But if I set ON_TIME=30, OFF_TIME=12 the function above gives me temperature from 20C...160C. Do you think K1 and K2 are correct? There is also a way to measure temperature first and then switch relay on/off, but I can't imagine how to do that. First I need to understand this. If you could help me with calculating time needed to swith on/off relay based on current temperature reading, I would appreciate it. nevertheless even with the last method I still need to know how inert is the oven on switching, to be able to do it on time. –  Pablo Jan 8 '13 at 23:06
@Pablo: If you are sensing temperature, you just turn it off when the temperature gets higher than the setpoint (or maybe a little lower to allow for overshoot) then on when it gets lower. No timing involved at all. The max error is then the ramp rate times the timesetp. For the 20-160 question you should have the temperature increase $10 \frac C{min} *$ON_TIME*timestep$=50C$. K1 should be the increase per time step $10 \frac C{min} *$timestep. I don't see where you set the timestep, but it needs to be consistent between ON_TIME and K1. –  Ross Millikan Jan 8 '13 at 23:58