The Arduino micro-controller is a powerful little wonder. We already know it can be programmed using the Arduino IDE, but wouldn’t it be cool if we could interact with it strictly using Flash ? This article shows you how it can be done.
Once everything is set up, what can be achieved by the Flash/Arduino combo is pretty amazing. For instance, you can configure the board, read sensor inputs or trigger external equipments directly from the comfort of your beloved ActionScript 3 code. While this is all fine and dandy, getting there is a little more complicated than the ol’ plug-and-play. Hang on, it might be a bumpy ride.
If you are a Mac OS X user, Kristofer Joseph posted a similar tutorial targeted at Mac OS X.6 on his site. Check out hist post called Arduino and Flash on Snow Leopard.
Update : while the procedure described on this page work fine, a new option is now available for Adobe AIR users. It involves the use of an ANE (Air Native Extension) that talks directly to the serial port freeing you from using a serial-to-tcp bridge. You can learn more about it on the project’s page.
0. The Arduino Board
Since you are reading this article, I am assuming you already own an Arduino board. If not, you can buy one online at many electronics and hobbies shops. I bought my Arduino Diecimila (below) at RobotShop.ca. I am told NKC Electronics is also a good spot for Arduino-related paraphernalia.
1. The Arduino IDE Software
First of all, if you haven’t already done so, get the Arduino IDE software. The Arduino IDE is a Java application that allows you to write and upload code to the board through its USB (or serial) interface.
By the way, the procedures outlined in this article have been tested to work on Windows XP – on a MacBook Pro . If you are using Mac OS X, Linux or some other version of Windows, your mileage may vary.
Once downloaded, unzip the file and put the resulting folder in a convenient location. In theory, you should then be able to double-click the
arduino.exe file to launch the IDE. However, this does not always work for me. If you are experiencing a similar problem, you can launch the IDE by double-clicking the
My understanding is that, in some circumstances, the Java environment is not properly registered with the app, thus the need to use the
run.bat file to set the path and other system variables.
2. The FTDI USB-to-Serial Drivers
The Arduino IDE expects a serial connection to the Arduino. Therefore if, like me, you are using the USB-enabled version, you have to install the USB-to-Serial driver from FTDI. The driver is included in the Arduino package in the
drivers\FTDI USB Drivers folder. Upon plugging-in your Arduino, Windows will prompt you with the
New Hardware Found wizard. Simply, direct the wizard to that folder and the installation will take place.
To make sure everything is set up properly, in the Arduino IDE, open the “Blink” sketch from the File menu :
A sketch is simply code that you want the Arduino to run. With the “Blink” sketch open, go to the File menu and select “Upload to I/O Board”. This will upload the “Blink” code to the unit. After a few moments, you should see a LED blinking on the Arduino (Yeah!).
If this does not work for you, make sure you have selected the right board and port from the “Tools” menu.
If it still does not work, you should confirm that your board is indeed detected by your system. In Windows XP, you can do so by going to the “Device Manager” and checking that you do have a COM port labeled “USB Serial Port (COM?)”:
On Mac OS X, you will most likely see two types of devices in the Serial port menu. There will be a terminal device (tty) and a call unix device (cu). To upload your sketch you can use either. However, in the SerialProxy configuration (see later) you need to use the cu device. For example, on my Mac I get both
/dev/tty.usbserial-A60049RW for my Arduino Diecimila.
Once your setup is known to work, you can try playing with other sketches or even write your own. Check out the Arduino language reference if you wish to do so. However, in our case, we will only be using one sketch: Firmata.
3. The Firmata Sketch
Most of you now want to know how to make this thing talk to Flash. The key is the Firmata sketch (which is bundled with the Arduino IDE since version 0012). As stated on the Firmata website: Firmata is a generic protocol for communicating with microcontrollers from software on a host computer.
So, just as we did with the
Blink sketch, load the Firmata sketch in the Arduino IDE and upload it to the board:
Basically, once Firmata is on the board, we will be able to configure and program it directly from Flash using a host code library. If you want to confirm it has been properly loaded on the board, open Arduino’s Serial Monitor. Upon opening, it should print the following to screen “
4. The as3glue ActionScript Library
We now have Firmata installed on the board but we need a matching software library that understands the Firmata protocol. For ActionScript 3, this library is called as3glue. You can download as3glue from Google Code where the project is hosted.
5. The SerialProxy Software Bridge
We’re almost there… But as it is now, there is no way for Flash to talk directly to the USB/serial port. In order to bridge this gap, we need to install the SerialProxy application. What it does is forward serial input and output to and from a network socket connection that Flash can use.
Last time I checked,
serproxy.exe 0.1.4 was bundled with as3glue (in the
applications folder). This is the one you want to use. Do not use
serproxy.exe 0.1.3 or older unless you are looking for trouble. If in doubt, you can download SerialProxy 0.1.4 from my site. Before running it, you need to modify the
serproxy.cfg file to match your system. Most likely, the only change you will have to make is assign the right COM port and matching TCP/IP port. For example, in my case, my Arduino is on COM port 7 and serproxy is listening for connections on TCP/IP port 5331 :
comm_ports=1,2,3,4,5,6,7,8,9 net_port7=5331 comm_baud=57600 comm_databits=8 comm_stopbits=1 comm_parity=none
Make sure to end the
net_port parameter with the matching COM port number. As you may have guessed, this means that you can have SerialProxy talk to multiple Arduinos all at the same time and, if you so desire, accross a network.
Depending on your Firmata version, the baud rate could be 115200 or 57600. You can confirm that by looking at the
standard_firmata.pde code in the Arduino IDE. Simply look for a
setup() routine near the end of the code. Inside it, you should find the following line which will tell you the speed to use :
If your Arduino gets assigned a COM port higher than 9 (I had this problem in one of my classes),
serproxy.exe version 0.1.3 will not be able to access it. As stated above, always use SerialProxy 0.1.4.
You can then launch the
serproxy.exe application. If everything went well, you should see a command prompt window with something like :
Serproxy - (C)1999 Stefano Busti, (2005) David A. Mellis - Waiting for clients
If you use TinkerProxy the output is different but you get the idea. This
serproxy.exe process needs to be running all the time. At least, as long as you want Flash to communicate with your Arduino.
6. Putting it all together
Let’s now check if Flash can indeed talk to the Arduino. In order to do this, you will have to put the as3glue ActionScript library in your
classpath. The easiest way to do this is to grab the
net folder inside the
as3glue/as3/ folder that contains the library and put it right beside the
.fla file in which you plan to use it. Then from the
as3glue/examples/arduino/blink folder, open the
blink.fla file in Flash. Look at the code in the first frame and make sure the port number (typically 5331) matches the one in the
serproxy.cfg file then compile the Flash file. You should see the following in the Flash file :
Waiting for Firmata firmware version (reset your board)... Just reset your board by pressing the reset button and you should see one of the LEDs start to flash. Congratulations you have made it!
Obvisouly, this is only the beginning. When you are ready to do more, start by looking at the as3glue QuickStart Reference page. When you’ve done that, you can find the full as3glue
API documentation (link down) on Bret Forsyth’s website.
If you do not see the blinking LED, the first thing to check is if
serproxy.exe was able to open the configured COM port. To do so, right after compiling the Flash movie, switch to the
serproxy.exe window. If you see
"Failed to open comm port. Connection refused." double-check your
serproxy.cfg settings because they are probably pointing to a wrong COM port. What you should be seeing after compiling the .fla, if everything worked right, is:
"Server thread launched".
A very good troubleshooting tool called as3glue Standalone Monitor has been released by Kasper Kamperman and can be dowloaded on his site (I believe it is not part of the as3glue download package). This tool allows you to monitor Arduino’s inputs and outputs in realtime. If you cannot get your setup to work, try with his
.fla file to identify if the problem is with your environment or with your ActionScript code. You will need to press your board’s reset button after compiling the
Here is a list of additional common pitfalls you should watch out for while trying to interface Flash and Arduino :
- When you transfer the
StandardFirmatasketch, make sure you have the right board selected in Arduino’s Tools->Board menu. For instance, I had a student who mistakenly identified his board as an Arduino Pro w/ ATmega328 while in reality he had a Duemilanove w/ ATmega328. The transfer completed just fine with Arduino giving no hints that something was wrong. The result was that some functions were working fine (like writing to a digital pin) while others did not work a all (like reading from a digital pin or printing to serial).
- Make sure you have the right character encoding for your
serproxy.cfgfile. Be safe and use a programmers editor like Notepad++ (and not Notepad) to edit this file or you may end up with
serproxy.exestarting and accepting connections but not transmitting to Flash properly.
- I also have seen a case where the Flash file seem to have been corrupted. Starting a new one from scratch fixed the problem (I have only seen this once and I’m not 100% convinced that this actually was the problem and not a mere coincidence but I guess it’s worth a try).
- There are security policies that apply if you try to run a swf file directly (as oppposed to compiling it in the Flash IDE). In order for a standalone swf file to talk to SerialProxy you must authorize it by going to Flash Player’s settings manager. You can authorize a single file, a folder or a whole drive. Check out my other article titled Opening a local socket from an SWF file for further information on this topic.
- Of course there are always the four Rs of desperation : Reset, Restart, Reboot and Reinstall… I truly hope you will not end up there!
Hope this helps !