Adding Delay to project

Thread Starter

tryingtolearn

Joined Apr 4, 2010
17
How would I go about adding a delay to just one part of my project instead of the whole code. I know how to code in a delay, just not specific to a certain part. I have a relay activating on say 23c and deactivates 27c.
After the relay activates, once it deactivates, how do I add a delay to just reactivating the relay, but the sensor still continue to pull temp and display on the screen? Say it deactivates, wait x amount of time before allowing it to reactivate while still performing all other functions.
Thanks
 

John P

Joined Oct 14, 2008
2,068
Delay routines provided as high-level instructions to the compiler are of the devil. Don't use them. Set up a periodic interrupt running at (say) a few kilohertz. Any time you need a delay, set a counter and count it down in the interrupt, then set a flag saying "Perform the action now" when it reaches zero. Meanwhile your main() routine is running in a loop, doing the sensing and display stuff that's required and also checking the flag, ready to perform the action when the flag is found to be set.
 

TierOne

Joined Feb 5, 2011
16
Rich (BB code):
while(1)
{
temp=check_thermistor();
disp_temp(temp);
 
if(temp==23 && wait_timer)
 relay_on=1;
 
if(temp==27)
 relay_on=0;
 start_timer();
 wait_timer=0;
 
if(timerinterrupt==1 && wait_timer=0)
 wait_timer=check_timer();
}
 
Top