Adding Delay to project

Discussion in 'Embedded Systems and Microcontrollers' started by tryingtolearn, Feb 6, 2011.

  1. tryingtolearn

    Thread Starter New Member

    Apr 4, 2010
    17
    0
    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
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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.
     
  3. TierOne

    New Member

    Feb 5, 2011
    16
    2
    Code ( (Unknown Language)):
    1.  
    2. while(1)
    3. {
    4. temp=check_thermistor();
    5. disp_temp(temp);
    6.  
    7. if(temp==23 && wait_timer)
    8.  relay_on=1;
    9.  
    10. if(temp==27)
    11.  relay_on=0;
    12.  start_timer();
    13.  wait_timer=0;
    14.  
    15. if(timerinterrupt==1 && wait_timer=0)
    16.  wait_timer=check_timer();
    17. }
    18.  
    19.  
     
Loading...