# Checking if a condition is satisfied for a certain amount of time

Hello, I'm trying to build a thermal cycler. I have a halogen lamp to increase and a fan to decrease the temperature. I want to have two set temperatures like 40 *C and 20 *C. First, I want the temperature to rise until 40 *C and check if the temperature is in range of 40 +- 1 *C for lets say 10 seconds. If so, I want the temperature to move to 20 *C. I'm able to get the temperature around 40 *C and stay within range but I couldn't figure out how am I going to check for that 10 seconds. How can I do this? Also, for setting the temperature around 40 C, I just compare the temperature with it and if it is greater than 40C, I turn the fan on a bit. It this a good way and is there something better I could do? Thanks.

How can I do this?

Forget, for a moment, how to get the Arduino to do it. Describe how YOU would do it.

You check the temperature. It is not 40 degrees. So, keep heating.
You check the temperature. It is not 40 degrees. So, keep heating.
You check the temperature. It is not 40 degrees. So, keep heating.
You check the temperature. It is 40 degrees. So, note the time and keep heating.
You check the temperature. It is still 40 degrees, and has been for only a short time. Keep heating.
You check the temperature. It is still 40 degrees, and has been for bit longer time. Keep heating.
You check the temperature. It is still 40 degrees, and has been for longer. Keep heating.
You check the temperature. It is still 40 degrees, and has been for longer. Keep heating.

At some point, it is, and has been, 40 degrees for long enough. So, turn the heat off.

Sorry PaulS, I should have helped him find the answer for himself...

``````if(temperature > 40 && !tempIs40c){
tempIs40c = true;
tempIs40cTime = millis();
}
else
tempIs40c = false;

if(tempIs40c && millis() - tempIs40cTime >= 10000){
//temperature have been at least 40*C for at least 10 seconds
...
}
``````