Professional Documents
Culture Documents
ARDUINO 3: INTERRUPTS
Name : Date :
Regd-No :
OBJECTIVES:
▪ Learn what an interrupt is
▪ Program Arduino using hardware interrupts.
SUGGESTED READING:
▪ Lab Lecture about interrupts
▪ http://playground.arduino.cc/Code/Interrupts
▪ http://arduino.cc/en/Reference/attachInterrupt
▪ http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-
interrupts/
▪ http://www.dave-
auld.net/index.php?option=com_content&view=article&id=107:arduino-
interrupts&catid=53:arduino-input-output-basics&Itemid=107
▪ http://www.gammon.com.au/forum/?id=11488
Please read through all the suggested reading before you come to lab.
a specific order, from start to end, and then repeat the process. Such programming is
known as ‘synchronous’ or ‘sequential’, because the controller is following a
specific pattern. Meanwhile it is taking logical decisions based on external (buttons,
sensors) and internal (delays, timers) events that occur during the execution of the
code. As you might already have noticed, the processor gets tied down performing
a specific task, and cannot detect any other change that occurs during that time.
A better way to program Arduino is to use an ‘asynchronous’ programming method
using interrupts. Interrupts are special events that alter the code flow. The processor
pauses whatever task it is doing and executes a special function (known as ISR or
Interrupt Service Routine). After execution of the ISR, the processor resumes
whatever it was doing before the interrupt event was generated (Fig.8.1)
Types of Interrupts:
There are two main types of interrupts:
▪ Hardware interrupts, which occur in response to an external event, such as
an input pin going high or low
▪ Software interrupts, which occur in response to an instruction sent in
software
We will discuss the hardware interrupts for now, that can be programmed using
built-in Arduino IDE functions (wiring library).
Note:
Inside the attached function, delay() won't work and the value returned by
millis() will not increment. Serial data received while in the function may be lost.
You should declare as volatile any variables that you modify within the attached
function. See the section on ISRs below for more information.
ISRs are special kinds of functions that have some unique limitations most other
functions do not have. An ISR cannot have any parameters, and they shouldn't return
anything.
Syntax:
attachInterrupt(interrupt, ISR, mode);
Parameters:
ISR: the ISR to call when the interrupt occurs; this function
must take no parameters and return nothing. This
function is sometimes referred to as an interrupt service
routine
mode: defines when the interrupt should be triggered. Four
constants are predefined as valid values:
• LOW to trigger the interrupt whenever the pin is
low,
• CHANGE to trigger the interrupt whenever the
pin changes value
Returns:
none
Example 4_1:
/*
* Simple code to demonstrate the use of
* interrupts to control the program flow.
*/
void loop ()
{
// loop doing nothing
}
TASK 4_1:
▪ Modify Task 1_1 and Task 1_4 to incorporate interrupts on buttons used.