Go Down

### Topic: Interrupt funtion with delay() (Read 44663 times)previous topic - next topic

#### Palmer

##### May 01, 2009, 08:28 pm
I just got my Arduino Duemilanove last week and have been toying with it daily with no major confusion until today.

I set up 4 LED's in digital 4-7, a button on digital 3 and a pot on analog 0.
The pot is mapped to 0-16 and the number is output in binary on the LEDs.
The button should do a mini-knight rider effect, but it seems to crash at the call of delay(100); and not do anything until I reset.

Is there some rule against using a delay in an interrupt call?
If so, why does delayMicroseconds() work? (not good for me who needs a longer delay though)

Code: [Select]
`int oldPot = 0;int val;int potVal;int k;void setup(){  DDRD = DDRD | B11110000;  PORTD = PORTD ^ B11110000;  attachInterrupt(1, krider, FALLING);}void loop(){  potVal = map(analogRead(0), 0, 1023, 0, 15);  if(oldPot != potVal){    PORTD = B11110000 & (~potVal << 4);    oldPot = potVal;    val = potVal;  }}void krider(){  for(int i = 0; i < 4; i++){    k = 1 << i;    PORTD = B11110000 & (~k << 4);    delay(100);  }  for(int i = 3; i >= 0; i--){    k = 1 << i;    PORTD = B11110000 & (~k << 4);    delay(100);        }}`
Any other tips and advice would be appreciated too

#1
##### May 01, 2009, 09:00 pm
You don't need that k variable

for (i=1; i<=8; i=(i<<1))
PORTD = B11110000 & i;

Just out of curiosity, why use an interrupt ?

Could switch bounce be part of your problem.  You trigger the interrupt, and then while you're in the delay, a second high signal from the button re-triggers the interrupt ?

#### bohne

#2
##### May 01, 2009, 11:13 pm
Don't use the delay() function in an interrupt routine!
It relies on interrupts itself.

The delayMicroseconds() function in contrast uses a simple wait loop and disables interrupts before execution (and then restores SREG after execution), so you can use it in interrupt routines.

For details have a look here:
http://www.dorkbot.de
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236434254
http://www.luminet.cc

#### Palmer

#3
##### May 02, 2009, 12:03 amLast Edit: May 02, 2009, 12:48 am by Palmer Reason: 1
@Ray: Thanks for the tip
And I used an interrupt because I haven't before and thought it was a simple use for one.
I did have to modify the example you gave me to work because of the pins I'm using and had to keep the not in there as I hooked the LED's to come on when the pins go low.

@bohne: Ahh, that'd be why then!
I saw it said that "interrupts will work as they should." but I'm guessing that meant that while it's being delayed an interrupt will work during the delayed period and not the other way.

I also saw it said that more knowledgeable programmers would avoid the use of delay for anything over 10's of seconds. It'd be nice to know how, just for future reference?

#### srossel

#4
##### Nov 04, 2014, 04:34 pm
Hi guys!

so, how did you fix it ? can you show us what did you do?

Thank you very much!

Go Up