plumbing-for-the-arduino
plumbing-for-the-arduino
the Arduino
Matthew C. Jadud
Christian L. Jacobsen
Adam T. Sampson
Revision 2011-01-24
2
Contents
1 Getting Started 11
1.1 Goals . . . . . . . . . . . . . . . . . . . . . . . . 11
1.2 Installing the Drivers—or Not . . . . . . . . . . 11
1.2.1 You have an old Arduino . . . . . . . . 12
1.2.2 You use an adapter . . . . . . . . . . . . 13
1.2.3 You have a new Arduino . . . . . . . . 14
1.3 Testing JEdit . . . . . . . . . . . . . . . . . . . . 14
1.3.1 Upload the Firmware . . . . . . . . . . 15
2 One Blinkenlight 18
2.1 Goals . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Code . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1 Open JEdit . . . . . . . . . . . . . . . . . 19
2.2.2 Write your Program . . . . . . . . . . . 20
2.2.3 Build Your Code . . . . . . . . . . . . . 21
2.3 Patterns . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.1 The PROCedure Definition . . . . . . . . 22
2.3.2 A PROCedure Call . . . . . . . . . . . . 24
2.4 Breakage . . . . . . . . . . . . . . . . . . . . . . 25
2.4.1 Programming Strategies . . . . . . . . . 28
2.5 Other Resources . . . . . . . . . . . . . . . . . . 29
3
Contents
3 Speedy Blinkenlight 30
3.1 Goals . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2 Building the Circuit . . . . . . . . . . . . . . . . 30
3.2.1 The Breadboard . . . . . . . . . . . . . . 33
3.2.2 The Arduino . . . . . . . . . . . . . . . . 33
3.2.3 The Resistor . . . . . . . . . . . . . . . . 34
3.2.4 The LED . . . . . . . . . . . . . . . . . . 35
3.2.5 Completing the circuit . . . . . . . . . . 36
3.3 Code . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 Patterns . . . . . . . . . . . . . . . . . . . . . . . 37
3.5 Experimenting with Changes . . . . . . . . . . 39
3.6 Breakage . . . . . . . . . . . . . . . . . . . . . . 39
3.6.1 Break your circuit . . . . . . . . . . . . . 39
3.6.2 Break your program . . . . . . . . . . . 40
4 Two Blinkenlights 42
4.1 Goals . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2 Build the circuit . . . . . . . . . . . . . . . . . . 42
4.3 Code . . . . . . . . . . . . . . . . . . . . . . . . 44
4.4 The PAR pattern . . . . . . . . . . . . . . . . . . 44
4.4.1 The truth about PAR . . . . . . . . . . . 45
4.4.2 Explorations . . . . . . . . . . . . . . . . 47
4.5 Breakage . . . . . . . . . . . . . . . . . . . . . . 48
4
Contents
7 Undressing Toggle 64
7.1 The Circuit . . . . . . . . . . . . . . . . . . . . . 64
7.2 The Network . . . . . . . . . . . . . . . . . . . . 65
7.3 Breaking up is hard to do . . . . . . . . . . . . 65
7.3.1 From pictures to code . . . . . . . . . . 66
7.4 What does toggle do? . . . . . . . . . . . . . . 68
7.5 Pattern: A Pipeline . . . . . . . . . . . . . . . . 70
7.6 Explorations and Breakage . . . . . . . . . . . . 72
8 Buttons Everywhere 74
8.1 The Challenge . . . . . . . . . . . . . . . . . . . 74
8.2 The Circuit . . . . . . . . . . . . . . . . . . . . . 75
8.3 Reusing Procedures . . . . . . . . . . . . . . . . 76
8.4 Managing complexity . . . . . . . . . . . . . . . 78
8.5 The Code . . . . . . . . . . . . . . . . . . . . . . 80
8.6 Breakage . . . . . . . . . . . . . . . . . . . . . . 80
10 Acknowledgements 89
10.1 Software . . . . . . . . . . . . . . . . . . . . . . 89
10.1.1 occam-π and Plumbing . . . . . . . . . 90
10.2 Images . . . . . . . . . . . . . . . . . . . . . . . 91
5
Contents
11 Book Bugs 93
6
Preface
7
Contents
Parallelism Yesterday
8
Contents
Parallelism Today
1 PAR
2 blink (11 , 500)
3 blink (12 , 500)
With the right circuit, this code would tell our Arduino to
blink two LEDs in PARallel, one on pin 11, and one on pin
12. The blink command comes from the Plumbing library of
code, and the PAR comes from occam-π . Combined, the lan-
guage and the library of code make it easy to express ideas
about problems that involve two things happening “at the
same time,” or (as we prefer to say), concurrently.
We assume that you, the reader, have little or no program-
ming experience, but are excited to explore our tools with
your trusty Arduino in hand. Please—enjoy.
9
Contents
The Commons
This text, as well as all of the tools you need to explore it,
are free and open. Our text is made available under a Cre-
ative Commons license, our software under the LGPL, and
we have chosen the Arduino (and its many variants) because
of the open nature of that community as well. We encourage
you to begin exploring parallel programming using Plumb-
ing, occam-π , and your Arduino.
http://creativecommons.org/licenses/by-sa/3.0/us/
Bugs
10
1 Getting Started
1.1 Goals
11
1 Getting Started
12
1 Getting Started
13
1 Getting Started
14
1 Getting Started
project that lets JEdit talk to your Arduino. You can freely
download a version of JEdit from www.concurrency.cc
that has our plug-in pre-configured and ready to go for your
choice of operating system.
15
1 Getting Started
Figure 1.6: Select the correct options and upload the firw-
mare.
First, select which kind of Arduino you have from the drop-
down menu. Then, you need to select your serial port.
16
1 Getting Started
17
2 One Blinkenlight
2.1 Goals
2.2 Code
18
2 One Blinkenlight
19
2 One Blinkenlight
Once you have JEdit open, you can write your first program.
The first step is to get the built-in LED on your Arduino blinking—
this will tell us that everything works.
1 # INCLUDE " plumbing . module "
2
3 PROC main ()
4 heartbeat ()
5 :
Type the above program into JEdit. Note the first line: the
INCLUDE line brings in all of the code that we call “Plumb-
ing.” We won’t always show this line, but you need it at the
start of every one of your programs.
Note that there are some spaces hidden there. Here’s the
same program, but with the spaces clearly marked:
1 # INCLUDE " plumbing . module "
2
3 PROC main ()
4 heartbeat ()
5 :
20
2 One Blinkenlight
the Mac, you can press COMMAND-S, and under Linux and
Windows, CTRL-S. Or, you can click the little floppy disk in
the toolbar. Regardless of how you do it, save your work of-
ten!
Your code is human-readable. (You may not feel that way yet,
but it is.) We need to convert it from something you under-
stand to something your Arduino understands. We would
properly call this compiling your program. Go up to the Plu-
gins menu, go down to Plumbing, and select the Start occPlug
option. You’ll get a new floating window that provides a few
critical tools. First, you need to select “Arduino” from the
drop-down menu (it defaults to “Desktop”).
Once you have the occPlug extension running, you can com-
pile and run your program. When you press the round arrow
on the left, our tools first check to see if your code is grammat-
ically correct, and then transform your code into something
that will run on the Arduino. If you made any mistakes in
typing in your program, this is where you’ll get one or more
seemingly incomprehensible errors. Think of them not as “er-
rors” but instead as “learning opportunities.”
If you code compiled, you’ll see a message that looks some-
21
2 One Blinkenlight
2.3 Patterns
22
2 One Blinkenlight
23
2 One Blinkenlight
24
2 One Blinkenlight
2.4 Breakage
1
You will learn how to write everything we show you in the first ten chap-
ters, so you’ll get to see all of the magic soon enough.
25
2 One Blinkenlight
26
2 One Blinkenlight
Misspellings
One of the most common errors made by beginning pro-
grammers in any language are typos and misspellings.
What happens if you type PRC instead of PROC? maim
instead of main? hearbeat instead of heartbeat?
Capitalization
What happens if you write proc instead of PROC? Like-
wise, Heartbeat instead of heartbeat?
Forget the colon
What happens when you leave the colon off the end of
a PROC definition? This is a common error.
Forget the parens, I
What happens when you leave one or both of the paren-
theses off the PROC definition?
Forget the parens, II
What happens when you leave one or both of the paren-
theses off the PROC call in the body?
Indentation
What happens if you indent the body of the PROC by
one space instead of two? Three spaces?
27
2 One Blinkenlight
Community
We have a website with more information and mailing
lists you can join; take a look at http://concurrency.
cc/. If you get stuck, join the discussion list and ask a
question. We’re there to help.
Attention to Detail
Spaces matter in Plumbing, and they are invisible. Be
careful about what you do, and begin learning to look
with the eyes of a programmer: start looking for pat-
terns and the invisible parts of your code.
Take Notes
As you discover new kinds of mistakes you’ve made,
take the time to make note of them, as well as your anal-
ysis of how you fixed them. Eventually, you won’t need
to make the notes, because you’ll make fewer mistakes.
Take a Walk
...or a roll, or whatever. You should especially do this if
the weather outside is your particular favorite. Let your
mind wander as you wander the outdoors.
Take a Shower
Or do whatever relaxes you and breaks your routine.
Perhaps you prefer a bubble bath? Either way, don’t
forget your rubber duckie.
28
2 One Blinkenlight
29
3 Speedy Blinkenlight
By changing just one line of code, you can control the speed
at which the LED on your Arduino blinks.
3.1 Goals
30
3 Speedy Blinkenlight
31
3 Speedy Blinkenlight
1
See http://oreilly.com/catalog/9780596153755/ for more in-
formation about Make: Electronics.
2
See http://frank.harvard.edu/aoe/ for more information about
The Art of Electronics.
32
3 Speedy Blinkenlight
TH
E Things that line up this
G way on the same side of
UT
TE the gutter are connected.
R
With the Arduino turned off (unplugged from the USB port),
take a wire and connect it from pin 13 to one of the columns in
the breadboard. Perhaps start with the left-most column, and
33
3 Speedy Blinkenlight
3
If you want something better than a guideline, look up Ohm’s Law on
the Wikipedia: http://en.wikipedia.org/wiki/Ohms_law.
4
See http://en.wikipedia.org/wiki/Electronic_color_code
for more information about the color codes on resistors.
34
3 Speedy Blinkenlight
Some people say “the long leg of the LED is the negative
leg.” This is true, but if your LED gets mangled, it becomes
difficult to tell which leg is which. Instead, look at Figure ??
on page ??, and find the “anvil.” The anvil is the larger of the
two bits inside the LED, and it is always the negative side of
the LED, meaning the “post” is always the positive side.5
5
See http://en.wikipedia.org/wiki/Led for more information.
35
3 Speedy Blinkenlight
Plug your LED into the breadboard so that the positive side
is in the same column as your jumper wire from 13, and the
negative side is in a column with one end of your resistor.
36
3 Speedy Blinkenlight
3.3 Code
1 PROC main ()
2 blink (13 , 500)
3 :
Figure 3.1: The blink procedure lets you control how
rapidly an LED blinks and which pin the LED is
connected to.
3.4 Patterns
37
3 Speedy Blinkenlight
38
3 Speedy Blinkenlight
You can experiment with a few things at this point. For exam-
ple, you could connect your LED up to pin 12 instead of 13.
After changing the circuit, you would then need to modify
your code.
1 PROC main ()
2 blink (12 , 500)
3 :
Figure 3.2: Blinking an external LED on pin 12.
3.6 Breakage
39
3 Speedy Blinkenlight
You can safely do each of these things, and see how your
circuit fails to blink properly.
There are quite a few ways you can break the software in this
chapter—even though it is only three lines long!
Wrong pin
If you forget to change the pin number from 13 to 12,
then you’ll blink the wrong LED. Or, for that matter, if
you blink pin 11, nothing will appear to happen at all.
Crazy parameters
Try replacing the number 12 with TWELVE. See what
happens.
40
3 Speedy Blinkenlight
Crazy parameters II
Try replacing the number 12 with 122. See what hap-
pens.
Too many parameters
Try using a parameter list like 13, 12, 500. That is,
your code would look like:
blink (13, 12, 500)
Parameters too big
The parameter for the speed of the LED blink is an integer—
a whole number without any decimal parts, if you pre-
fer. Computers can only keep track of numbers that
are so big (or small). How big can you make the blink
speed?
Blink too fast
What happens if you make the blink speed too small
(e.g. zero)?
Fractional blinking
What happens if you try and blink the LED every 100.5ms?
41
4 Two Blinkenlights
4.1 Goals
42
4 Two Blinkenlights
43
4 Two Blinkenlights
4.3 Code
1 PROC main ()
2 PAR
3 blink (12 , 500)
4 blink (11 , 500)
5 :
Figure 4.1: We can blink in PARallel.
44
4 Two Blinkenlights
another process
:
Figure 4.2: Executing two processes in parallel.
45
4 Two Blinkenlights
1
http://www.transterpreter.org/
46
4 Two Blinkenlights
4.4.2 Explorations
47
4 Two Blinkenlights
4.5 Breakage
There are a lot of neat ways to break the code in this chapter.
Indentation of PAR
What happens if you fail to indent PAR, but indent all
of the blink procedures? We make this mistake in our
code all the time.
Lowercase PAR
What happens if you make par all lowercase? What if
you only capitalize the P?
Indentation of blink
Try indenting each blink process four spaces instead
of two. What happens?
Multiple blinks on one pin
Modify your program so that two of your blink pro-
cesses refer to the same pin number. (Note this breaks
after you upload your program, not before!)
Replace one blink with a heartbeat
Modify your program so the PAR looks like this:
2 PAR
3 heartbeat ()
4 blink (11 , 500)
48
4 Two Blinkenlights
parallel?
49
Waiting for the World
In terms of electronics,
we learned how to con-
nect an LED to our Arduino. In terms of programming, we
learned some of the basics of the syntax of occam-π . Next,
we’re going to learn a bit about waiting and signaling. Like
everything else about Plumbing, we’ll do this in PARallel—
one process will be responsible for signaling that something
has changed (a button is pressed, for example), while another
waits to see what has happened.
50
5 Push the Button
5.1 Goals
For this circuit, we’ll add a button to the Arduino that lets us
turn an LED on and off. To do this, we’ll need to see how to
attach a button to the Arduino.
You will need:
1. Your Arduino
2. A button
51
5 Push the Button
3. A 10kΩ resistor
4. Some jumper wire
5. An LED and a 470Ω resistor
A picture of what you’re going to build (and the circuit di-
agram) can be found in Figure 5.1.
52
5 Push the Button
1
FIXME Wikipedia link
53
5 Push the Button
s
button.press pin.toggle
SIGNAL
14 13, LOW
54
5 Push the Button
1 button . press ()
2 pin . toggle ()
55
5 Push the Button
1 PAR
2 button . press (2)
3 pin . toggle (12 , LOW )
1 CHAN SIGNAL s :
2 PAR
3 button . press (2)
4 pin . toggle (12 , LOW )
56
5 Push the Button
!
end of a channel is marked
with a !. The exclama-
tion point (or bang, from
British English) tells occam-
π which end of the channel
is sending information. It’s
as if that end of the channel sending
is shouting: “Hey! Hey you!
I’ve got a SIGNAL for you!”
?
The listening (or receiv-
ing) end of a channel is
marked with a ?. The
question mark (or eh?, from
Canadian English) tells occam-
π which end of the chan-
nel is receiving information. receiving
It’s as if that end of the of
the channel is waiting for
instructions: “Eh? Come
again? What did you say?”
57
5 Push the Button
1 PROC main ()
2 CHAN SIGNAL s :
3 PAR
4 button . press (2 , s !)
5 pin . toggle (13 , LOW , s ?)
6 :
Figure 5.1: Using a button to toggle an LED on and off.
5.3.2 In summary
58
5 Push the Button
5.4 Breakage
59
6 Tick... tick... tick...
The neat thing about a process network is that you can swap
one process for another if it “speaks the same language.” By
this, we mean that you can swap one process for another if
the input channels and the output channels carry the same
kind (or type) of information.
In this short chapter, we’ll make one change to the code
from Push the Button to demonstrate this.
s
button.press pin.toggle
SIGNAL
14 13, LOW
60
6 Tick... tick... tick...
s s
button.press pin.toggle
SIGNAL SIGNAL
14 13, LOW
Figure 6.2
s
tick
SIGNAL
500
61
6 Tick... tick... tick...
1 PROC main ()
2 CHAN SIGNAL s :
3 PAR
4 button . press (2 , s !)
5 pin . toggle (13 , LOW , s ?)
6 :
Figure 6.1: Our original program uses button.press.
1 PROC main ()
2 CHAN SIGNAL s :
3 PAR
4 tick (500 , s !)
5 pin . toggle (13 , LOW , s ?)
6 :
Figure 6.2: One substitution changes the program.
62
6 Tick... tick... tick...
63
7 Undressing Toggle
64
7 Undressing Toggle
s
button.press pin.toggle
SIGNAL
14 13, LOW
s v
button.press toggle pin.level
SIGNAL LEVEL
14 LOW 13
65
7 Undressing Toggle
1 PAR
2 button . press ()
3 toggle ()
4 pin . level ()
1 CHAN SIGNAL s :
2 CHAN LEVEL v :
3 PAR
4 button . press ()
5 toggle ()
6 pin . level ()
66
7 Undressing Toggle
1 CHAN SIGNAL s :
2 CHAN LEVEL v :
3 PAR
4 button . press (2 , s !)
5 toggle ( LOW , s ? , v !)
6 pin . level (12 , LOW , v ?)
1 PROC main ()
2 CHAN SIGNAL s :
3 CHAN LEVEL v :
4 PAR
5 button . press (2 , s !)
6 toggle ( LOW , s ? , v !)
7 pin . level (12 , v ?)
8 :
67
7 Undressing Toggle
s v
button.press toggle pin.level
SIGNAL LEVEL
14 LOW 13
Figure 7.2: Toggle has one channel in, and one channel out.
68
7 Undressing Toggle
69
7 Undressing Toggle
70
7 Undressing Toggle
2
See http://en.wikipedia.org/wiki/Pipeline_(computing)
for more information about pipelines in computing.
71
7 Undressing Toggle
72
7 Undressing Toggle
73
8 Buttons Everywhere
74
8 Buttons Everywhere
s
button.press pin.toggle
SIGNAL
14 13, LOW
75
8 Buttons Everywhere
1 PROC main ()
2 CHAN SIGNAL s :
3 PAR
4 button . press (2 , s !)
5 pin . toggle (13 , LOW , s ?)
6 :
76
8 Buttons Everywhere
s1
button.press pin.toggle
SIGNAL
2 12, LOW
s2
button.press pin.toggle
SIGNAL
3 6, LOW
1 PROC main ()
2 CHAN SIGNAL s1 , s2 :
3 PAR
4 button . press (2 , s1 !)
5 pin . toggle (13 , LOW , s1 ?)
6 button . press (3 , s2 !)
7 pin . toggle (6 , LOW , s2 ?)
8
9 :
77
8 Buttons Everywhere
78
8 Buttons Everywhere
1 PROC main ()
2 PAR
3 b2p (2 , 12)
4 b2p (3 , 6)
5 :
If we were to “peel back” both of these PROCedures, we’d
end up right back where we started: with two copies of the
button.press PROC and two copies of pin.toggle, each
connected by their own channel. This way, we’ve simplified
our program, and we can now reuse b2p, our new PROC, as
part of other process networks.
79
8 Buttons Everywhere
8.6 Breakage
80
8 Buttons Everywhere
What happens if you try and use b2p twice, but you use
the same constants both times?
Mixup pins
What happens if you mix up button.pin and led.pin
in the PROCb2p?
Switch the order
Does anything break if you put one b2p before the other
in the PAR?
Forget a comma
There are several place that we’ve used commas. What
happens if you leave one out?
Use a channel twice
If you go back to the code from the middle of the chap-
ter, what happens if you use s1? more than once? What
happens if you use s2! more than once?
Fail to use a channel
What happens if you declare a channel but never use it?
Add
81
9 Making things Move:
Servos
9.1 Goals
82
9 Making things Move: Servos
One of the fun things about this circuit is that we don’t ac-
tually need the bread-board. We can plug our servo right
into the Arduino pins and run straight off the board. All you
will need are: 1 Your Arduino 2 Your Servo [[CIRCUT DIA-
GRAM]]
You servo might not look much like the one in the diagram
above, and perhaps your ground, power and control wires
aren’t in the same order. That’s perfectly fine. What’s crucial
is making sure your ground wire is the one plugged into the
ground pin, your power is plugged into the 5v pin, and that
your control is plugged into one of the correct PWM pins. If
this is not done, bad things could very well happen to both
your board and your servo. And that would be bad. Measure
twice, run your servo.. more than once, actually, is the goal.
The fact that we’re plugging the control wire into pin 11 is
more important than you might think. As discussed in previ-
ous a chapter[s], PWM pins are connected to Counters in the
Arduino’s processor. Some of these counters are 8 bit (and
can thus only count up to 255 – the maximum value of a sin-
gle BYTE), and – on the Arduino – only one of these is a 16 bit
counter (meaning it can count well past 60,000). For servos to
operate properly, we need the control wire to be plugged into
83
9 Making things Move: Servos
9.3 Code
1 PROC main ()
2 CHAN BYTE pos . chan :
3 PAR
4 simple . servo (11 , pos . chan ?)
5 pos . chan ! 45
6 :
84
9 Making things Move: Servos
85
9 Making things Move: Servos
86
9 Making things Move: Servos
87
9 Making things Move: Servos
88
10 Acknowledgements
10.1 Software
Plumbing for the Arduino was typeset using LATEX. Writing was
carried out in either TextMate by Allan Odgaard or vi. Di-
agrams were produced using OmniGraffle Pro by the Omni
Group, and screen captures were made using Snapz Pro by
Ambrosia Software.
The completely awesome circuit diagrams were made using
Fritzing, an open source project that lets complete noobs de-
sign circuits visually, then see the same circuit as a schematic,
and finally export that circuit as a PCB for etching by hand or
automated manufacture.
89
10 Acknowledgements
90
10 Acknowledgements
10.2 Images
91
10 Acknowledgements
http://arduino.cc/en/uploads/Guide/FTDIChip.png
The Arduino Pro without an FTDI chip on page 13 was made
available by David Mellis under a CC-BY 2.0 license (20110115):
http://www.flickr.com/photos/mellis/4784333335/
The FTDI Friend on page 14 was made available by Lady Ada
under a CC-BY license (20110115):
http://www.flickr.com/photos/ladyada/4987941401/
92
11 Book Bugs
Our text is not perfect. If you find errors in the text, please pass
them on to bookbugs at concurrency dot cc. We would like
to acknowledge your help here.
93