Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Bugs >> Website,  Reference,  Examples Bugs >> why the delay can't be used in interrupt?
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1216104518

Message started by zouguokanguo on 07/15/08 at 06:48:38

Title: why the delay can't be used in interrupt?
Post by zouguokanguo on 07/15/08 at 06:48:38
Why the delay function can not be used in interrupt?

Call a function which includes a delay by attachInterrupt but doesn't work properly, why?

Title: Re: why the delay can't be used in interrupt?
Post by Vaccumdiode2 on 07/15/08 at 15:10:02
zouguokanguo,

Could you leae post your code.  I will make it easier for some to help you.

Thank you,
Vaccumdiode


Title: Re: why the delay can't be used in interrupt?
Post by Alan_Kilian on 07/15/08 at 20:51:54
There's a timer generating interrupts which increments a "clock" variable.

The delay() routine waits until that clock variable gets to time+delay.

Interrupts are "disabled" when you are all ready inside an interrupt handler.

So, when you call delay() the clock variable never increments, and you
never get back out of the delay() routine.

Title: Re: why the delay can't be used in interrupt?
Post by zouguokanguo on 07/16/08 at 05:14:44
thank you very much for your help

my code is simply like this:

void setup()
{
   pinMode(1, INPUT);
   attachInterrupt(1, flash, RISING);
   pinMode(2, OUTPUT)
}

void loop()
{}

void flash()
{
   digitalWrite(2, HIGH);
   delay(1000);
   digitalWrite(2, LOW);
}

I basically want the LED which connects to pin 2 to blink when the pin 1 get from low to high.

Is there an alternative to use the timer or delay function (or something similar)during the interrupt ?

Thanks again for your help

Title: Re: why the delay can't be used in interrupt?
Post by roypardi on 07/17/08 at 15:33:12
Set a variable == 1 in the interrupt routine and then in your loop check its value- if it has changed then do your delay, and  set the variable back to 0 to get ready for the next interrupt

Title: Re: why the delay can't be used in interrupt?
Post by zouguokanguo on 07/18/08 at 08:04:06
Thank you very much

I will try tomorrow

and I will appreciate if you can further provide a sample code.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.