#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Required delay before function execution!!!


    Hello,

    Please refer the function mentioned below.
    Code:
    void ringAlarm(int8* ai)
    {   
       int i,j;
       static int16 time_delay=1000;
       time_delay=time_delay-1;
    
     for(i=0;i<8;i++)
     {     
     
       if(ai[i]==1 && time_delay==0)
       {
                  
              switch(i)
              {
                case 0:
                   output_high(pin_A0);
                   break;
                case 1:
                   output_high(pin_A1);
                   break;
                case 2:
                   output_high(pin_A2);
                   break;
                case 3:
                   output_high(pin_A3);
                   break;
                case 4:
                   output_high(pin_A4);
                   break;
                case 5:
                   output_high(pin_A5);
                   break;
                case 6:
                   output_high(pin_E0);
                   break;
                case 7:
                   output_high(pin_E1);
                   break;
               }//switch ends
            
        }// if ends
    }//for ends
    
    }
    a[i] gives alarm conditions.

    My requirement is
    1. Check a[i]==1 i.e checking condition
    2. Wait for some delay
    3. Again check for a[i]
    4. And if a[i]==1 after delay then only execute the function

    But i can not use delay function which halts execution of next code.

    As mentioned i have used a variable and it decrements every time when it enters into function.

    But with the above mentioned code i am not getting expected result.

    Please suggest where i have to re-initialize it's original value once it becomes zero?

    thanks,
    paddy
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Post your code between [code][/code] tags, to preserve indentation and readability.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Ohhh my mistake...

    I am sorry!!!!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Perhaps you need a threaded application?
    one thread can sleep or usleep as needed while the other thread continues the important application.
    This tutorial was so clear that even I was able to write a threaded program. Actually my program doesn't work so well on linux. A histogram of the interval between my gui updates revealed a bimodal distribution of 40 and 200 milliseconds. I'll need to install the real time kernel (not really an option for a general purpose program) or find some other solution.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Thanks for support.

    I forgot to tell that i am using windows os.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    So how about
    Code:
       time_delay=time_delay-1;
       if ( time_delay > 0 ) return;
       // time_delay is now 0, so reset it
       time_delay = 1000;
    
       // now do the rest
      for(i=0;i<8;i++)
      {     
         if(ai[i]==1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    So how about
    Code:
       time_delay=time_delay-1;
       if ( time_delay > 0 ) return;
       // time_delay is now 0, so reset it
       time_delay = 1000;
    
       // now do the rest
      for(i=0;i<8;i++)
      {     
         if(ai[i]==1)
    Okay. I will check with this.

    thanks

IMN logo majestic logo threadwatch logo seochat tools logo