Tip:
Highlight text to annotate it
X
If you think using interrupts on your Arduino
is too difficult then think again. In this video I'm going to show you how easy
it can be to use
interrupt service routines. Find full details
of this reentrant interrupt example for the Arduino at HomeDIYElectronics.com
The Arduino Uno has 3 hardware timers that you can use to run
tasks at precise intervals. If you call an interrupt service routine
when the time triggers you can achieve accurate timing
irrespective of what the other code in your sketch or program is doing
I'm going to demonstrate the use of interrupts by using them to trigger
two LED's from within the service routine.
LED d2 is toggled on each interrupt and LED d1 is toggled
after a number of interrupts are counted
When d1 is toggled the service routine executes a long
task to demonstrate the effect that this has on the behavior of your circuit
Here's a pseudocode version of the sketch I used. As you can see
it is very simple and isn't that different
to the real code that I used. This is what happens when you load and run the sketch on your Arduino
d2 flashes quickly when toggled
by the interrupt. d1 flashes slower but notice that d2
pauses each time d1 changes state this is because the time-consuming
task takes longer than the time between the timer interrupts.
Interrupts are disabled as soon as you go into an interrupt service routine
so d2 has to wait. If you want to schedule tasks to run
at precise intervals then this isn't going to work because the long routine
holds up the timing so what if you could enable interrupts while the long
routine is running
and allow it to be interrupted by the fast interrupt? Well you can
it's called reentry interrupt service routine
and it's very easy to code see the additions to the pseudocode in red
all you have to do is enable interrupts while it's in the long routine
and disable them again afterwards. Provided
that the long task is not allowed to interrupt itself then all is well
this is what happens when you make the interrupt service routine
reentrant. Now the fast LED d2 is flashing at
regular intervals without any pause. The long routine is no longer holding it up
Get the full details of this Arduino reentrant interrupt example
at HomeDIYElectronics.com
There's lots of other stuff
at HomeDIYElectronics.com too. Go and have a look