# Call a 'Interrupt driven' subroutine by temperature change

Hi everybody!

I 'm very new to this electronic stuff (i have 25 years experience in different software fields, specially in programming using C,C++,Java,...). Quick to the point: is it possible to do the following?:

i want to connect a temperature sensor (i think it will be a LM35) to the Arduino so that if temperature is changing from the temperature range 10-20 degree Celsius range to the area of 20-30 degree Celsius or to 0-10 degree Celsius a routine should be called.

Suppose we have a current temperature of 25 degr. Celsius (which means it is in the range 20 to 30). If the temperature is rising to 29 nothing should happen. But as soon as the temperature gets 31, a routine should be called (which will -hopefully- do some calibration to the crystal). Suppose temperature is falling again to 29, the same routine should be called (to do again some calibration to the crystal).

I know that i could put everything in an endless loop, but my problem is that i need in parallel (should i say concurrent??) to the above, produce a quadratic pulse at 35kHz which has to be as accurate as possible. How to achieve this?

The first thing i thought about was using interrupts. Is this correct? Is this achievable with interrupts?

My actual problem is this: i need to create a quadratic pulse at 35kHz which should be as accurate as possible in a temperature range from 5 to 85 degrees Celsius. I already wrote a sketch using timer interrupts, uploaded it to the Arduino, heated the Arduino to aprox 50 degree Celsius and the perfect quadratic pulse went away: so i have to somehow calibrate the frequency of the crystal! I assume this is correct. isn't it?!

you need to have a state variable which represents the range it is in. e.g. int state = 1; // 1 = 10-19 2 = 20-29

then you get the following pseudo code;

``````int state = 1;
int newstate = 1;
loop()
{
T = get temperature:
newstate = T/10;
if (newstate  != state)
{
state = newstate;
switch(state)
{
case 1 : doTen(); break;
case 2: do Twenty(); break;
case 3: doThirty(); break;
default: doDefault();
}
}
``````

Problem with this is when you are on the 'border' . as every sensor fluctuates it will be 19.9, 20, 19.9, 19.8, 20.1 etc So you will get many range switches. If this is ok fine, otherwise you have to add some extra code to let the state only change when the temp is more than x readings in the new state.

Maybe these classes are interesting: - http://arduino.cc/playground/Main/RunningAverage - and - http://arduino.cc/playground/Main/RunningMedian -

Hopes this helps,