must be done immediately when an external event occurs.
It is so common, in fact, that most microcontrollers have
built-in interrupt peripheral circuitry to accomplish the task.
This circuitry monitors a pin voltage and when a
certain condition happens such as ‘was high, now low,’ it
generates an interrupt that causes the main program flow
to halt, store what it was doing in memory, and then
the system runs the function that was assigned to the
interrupt. When that function finishes, the system state is
restored and the main program runs again.
You mainly deal with interrupts in one of two ways. If
a simple task is all that is required and the rest of the program
doesn’t need to know about it, then the interrupt service
routine can sneak in, handle it, then sneak back out without
the main code ever knowing. If, however, a complex task
that takes a lot of time away from the main program (yes,
it is all relative) needs to be performed, then the interrupt
routine should set a flag (change the value of a variable
that both it and the main program can see) so that the main
program can check that flag as part of the loop() and deal
with the consequences of the interrupt when it gets time.
The Arduino library function attachInterrupt(interrupt,
function, mode) simplifies the chore of setting up and using
an external interrupt. The ‘interrupt’ parameter is either 0 or
1 (for the Arduino digital pin 2 and 3, respectively). The
‘function’ parameter is the name of the function you want
to call when the interrupt occurs (the interrupt service
routine). The ‘mode’ parameter is one of four constants to
tell the interrupt when it should be triggered:
LOW: Trigger when the pin is low.
CHANGE: Trigger when the pin changes value.
RISING: Trigger when the pin rises from low to high.
FALLING: Trigger when the pin falls from high to low.
Hopefully, you still have your IR detector set up on the
breadboard from before (see Workshop 15; Figures 7, 8, and
9). All you need to do in the hardware is move the signal wire
from the Arduino Analog pin 0 to the Digital pin 2 as shown
in Figure 2, then run the Edge_Detect_Interrupt software. In the
former setup, we used the ADC to measure an analog voltage,
but this time all we will sense is that the voltage is high enough to
represent a digital ON or low enough to represent a digital OFF.
Run the Edge_Detect_Interrupt software and waggle
your finger in front of the sensor to get a count like shown
in Figure 3. Next month, we will use this concept to
detect the passing of black and white stripes on a motor
■ FIGURE 3. Edge Detect Interrupt Counter.
encoder wheel to control the speed of that motor.
Edge Detect Interrupt Software
// Joe Pardue 6/28/09
volatile int count = 0;
// setup the serial port
// say hello
Serial.println(“Edge Detect Interrupt”);
// attach interrupt 0 (pin 2) to the
// edgeDetect function
// run function on falling edge interrupt
// do nothing
// on each interrupt
// increment and show the count
Getting Real With Serial Input
The Arduino IDE provides a simple Serial Monitor (PC
side serial terminal program) and some serial communications
functions that allow you communicate with the Arduino
board. These functions do a good job in sending serial
text from the Arduino to the PC, but (IMHO) not such
a hot job of receiving data from the PC. The Serial.
available() function tells you when there is a character
available on the serial port, and the Serial.read() function
will fetch that character for you. However, the Arduino does
not provide (that I know of) any Arduino-like simplified way
to deal with those characters once you’ve got them.
I contrast this weakness of sorts to a real strength of
the C programming language: The C Standard Libraries contain
a wealth of functions for dealing with data input over a serial
port. I want to emphasize that you can use those C libraries
with the Arduino IDE, but that kind-of defeats the purpose
of Arduino. I touched on this a bit in an earlier article where
I differentiated between TAW (The Arduino Way) and ACW
(A C Way), so my approach will be to show what you can
do with TAW and save the C libraries for later discussion of
ACW. I will present an Arduino program that uses logic that
mimics one of those C library functions — atoi() (ascii to
integer) — that will allow us to input a sequence of numeric
characters using the Arduino Serial Monitor and then convert
those characters to an integer data value from 0 to 65535.
This will be used next month to set the motor speed.
In Workshop 13, we built a Number_Commander that
allowed us to pick a tune to play by entering a numeric