ATLAS 2013: Super Fast Intro To Arduino and Processing
ATLAS 2013: Super Fast Intro To Arduino and Processing
For Mac:
Double-click the .dmg file and drag the Processing icon from inside this file to your applications folder,
or any other location on your computer. Double click the Processing icon to start Processing.
For Windows:
Double-click the .zip file and drag the folder inside labeled Processing to a location on your hard drive.
Double click the Processing icon to start Processing.
If you are stuck go to http://wiki.processing.org/index.php/Troubleshooting for help.
Anatomy of a sketch
A sketch is a file or
project you create
in Processing.
When you first
open up a new
sketch it will be
completely blank.
Setup()
This function runs once, at the very beginning of your
sketch. You will use setup to set up certain aspects of
your sketch, makes sense right?
Most importantly for this class you will begin Serial
communication in the setup function. The setup function
without anything in it looks like this:
Draw()
This function is where everything happens in your
sketch. The draw loop is the portion of code that keeps
repeating while the Processing sketch is open. The draw
loop looks like this:
Basic Graphics we will be using
Our potentiometer is
connected to analog pin 0.
The establishContact()
function keeps the
Arduino sketch from
exiting the setup function
until it hears from
Processing and
establishes contact.
Hooking into
Processing
Heres the establishContact() function. It continually
sends Hello out on the transmit serial pin. This travels to
the computer. While the computer has not responded there
is no data in the Serial buffer so Arduino just keeps sending
Hello. Once the Arduino hears anything from the
computer it exits establishContact() as well as the
setup function and enters the Arduino draw function.
Hooking into
Processing
In our loop() function,
we need to read from all
the sensors and send
the values out the serial
port by calling
Serial.write
and
Serial.print
Well talk about the
difference between these
two commands as well as
the start byte, delimiter
and the end byte next.
Hooking into
Processing
The difference
between
Serial.write
and
Serial.print
is that write is
used to transmit
byte type variables
and print is used
to transmit all other
variable types.
Hooking into
Processing
Here are the rest of the loop() statements. Basically we
are just sending more data about the other sensors
plugged into the Arduino.
Then we send an end byte with Serial.write.
Finally we send an empty println() command so
Processing receives a \n and knows we have sent all the
sensors values and will be starting over from the beginning.
Receiving Data in
Processing
Were sending data from the Arduino but import processing.serial.*;
we need a way to receive it in Processing.
First, import the serial library. Serial usbPort;
Well also need a Serial object to define int [ ] sensors = null;
which serial port well be using, as well as boolean firstContact = false;
an integer array for our sensor data. We
also have a boolean variable to keep track
of if weve heard from Arduino or not. void setup() {
In your setup function, you need to initialize
your Serial object, passing it the parent usbPort = new Serial (this,
object (dont worry about this) which port Serial.list( ) [0], 9600);
you want to use, and the baud rate.
Make sure you pick the same baud rate usbPort.bufferUntil (\n);
that you defined in the Arduino sketch.
The bufferUntil function just stores our }
incoming data in a buffer until were ready
to do something with it.
Receiving Data in Processing
Our next step is to define a SerialEvent function this
function automatically gets called every time a character in
our bufferUntil() statement is read from the serial
port.
We then read a chunk of data into a String, trim
whitespace, and split it using our delimiter character into
our sensors[ ] integer array. This puts each sensor
value into its own addressable place in the array.
There is a println that should be printing out the sensors
values it sees try running the sketch to make sure youre
getting values. If youre getting errors trying unplugging
and plugging your Arduino back in.
The code is on the next slide.
Receiving Data in Processing
Receiving Data in Processing
Theres a lot going on here, so dont worry if it doesnt
make sense at first.
Basically, we check for Serial Communication from
Arduino. Once it reads a carriage return it checks to make
sure the data string is not empty.
Then we print out the string to the console in Processing
so we can see what data we are receiving.
Receiving Data in Processing
Next there is some code to deal with the eventuality that we have not
made contact with the Arduino yet. If we havent made contact it listens
for a Hello. When it receives this it clears the Serial Buffer with
usbPort.clear(), sets the variable firstContact true and sends
an A to the Arduino. This causes the Arduino to exit the
establishContact() function and start sending data. After all that
we print contact to the Processing console so that we can follow what
is going on and we know weve made contact with the Arduino.
Receiving Data in Processing
If firstContact is true we do the following code which splits the
string up by our delimiter character into an array. This lets us put each
sensor value into its own variable.
Then there is a for loop that prints out the sensor values so we can
see them.
Then we assign the various values from the sensors array to individual
variables. If you dont put the array values into their own variables you
will get a null pointer exception when you try to use them in your code.
Using Received Data in Processing
So weve got all of the data we want placed in variables.
Now we need to use them.
While we may not have time for it- lets talk about how we
could make sure that the circle or square stays in the same
place when we switch between their functions.
http://learn.sparkfun.com/curriculum/16
http://learn.sparkfun.com/curriculum/35
http://learn.sparkfun.com/curriculum/43
http://learn.sparkfun.com/curriculum/45
Questions?
www.sparkfun.com
6175 Longbow Drive, Suite 200
Boulder, Colorado 80301