Intro to Arduino & Flex/AS3
By: Sean Moore
http://www.seantheflexguy.com
@seantheflexguy
http://gplus.to/seantheflexguy
Wait, what?
Who is this dude!!??
Adobe Community Professional
Flex Developer Community Champion
Technical author: Adobe, O’Reilly, Flash Magazine
Technical reviewer: O'Reilly, Addison Wesley, Manning
Publications and Pakt Publishing
Flash dev since 1998, Flex/AIR dev since 2006
Currently the Flex Architect for iorad
What is Arduino?
• "Arduino is a tool for making computers that can sense
and control more of the physical world than your
desktop computer. It's an open-source physical
computing platform based on a simple microcontroller
board, and a development environment for writing
software for the board."
• http://www.arduino.cc/en/Guide/Introduction
What is Arduino?
Open source Hardware / Software prototyping platform
Used by artists, designers, hobbyists to create
interactive objects and environments
Uses sensors to capture data: motion, temperature,
light, sound, RFID
Controls things hooked up to it: motors, servos, lights,
LCD, coffee machines, dishwashers, doors, etc.
Why Arduino?
Inexpensive (microcontroller costs $30, sensors range
from a few cents to hundreds of dollars)
Cross-platform (IDE available for Windows, OSX and
Linux)
Simple, clear programming environment (C like syntax,
a lot of pre-built libraries)
Open source and extensible software and hardware
How Is Arduino Used?
Typically a circuit will be designed using various sensors a
breadboard, wires and other peripherals (LCD, servo, etc.)
Once the circuit is designed the Arduino IDE is used to
write programs or sketches for the circuit.
The program is compiled in the IDE and then uploaded to
the Arduino microcontroller.
A very basic example is to hook an LED up to the Arduino
and then write code that makes the LED blink.
Is Arduino Expensive?
Arduino development is very affordable, most circuits
cost less then $100
Arduino UNO costs $30
PIR (motion) sensor $10
TMP36 - Analog Temperature sensor $2
Piezo Buzzer $1.50
Arduino Ethernet shield $45
Graphic ST7565 LCD 128x64 $15.25
Download and Install the Arduino IDE
Used to create code/sketches and upload to Arduino
Free to download/install/use
Open-source
Available for Mac/Windows/Linux
Download and Install the Arduino IDE
Arduino IDE 022 is current rev
Comes with a lot of pre-existing code/examples/libraries
*Potentially issues with IDE version and some libraries
http://www.arduino.cc/en/Main/Software
Connecting Arduino to Your Computer
Arduino typically connects to your computer with USB
The sketches are sent to the Arduino from your
computer from the IDE to the microcontroller
Once the program has been uploaded it stays on the
Arduino (No need to upload it every time)
Need to select your Arduino type in the IDE
Ensure the proper serial port is selected in the IDE
Basic LED Blink Sketch
Required: Arduino and Arduino IDE, add an LED to kick
things up, jumbo LED for another notch!
Demonstrates the basics for all Arduino sketches:
setup/loop/pinMode/digitalWrite
This code comes with the Arduino IDE: File -> Examples
-> Basics -> Blink
Basic LED Blink Sketch
• int ledPin = 13;
• void setup()
• {
• pinMode(ledPin, OUTPUT);
• }
• void loop()
• {
• digitalWrite(ledPin, HIGH);
• delay(1000);
• digitalWrite(ledPin, LOW);
• delay(1000);
• }
Arduino “Pins”
Arduino has digital and analog pins used with different
peripherals and sensors.
The pins are labeled with numbers on the
microcontroller.
Each pin has a mode that can be changed from input
mode to output mode.
Changing a pin from INPUT TO OUTPUT with pinMode
drastically changes the electrical behavior of the pin.
Pins are used to read and write data.
Download as3glue (bundle)
"as3Glue gives Flash, Flex and AIR applications physical
interaction capabilities."
Flex communicates with Arduino through sockets
as3glue is a pre-written library that wraps the socket
communication and simplifies Arduino dev in Flex/AS3
Flex is not required. Arduino can also work with pure
AS3 and Flash projects.
Download as3glue (bundle)
Download zip, extract and copy the com and net
directories from the source directory into your project
Create instance of Arduino object and call
requestFirmwareVersion, register listener.
Once listener fires you're good to go!
http://code.google.com/p/as3glue
serproxy
"multi-threaded proxy program for redirecting network
socket connections to/from serial links, in cases where
the remote end of the serial link doesn't have a TCP/IP
stack (eg an embedded or microcontroller system)"
Configure serproxy.cfg
Comes with the as3glue zip archive
The address of the Arduino needs to be added/updated
in serproxy.cfg (serial_device1)
Run this command from the Terminal to find arduino
port on mac: ls /dev/cu.usb*
Also make sure that comm_baud matches the value in
the StandardFirmata code (usually 57600)
Upload StandardFirmata
Firmata is a generic protocol for communicating with
microcontrollers from software on a host computer.
Is included with the Arduino IDE.
Use it to communicate from Flex/AS3 to the Arduino
(uses sockets).
All you really need to do is compile and upload the
StandardFirmata code from the IDE to the Arduino.
Basic Flex/as3glue test
Once you've configured serproxy and have it running
and communicating with the Arduino you can code and
build circuits with AS3 and/or Flex
Basically create an Arduino object, set up the event
listeners and call the requestFirmwareVersion method
Make sure the port in your AS3 code matches the
net_port1 setting in serproxy.cfg
LED Blink in Flex
Connect the LED to the Arduino. The long wire goes
into pin 13 and the short wire goes into the GND pin.
Call setPinMode( LEDPin, Arduino.OUTPUT );
Call arduino.writeDigitalPin( LEDPin, Arduino.LOW ); to
initially turn the LED off.
Create a Button and wire it to a method to blink the LED
off and on.
Passive Infrared Motion
Sensor in Flex
First build the circuit. I’ve created a Fritzing schematic
you can follow to build the PIR circuit.
Next create a new Flex project and add the as3glue
library to the project.
Create the usual Arduino object, register the listeners,
etc.
Either copy the code provided or use it as a starting
point or model. (Let’s look at the code now!)
Resources: Links
http://www.arduino.cc/
http://adafruit.com/
http://www.sparkfun.com/
http://www.arduino.cc/forum/
http://www.kevinhoyt.org/
http://www.mikechambers.com/blog/
http://blog.classsoftware.com/
http://robotgrrl.com/blog/
Resources: Books
Beginning Arduino
Arduino Cookbook
Practical Arduino
Getting Started with Arduino By Massimo Banzi
Starter Packs
Budget Pack for Arduino UNO ($49.50)
http://www.adafruit.com/products/193
Adafruit ARDX - v1.3 Experimentation Kit for Arduino
http://www.adafruit.com/products/170