Arduino Working With Interrupts
by Sebastian95 in Circuits > Arduino
2924 Views, 3 Favorites, 0 Comments
Arduino Working With Interrupts
Hello guys.
Today I will pots a short instructable about using interrupts with Arduino controllers.
This instructable will contain two codes which can be used in any other program and can also help you with writing your codes, so writing can be a lot easier. Let's begin.
Why Interrupts?
What is interrupt?
The interrupts let to you respond to external events, while doing something else.
Working with them can be very easily described. Let's just say for example that we have small project in which there is main program that is working until the user calls interrupt routine. This routine is know as Interrupt Service routine or ISR. These routines are useful for making things happen automatically in microcontroller programs and can help solve timing problems.
If you see the first picture of this instructable you can see that there are two parts of program. First part is the main part of program in which there are six commands in main loop. When program is running, microcontroller goes through those commands and keeps doing them. If ISR is called, then main program stops between commands, for example between command four and command five and starts doing ISR part of program. After finishing all parts of IRS program it continues doing command five of the main program.
Keep in mind that ISR are special kinds of functions that have some unique limitations most other functions do not have. An ISR can't have parameters, and they shouldn't return anything.
Basic rules when using ISR are:
- Keep them short
- Don't use delays
- Don't do Serial prints
- Make variables shared with the main code volatile
Arduino Uno and Interrupts
Today I am going to focus my instructable only on working with Arduino UNO.
Arduino UNO has only 2 pins usable for making interrupt routines. Keep in mind that some other type of Arduino can use more pins for interrupt routines. If you have any other type of Arduino you can check which pins are used for your type of board on the official Arduino site.
Arduino uno, nano, mini and other 328 based boards use only two pins for ISR. Those pins are pin 2 and pin 3. For projects used in this code one pin is enough.
Syntax:
- attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
- attachInterrupt(interrupt, ISR, mode);
- attachInterrupt(pin, ISR, mode);
parameters:
interrupt - number of the interrupt
pin - pin used on arduino board
ISR - function called when ISR is triggered
mode - defines when the interrupt should be triggered. There is four constants used: LOW, CHANGE, RISING and FALLING
Real Life Examples That Can Be Used for Explaining Interrupts
In this part I will try to explain these routines through some real life examples.
Example number 1:
You are working on your new Arduino project. While you are writing your code for project you hear somebody is ringing the doorbell. What are you doing? You are probably stopping your work with project, and then you go to door to see that there is postman waiting for you and he is bringing you some new Arduino parts that you ordered few days ago. After you pick those parts you are going back to your computer and you continue writing your code. So in this example you writing your project is main task, and the postman ringing your doorbell is something that interrupts you in your work and something that needs to be solved.
Example number 2:
You are drawing something on the paper, when suddenly your drop your pencil on the floor. The thing that you need to continue drawing your picture in this case is pick up the pencil. Picking up the pencil is like ISR in our codes. After finishing your ISR you continue drawing your picture.
Enough of these examples let's start making some real Arduino projects and let's have fun
Obtaining of the Parts Used in This Projects
Today I am presenting you two projects. Both of them are very simple to make, and they use only few parts, but they are great for learning about ISR.
Project 1:
- Arduino uno
- Breadboard
- Blue LED
- 220 ohm resistor
- Button(in my case I use rotary encoder)
- Few jumpers
Project 2:
- Arduino uno
- Breadboard
- Blue LED
- Red LED
- 2 x 220 ohm resistors
- Button(I used rotary encoder this time too)
- LCD 1602 green display with I2C(you can also use any other display)
- Few jumpers
- Buzzer(if you want it, it is not necessary)
Project 1
This is pretty simple project to make and it is very understandable.
The main thing in this one is changing the state of diode used. If IRS is called the diode turns off or it turns on, it depends of the previous state if was in. As you can see in the loop part of the code there is basically just few lines of code, some of them can even be erased. The main focus is on part in which is interrupt is called. Pay more attention on the setup part of this project.
Downloads
Project 2
This is a little bit more complex project then the previous one, but it is also easy to make and I think that this one is better for understanding interrupts.
So in this code basically there are two FOR loops. One uses int i, and the other one uses int j. Int i loop is written on the top side of LCD, and j is written on the bottom side of the LCD. Main program just shows the changing number of i and j on the LCD until ISR is called. Blue led is working while main program is on, and if ISR routine is called blue diode goes off, and red diode starts blinking for few time. When ISR is called you can see on LCD that numbers that presents values of i and j are staying frozen until ISR is finnished.
You can even add buzzer to pin 10 to work like a door bell. The code for working with buzzer is in attachment.
Video:
Thank you guys for watching this instructable. I hope it was useful.