arduino connect.pdf

(457 KB) Pobierz
Connecting Arduino to Processing
a
learn.sparkfun.com tutorial
Available online at:
http://sfe.io/t69
Contents
Introduction
From Arduino...
...to Processing
From Processing...
...to Arduino
Shaking Hands (Part 1)
Shaking Hands (Part 2)
Tips and Tricks
Resources and Going Further
Introduction
So, you’ve blinked some LEDs with
Arduino,
and maybe you’ve even drawn some pretty pictures
with
Processing
- what’s next? At this point you may be thinking, ‘I wonder if there’s a way to get
Arduino and Processing to communicate to each other?’. Well, guess what - there is! - and this
tutorial is going to show you how.
In this tutorial we will learn:
How to send data from Arduino to Processing over the serial port
How to receive data from Arduino in Processing
How to send data from Processing to Arduino
How to receive data from Processing in Arduino
How to write a serial ‘handshake’ between Arduino and Processing to control data flow
How to make a ‘Pong’ game that uses analog sensors to control the paddles
Before we get started, there are a few things you should be certain you’re familiar with to get the
most out of this tutorial:
What’s an Arduino?
How to use a breadboard
Working with wire
What is serial communication?
Some basic familiarity with
Processing
will be useful, but not strictly necessary.
From Arduino...
Page 1 of 18
Let’s start with the Arduino side of things. We’ll show you the basics of how to set up your Arduino
sketch to send information over serial.
First things first. If you haven’t done so yet,
download and install the Arduino software
for your
operating system.
Here’s a tutorial
if you get stuck.
You’ll also need an Arduino-compatible microcontroller and an appropriate way to connect it to
your computer (an A-to-B USB cable, micro USB, or FTDI breakout). Check
this comparison
guide
if you’re not sure what’s right for you.
Ok. You should by this point have the Arduino software installed, an Arduino board of some kind,
and a cable. Now for some coding! Don’t worry, it’s quite straightforward.
Open up the Arduino software. You should see something like this:
The nice big white space is where we are going to write our code. Click in the white area and type
the following (or copy and paste if you feel lazy):
Page 2 of 18
language:cpp
void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}
This is called our setup method. It’s where we ‘set up’ our program. Here, we’re using it to start
serial communication from the Arduino to our computer at a baud rate of 9600. For now, all you
need to now about baud rate is that (basically) it’s the rate at which we’re sending data to the
computer, and if we’re sending and receiving data at different rates, everything goes all gobbledy-
gook and one side can’t understand the other. This is bad.
After our
setup()
method, we need a method called
loop()
, which is going to repeat over and over as
long as our program is running. For our first example, we’ll just send the string ‘Hello, world!’ over
the serial port, over and over (and over). Type the following in your Arduino sketch, below the code
we already wrote:
language:cpp
void loop()
{
//send 'Hello, world!' over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}
That’s all we need for the Arduino side of our first example. We’re setting up serial communication
from the Arduino and telling it to send data every 100 milliseconds. Your Arduino sketch should
now look something like this:
Page 3 of 18
All that’s left to do is to plug in your Arduino board, select your board type (under Tools -> Board
Type) and your Serial port (under Tools -> Serial Port) and hit the ‘upload’ button to load your code
onto the Arduino.
Now we’re ready to see if we can magically (or through code) detect the ‘Hello, world!’ string we’re
sending from Processing.
...to Processing
Our task now is to find a way to listen in on what our Arduino sketch is sending. Luckily, Processing
comes with a Serial library designed for just this kind of thing! If you don’t have a version of
Processing, make sure you go to
Processing.org
and download the latest version for your operating
system. Once Processing is installed, open it up. You should see something like this:
Page 4 of 18
Looks a lot like Arduino, huh? The Arduino software was actually based in part off of Processing -
that’s the beauty of open-source projects. Once we have an open sketch, our first step is to import
the Serial library. Go to Sketch->Import Library->Serial, as shown below:
Page 5 of 18
Zgłoś jeśli naruszono regulamin