# Modelling Temperature Sensor Delay

Hi all, I'm incredibly new to programming and trying to figure out how to compensate for temperature sensor delay in my program. My problem is that I don't know how to store the previous reading and recall it for use in the equation.

The equation is basically Temperature = TempRead + (TempRead - TempPrev) where Temperature is a proposed temperature, TempRead is the current reading and TempPrev is the previous reading.

Can anyone help point me in the right direction?

this?

``````setup()
{
TempPrev = getTemp(); // function that wraps the reading of the temperature
}
loop()
{
...
}
``````

That code will do what you asked for, but what you asked for doesn't seem to actually make sense.

What exactly are you trying to calculate with that equation ?

Robtillaart - thanks so much for your help!

It's part of a larger system, I'm just trying to minimise the delay of the temperature sensor by predicting what the temperature actually is.

So you are adding an estimate of the rate of temperature change, to the latest reading ? To try to predict what the following reading will be ?

At the rate at which a microcontroller would be reading the temperature, that hardly seems necessary. You may end up simply confusing yourself by amplifying the "noise" which will be present in the temperature readings.

Temperatures are slow moving inputs compared to the speed of any control devices, so the issue is not generally to predict the temperature changes but to smooth out any of the noise that happens on the input from the sensor. This is usually done by taking an average of a number of values over a set period of time and calling that the 'tempertaure'.

``````unsigned short Temperature(void) {
static unsigned short TempPrev=0;
return tmp;
}
``````

All if wrap'd in the function call.

It's for a uni project where they specifically want the temperature change to be predicted in the equation I gave, otherwise I would do what you guys are suggesting :)

This is where things like the PID library can also come in handy.

http://www.arduino.cc/playground/Code/PIDLibrary

"It's for a uni project where they specifically want the temperature change to be predicted in the equation"

It seems to me that this equation does not (a) estimate the actual temperature (b) estimate the temperature delta since the last reading (c) estimate the rate of change of the temperature (d) make a useful prediction of the temperature.

It doesn't seem to do any of those things.

Just because "they" want something, doesn't make it right.