I'm trying out interrupts for the first time. I've read about them . What I want to use them for is run a program that displays information on a GLCD while using the delay function. I want the interrupt to activate on a button press perform some function and then go back to the beginning of void loop(). I know Interrupts stop the delay function fro working so i 'm using delayMicroseconds function. My problem with the code below is that the program doesn't respond to every button push and when it does respond led 13 switches back state immediately. I am hardware debouncing the switch.
Main questions
1: I have is why is the Led 13 switching back immediately sometimes.
2: Why does it not respond at all sometimes
3: How to go to the beginning of void loop from the interrupt function.
int pin = 13;
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, RISING);
}
void loop()
{
digitalWrite(pin, state);
delayMicroseconds(10000);
}
void blink()
{
state = !state;
}
An interrupt only interferes with timing while the interrupt is active. Your interrupt routine is commendably short so it won't have any effect on timing.
If you press the button while delayMicroseconds() is working the interrupt will trigger but the code will go straight back and complete delayMicroseconds() so the fact that the button was pressed won't be noticed.
If the purpose of the code is just to switch the LED on and off get rid of the delay altogether and see what happens.
I assume this is just code to learn about interrupts. They are not really necessary for something like this.
Theyreis a bug when calling delayMicroseconds with large values, depending on the clock speed, so
be careful passing values as large as 10,000
You must not call delay() in an interrupt routine, that's the issue, because delay() waits for
other interrupts to happen and interrupt handling is globally disabled during an ISR.
You can call digitalWrite() directly in an interrupt routine.
Thanks for the reply. I tried using the delay function inside the void loop not the interrupt and the program didn't run. Does that mean if i use interrupts that I can't use the delay function at all anywhere in my code? When a ISR finishes does it return to the function that was interrupted and repeats it or does it skip over it?
Yigiter007:
When a ISR finishes does it return to the function that was interrupted and repeats it or does it skip over it?
As I said in Reply #1 when an ISR finishes the Arduino goes back to where it was when it was interrupted - which might be halfway through a multiplication or part way through a delay().
It does NOT restart the function that it was in when the interrupt happened nor does it skip over anything.
There is no prohibition on using delay() in code with interrupts as long as you are happy with the consequences. There are probably other good reasons for avoiding the use of delay() anyway as it prevents the Arduino from doing anything useful during the waiting time.
Did you try your example without any delay() code?
This bit of code is fairly pointless, but if you really did want to have a fairly long delay, but then return to the start of loop() when an interrupt event occured, this is one way you could do it.
In my opinion don';t try and fool it to go back after an interrupt to anywhere other than were it was interrupted.
Theres something called the stack frame created at interrupt, if you fiddle about this will be corrupted.. It is possible to manually play with stack frames but... veRy aDvAnCED STUFF>>