<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Chambers &#187; General</title>
	<atom:link href="http://www.mikechambers.com/blog/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog</link>
	<description>code = joy</description>
	<lastBuildDate>Thu, 12 Aug 2010 17:31:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Accelerate : Flash / Arduino Based Speed Detector</title>
		<link>http://www.mikechambers.com/blog/2010/08/11/accelerate-flash-arduino-based-speedometer/</link>
		<comments>http://www.mikechambers.com/blog/2010/08/11/accelerate-flash-arduino-based-speedometer/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 05:06:41 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2208</guid>
		<description><![CDATA[Well, as you may have noticed from my blog posts the past week or two, I have been playing around with Flash and Arduino. I have a ton of ideas and projects in mind, but wanted to start on something that wouldn&#8217;t be too overwhelming, and would give me a good opportunity to learn the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F11%2Faccelerate-flash-arduino-based-speedometer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F11%2Faccelerate-flash-arduino-based-speedometer%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Well, as you may have noticed from my <a href="http://www.mikechambers.com/blog/tag/arduino/">blog posts the past week or two</a>, I have been playing around with Flash and <a href="http://www.arduino.cc">Arduino</a>. I have a ton of ideas and projects in mind, but wanted to start on something that wouldn&#8217;t be too overwhelming, and would give me a good opportunity to learn the basics. Well, I have finished my first project, and wanted to share it with everyone. It is a Flash / Arduino based speed detector with clients for Mac, Windows and Android based devices (via <a href="https://prerelease.adobe.com/callout/default.html?callid={AEF64EB4-A977-4317-909A-14AD8014BA21}">Adobe AIR 2.5 beta</a>).</p>
<p>I have put together a quick video that shows the app in action:<br />
<span id="more-2208"></span><br />
<object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=14053460&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=14053460&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object>
<p>View HD version <a href="http://vimeo.com/14053460?hd=1">here</a>.</p>
<p>Here is how it works. I have an Arduino Duemilanove with ATMega328 which has two photo-resistors connected (with a 10k pull down resistor). I set up two laser pointers to shine a laser directly onto the photo-resistor (which is enclosed within a dark box). The Arduino monitors the values returned from the light sensor, and watches for any changes that indicate that the laser bean has been broken. When both laser beams are broken, the Arduino calculates the amount of time between when each sensor was tripped. It then sends that value to the Adobe AIR based client, which is connected to the Arduino via USB / Serial port and a serial port proxy (in the case, TinkerProxy).</p>
<p><a href="http://www.flickr.com/photos/mikechambers/4884468886/" title="Untitled by mike.chambers, on Flickr"><img src="http://farm5.static.flickr.com/4143/4884468886_c93a9daae1.jpg" width="500" height="291" alt="" /></a></p>
<p>You can download <a href="http://github.com/mikechambers/Accelerate/tree/master/src/arduino/eagle/">the schematic from the project&#8217;s repository</a>.</p>
<p>Once the AIR application (running on the desktop, or an Android based device) gets the message from the Arduino that both sensors have been tripped, the AIR app takes the time that it took for each sensor to be tripped, and combines it with the distance between the sensors (in inches), and uses that data to determine the speed.</p>
<p><a href="http://www.flickr.com/photos/mikechambers/4884434876/" title="Accelerate Screenshot by mike.chambers, on Flickr"><img src="http://farm5.static.flickr.com/4134/4884434876_ae2fa2dc45.jpg" width="500" height="306" alt="Accelerate Screenshot" /></a></p>
<p>While I put this together mostly for fun, it is actually useful for tweaking RC car gears and setup. You can use it to help tweak both maximum speed, as well as acceleration / starting speed. Unfortunately, I dont know enough about working on RC cars yet to actually take advantage of the data, but it is fun to hack around with regardless.</p>
<p>I have <a href="http://github.com/mikechambers/Accelerate">posted all of the Flash and Arduino code on GitHub</a>, and released it under an open source <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>. The AIR application is built using Flex 4, and although Flex 4 is not optimized for mobile, it performs really well on my Nexus One.</p>
<p>When looking at the Arduino code, keep in mind this is my first Arduino project, and the code could use some optimization, refactoring. (post in the comments if you have any suggestions).</p>
<p>I have posted some of the lessons learned from the projects in <a href="http://www.mikechambers.com/blog/2010/08/06/flash-arduino-tips-tricks-and-gotchas/">another blog post</a> , so make sure to check that out.</p>
<p>Big thanks to Ben Griffith, Geoffrey Mattie who made some suggestions to clean up the design and UI of the app.</p>
<ul>
<li><a href="http://vimeo.com/14053460">View video of Accelerate project.</a></li>
<li><a href="http://github.com/mikechambers/Accelerate">Code Repository for all code.</a></li>
<li><a href="http://www.flickr.com/photos/mikechambers/sets/72157624582669047/">Images of Project.</a></li>
</ul>
<p>If you have any questions or suggestions about the project, just post them in the comments.</p>
<p>Update: Changed title since it is really more of a speed detector than speedometer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/08/11/accelerate-flash-arduino-based-speedometer/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Flash / Arduino Tips, Tricks and Gotchas</title>
		<link>http://www.mikechambers.com/blog/2010/08/06/flash-arduino-tips-tricks-and-gotchas/</link>
		<comments>http://www.mikechambers.com/blog/2010/08/06/flash-arduino-tips-tricks-and-gotchas/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 18:19:07 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[physical computing]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2191</guid>
		<description><![CDATA[This is a ongoing post where I will post tips, tricks and gotchas that I learn while developing with Flash and the Arduino. I will update this from time to time with more information. &#160; Make sure you are using the correct BAUD Rate If your Flash / Arduino communication does not seem to be [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F06%2Fflash-arduino-tips-tricks-and-gotchas%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F06%2Fflash-arduino-tips-tricks-and-gotchas%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>This is a ongoing post where I will post tips, tricks and gotchas that I learn while developing with Flash and the Arduino. I will update this from time to time with more information.<span id="more-2191"></span></p>
<p>&nbsp;</p>
<h3>Make sure you are using the correct BAUD Rate</h3>
<p>If your Flash / Arduino communication does not seem to be working, then make sure that you have set the BAUD rate to the same rate in both your Arduino code, and the configuration file for your Serial Proxy (such as TinkerProxy).</p>
<p>&nbsp;</p>
<h3>Slow down the BAUD Rate</h3>
<p>If you are getting weird errors, or some messages between Flash and Arduino seem to get dropped, then try slowing down your BAUD rate to 9600 and see if that fixes the issue.</p>
<p>If it does, you can then try and debug and figure out if any particular thing in your code was causing the problem.</p>
<p>&nbsp;</p>
<h3>Sending NULL Bytes from Arduino</h3>
<p>If you are using XMLSocket for your communication between Flash and Arduino, then you need to send a NULL Byte from Arduino when you are done writing a message to the serial port.</p>
<pre><code>Serial.println("Hello From Arduino");
Serial.print(0, BYTE);</code></pre>
<p>&nbsp;</p>
<p>However, I have run into issues where if you send multiple NULL Bytes too fast, then some of the messages will get dropped (not sure if this is an Arduino, TinkerProxy or Flash issue).</p>
<p>So, if you are using XMLSocket, and seem to be losing messages from Arduino, then try reducing some of NULL Bytes being sent and see if that fixes the issues.</p>
<p>&nbsp;</p>
<h3>Sending data in Socket connect handler</h3>
<p>Be careful when sending data from Flash in the Socket class&#8217;s Event.CONNECT handler. For example:</p>
<pre><code>private function onConnect(event:Event):void
{
	_socket.writeUTFBytes("Hello from Flash");
	_socket.flush();
}</code></pre>
<p>&nbsp;</p>
<p>My experience is that this message will be lost. Remember that the Arduino does not run its code until something connects to it. TinkerProxy appears to not connect to the Arduino until the Flash content connects to TinkerProxy.</p>
<p>Thus, the Event.CONNECT event handler may be broadcast before TinkerProxy has connected to the Arduino or before the Arduino has finished running its setup function.</p>
<p>I have found that you have to wait about 2 seconds (2000 ms) before you can reliably send data to the Arduino after you connect to TinkerProxy. So, just start a two second timer in the connect event handler, and send your data once that fires (then clean up the timer).</p>
<p>Here is an example:</p>
<pre><code>var connectTimer:Timer;
private function onConnectTimer(event:TimerEvent):void
{
	connectTimer.stop();
	connectTimer.removeEventListener(TimerEvent.TIMER, onConnectTimer);
	connectTimer = null;

	_socket.writeUTFBytes("Hello from Flash");
}

private function onConnect( event:Event ):void
{
	connectTimer = new Timer(PING_INTERVAL);
	connectTimer.addEventListener(TimerEvent.TIMER, onConnectTimer);

	connectTimer.start();
}</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>Socket verses XMLSocket</h3>
<p>You can use both the String based XMLSocket class, and the binary based Socket class to send and receive data between the Flash and Arduino.</p>
<p>I suggest that you use the Socket class instead of the XMLSocket class for a number of reasons:</p>
<ol>
<li>Socket is more flexible and extensible, allowing you send send multiple data types (not just strings).</li>
<li>It can be difficult working with strings in Arduino code, and thus easier to send numbers from Flash (which requires Socket class). Ive written a seperate post with info on <a href="/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/">how to send numbers from Flash to Arduino</a>.</li>
<li>Using XMLSocket requires that you send NULL bytes from Arduino to terminate messages, which has caused problems for me in the past (see above).</li>
</ol>
<p>Even if you just want to send Strings, I still suggest using Socket, as you then have the option in the future to send additional data types.</p>
<p>Here is how to send a String with the Socket class:</p>
<p><code>_socket.writeUTFBytes("Hi, im a String!");</code></p>
<p>The one disadvantage to using Socket verses XMLSocket, is that you have to buffer the input yourself, since all of the data may not arrive at once. However, that is pretty simple:</p>
<pre><code>//Character that delineates the end of a message received
//from the Arduino
private static const EOL_DELIMITER:String = "\n";

//string to hold data as it comes in.
private var buffer:String = "";

//event called when data arrives on the socket from the
//arduino
private function onSocketData(event:ProgressEvent):void
{
	//get the string sent from the Arduino. This could be any binary data
	//but in our case, we are sending strings.
	//In general, it is much easier to just always send strings from
	//Arduino, and then parse then in ActionScript
	var data:String = _socket.readUTFBytes( _socket.bytesAvailable );

	//copy the newly arrived data into the buffer string.
	buffer += data;

	//completed message from the server
	var msg:String;
	var index:int;

	//loop through the buffer until it contains no more
	//end of message delimiter
	while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
	{
		//extract the message from the beginning to where the delimiter is
		//we don't include the delimiter
		msg = buffer.substring(0, index);

		//remove the message from the buffer
		buffer = buffer.substring(index + 1);

		//trace out the message (or do whatever you want with it)
		trace("Message Received from Arduino : " + msg);
	}				

}</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>Client / Server Development</h3>
<p>It can be useful to think of Flash / Arduino development as regular client / server development. Most of the communication techniques you would use when developing on the web also apply when developing with the Arduino.</p>
<p>&nbsp;</p>
<h3>Integer Division</h3>
<p>Be careful when dividing numbers on the Arduino. If you divide 2 ints, they are not automatically converted to floats, which means you may get some odd results:</p>
<p><code>5 / 15 //will result in 0</code></p>
<p>If you do need to divide ints, then cast them to a float first, like so:</p>
<p><code>(float)5/(float)15 //results in .33333</code></p>
<p>In general, you should try and work with ints, as working with floats on the Arduino is relatively slow.</p>
<p>&nbsp;</p>
<h3>Dont Upload Arduino code while Flash content is connected</h3>
<p>You can upload new Arduino / sketch programs while TinkerProxy is connected to the Arduino. However, if you try and upload a new program while your Flash content is connected to the TinkerProy (and thus the Arduino), the upload will fail.</p>
<p>Try closing the Flash content and uploading again, and if that doesn&#8217;t work, stop TinkerProxy, press the reset button on the Arduino, and try and upload it again.</p>
<p>&nbsp;</p>
<h3>Serial Monitor is your friend</h3>
<p>Before hooking up your Flash content to the Arduino, try and confirm as much of the Arduino code is working well as possible. </p>
<p>This can often be done by monitoring output (regular and debug) from the Arduino App&#8217;s Serial Monitor (<em>Arduino App > Tools > Serial Monitor</em>). (Make sure to set the correct BAUD rate in the Serial Monitor.</p>
<p>You can put in extra <code>Serial.println()</code> statements in your code, and they will be traced out to the monitor. Once you are sure things are working, then you can remove the statements.</p>
<p>&nbsp;</p>
<h3>Dont forget to blink</h3>
<p>If you are having trouble getting your Flash content to work with the Arduino (and are not receiving data from the Arduino), try blinking LEDs in different parts of your code. That can help tell you what is being run, and narrow down the issue.</p>
<p>Here is a simple function to blink the onboard LED:</p>
<pre><code>//function to blink the LED
void blinkLED(int count)
{
	//loop through
	for(int i = 0; i &lt; count; i++)
	{
		//turn LED on
    	digitalWrite(LED_PIN, HIGH);

		//wait 500 ms
    	delay(500);

		//turn LED off
    	digitalWrite(LED_PIN, LOW);

		//wait 500 ms
    	delay(500);
	}
}</code></pre>
<p>&nbsp;</p>
<p>Which you can call in your code like so:</p>
<p><code>blink(2);</code></p>
<p>If you have any additional tips or things to watch out for, post them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/08/06/flash-arduino-tips-tricks-and-gotchas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting Started with Flash and Arduino</title>
		<link>http://www.mikechambers.com/blog/2010/08/04/getting-started-with-flash-and-arduino/</link>
		<comments>http://www.mikechambers.com/blog/2010/08/04/getting-started-with-flash-and-arduino/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 05:59:56 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[physical computing]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2170</guid>
		<description><![CDATA[In this post, I will show how to setup your development environment and started developing Flash projects that integrate with the Arduino electronic platform. So, what is Arduino? From the website: Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It&#8217;s intended for artists, designers, hobbyists, and anyone interested in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F04%2Fgetting-started-with-flash-and-arduino%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F04%2Fgetting-started-with-flash-and-arduino%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In this post, I will show how to setup your development environment and started developing Flash projects that integrate with the <a href="http://www.arduino.cc">Arduino</a> electronic platform.</p>
<p>So, what is Arduino? From the website:<br />
 <span id="more-2170"></span></p>
<blockquote><p>Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It&#8217;s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.</p></blockquote>
<p>Basically, the Arduino can be a bridge from the computer to the physical world, allowing developers and artist to both read input from sensors, as well as output from the computer to the physical world. </p>
<p>The design of the Arduino board is open source, which among other things, means that there are a lot of options for both the main board, as well as add-ons. Take a look at the Arduino sections at <a href="http://www.adafruit.com/index.php?main_page=index&#038;cPath=17_21">Adafruit</a> and <a href="http://www.sparkfun.com/commerce/categories.php?c=103">SparkFun.com</a> to get an idea of the type of add-ons / shields that have already been created.</p>
<p>For this tutorial, we will cover how to:</p>
<ol>
<li>Set up Arduino and make sure it is connected correctly.</li>
<li>Install and configure a proxy program so Flash can communicate with the Arduino.</li>
<li>Build a simple Flash application that blinks an LED on the Arduino.</li>
</ol>
<p>Once we are finished with the tutorial, you will be all setup to begin working on real world Flash / Arduino projects.</p>
<p><strong>What you will need</strong></p>
<ol>
<li>An Arduino Main Board: These are pretty cheap ($20 &#8211; $30) and you can pick them up tons of places (I recommend <a href="http://www.adafruit.com/index.php?main_page=product_info&#038;cPath=17&#038;products_id=50">AdaFruit</a> and <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=666">Sparkfun</a>). If you are just getting started with basic electronics, you may want to consider picking up a starter kit (<a href="http://www.adafruit.com/index.php?main_page=product_info&#038;cPath=17&#038;products_id=68">AdaFruit</a>, <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9284">SparkFun</a>). The most current version is the <a href="http://arduino.cc/en/Main/ArduinoBoardDuemilanove">Arduino Duemilanove with Atmega328</a> which I will be using. Make sure that you get the appropriate USB cable if it is not included with your Arduino.</li>
<li>Arduino software and drivers for your platform: You can grab these from the <a href="http://arduino.cc/en/Main/Software">Arduino downloads page</a>. I will be using the Arduino 0018 download on Mac.</li>
<li>TinkerProxy. <a href="http://code.google.com/p/tinkerit/wiki/TinkerProxy">TinkerProxy</a> is a small local proxy that acts as a bridge between the serial port and Flash (via a socket server). This allows Flash to communicate with the Arduino.</li>
<li>Editor for writing ActionScript code. You can use any editor such as Flash Authoring, Flash Builder, FDT, etc&#8230;</li>
<li>Flash / ActionScript compiler. Finally, you will need a way to compile your ActionScript. Again, this could be anything that can compile ActionScript 3, such as Flash Builder, Flash Authoring, etc&#8230;  I will be using the MXMLC command line compiler from the open source Flex 4 SDK.</li>
</ol>
<p>You can download and view all of the code from this article from <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/FlashArduinoGettingStarted/">my GitHub examples repository</a>.</p>
<p><strong>Installing Arduino</strong></p>
<p>First, we need to install the Arduino software and drivers. The Arduino site has very good <a href="http://arduino.cc/en/Guide/HomePage">platform specific getting started articles</a>, so I am not going to try and reproduce them here.</p>
<p>Following the instructions for your specific platform, and once your Arduino is setup and working, come back here:</p>
<ul>
<li><a href="http://arduino.cc/en/Guide/Windows">Windows</a></li>
<li><a href="http://arduino.cc/en/Guide/MacOSX">Mac OS X</a></li>
<li><a href="http://www.arduino.cc/playground/Learning/Linux">Linux</a></li>
</ul>
<p>If you run into any issues, make sure that you check out the <a href="http://arduino.cc/en/Guide/Troubleshooting">troubleshooting document</a>.</p>
<p><strong>Setting up TinkerProxy</strong></p>
<p>Now that you have your Arduino set up and working, we are ready to install a local proxy application that will bridge the communication between Flash and the Arduino. There are a <a href="http://www.arduino.cc/playground/Interfacing/SerialNet">number of possible proxies</a> for this, but for this article, we will be using <a href="">TinkerProxy</a>. This is an open source port of serproxy and has good support for Flash.</p>
<p>Download the most recent version of TinkerProxy from the project&#8217;s <a href="http://code.google.com/p/tinkerit/downloads/list">download page</a>. I am using the 2.0 release from March 2009. </p>
<p>Once you have downloaded it, unzip the file. This will create a directory named <i>tinkerproxy-2_0</i>. Move this directory to where you want to keep it. On my system, I put it at <i>~/bin/tinkerproxy</i>. If you like, you can add this directory to your system&#8217;s path, which will allow you to start TinkerProxy from anywhere.</p>
<p>Open up the tinkerproxy directory in your file manager. You should see 3 files:</p>
<ul>
<li>serproxy.cfg : configuration file</li>
<li>serproxy.exe : windows executable</li>
<li>serproxy.osx : mac executable</li>
</ul>
<p>If you are on Mac, rename the <i>serproxy.cfg</i> file to <i>serproxy.osx.cfg</i>. (Im not sure if you need to rename it on Windows).</p>
<p>Open the configuration file (<i>serproxy.osx.cfg</i>) in a text editor so we can make a couple of changes.</p>
<p>First, find the line that looks like this:</p>
<p><code>serial_device1=/dev/tty.usbserial-A6004osh</code></p>
<p>You need to update this to the serial port that the Arduino is connected to. You can do this 2 ways.</p>
<p>The easiest way is to open up the Arduino development environment (make sure the Arduino is connected), and find the entry in <i>Arduino > Tools > Serial Port</i>.</p>
<p>You can also run the following command : <code>ls -l /dev/</code> and find the Arduino entry there (it will be the <i>usbserial</i> entry).</p>
<p>Once you have found the correct serial port, update the configuration file with the information.</p>
<p><code>serial_device1=/dev/cu.usbserial-A700dZgM</code></p>
<p>Next, we need to specify which comm port the Arduino is associated with. (This is usually comm port 3 on Windows). For the mac, just change the comm_ports entry like so:</p>
<p><code>comm_ports=1</code></p>
<p>Next, change the <em>comm_baud</em> setting to 9600.</p>
<p><code>comm_baud=9600</code></p>
<p>This is the speed that Arduino and TinkerProxy communicate (9600 bytes a second). You can set this at a higher level but it is good to start out low in order to ensure that any issues are not due to the baud rate. </p>
<p>Finally, we need to update the <em>net_port</em> setting. Comment out all of the net_port* entries, except for <em>net_port1</em>. For its value, set the port that you want the proxy to listen on.</p>
<p><code>net_port1=5331</code></p>
<p>Here is what my entire <em>serproxy.osx.cfg</em> proxy looks like:</p>
<pre><code># Config file for serproxy
# See serproxy's README file for documentation

# Transform newlines coming from the serial port into nils
# true (e.g. if using Flash) or false
newlines_to_nils=false

# on a mac you will need to add this
serial_device1=/dev/cu.usbserial-A700dZgM

# Comm ports used
comm_ports=1

# Default settings
comm_baud=9600
comm_databits=8
comm_stopbits=1
comm_parity=none

# Idle time out in seconds
timeout=300

# Port 1 settings (ttyS0)
net_port1=5331

# Port 2 settings (ttyS1)
#net_port2=5332

# Port 3 settings (ttyS2)
#net_port3=5333

# Port 4 settings (ttyS3)
#net_port4=5334</code></pre>
<p>&nbsp;</p>
<p>Once you have made the changes, save the file and close it.</p>
<p>We are now ready to start TinkerProxy. Make sure that the Arduino is still connected to your computer.</p>
<p>Open up the terminal, and change to the directory where TinkerProxy is installed. Run the following command:</p>
<p><code>./serproxy.osx</code></p>
<p>You should see the following output:</p>
<pre><code>Serproxy 0.2.0 - Tinker.it
Based on code by (C)1999 Stefano Busti, (C)2005 David A. Mellis

Waiting for clients</code></pre>
<p>&nbsp;</p>
<p>If you see this, then you are all set. If you get an error, double check your configuration file, and make sure you are in the directory where <a href="">serproxy.osx</a> is located. </p>
<p><strong>Code</strong></p>
<p>We are finally ready to write some code. For this tutorial, we have Flash control an LED on the Arduino, and have the Arduino send status messages back to Flash. </p>
<p>We need to write two pieces of code. First, we need to write code that will be loaded and run on the Arduino. This will control the hardware, and listen for and respond to messages from Flash. We then need to write some Flash / ActionScript code that will send and received commands to and from the Arduino.</p>
<p><strong>Arduino Code</strong></p>
<p>Lets write the Arduino code first.</p>
<p>Open up the Arduino application, and create a new file called <em>FlashBlink.pde</em>. Type the code below into the editor:</p>
<pre><code>/*
	Simple example that controls the blinking of the LED
	connected to digital PIN 13.

	Created by Mike Chambers

http://www.mikechambers.com

	You can hook up an external LED by connecting an LED
	to PIN 13 and GND (make sure that the anode (long leg) is
	connected to PING 13 and the cathode (short leg) is connected
	to GND.

	The on board LED is also connected to PIN 13, and will
	be controlled by this program.

	Example is released under the following license:

	The MIT License

	Copyright (c) 2010 Mike Chambers

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

//The digital pin that the LED is connected to
#define LED_PIN 13

//incoming command to toggle LED blinking state
#define TOGGLE_LED_STATE 't'

#define EOL_DELIMITER "\n"

//int to hold incoming byte when we read it
int incoming = 0;

//whether the led should blink
boolean shouldBlinkLED = false;

//setup function. called when the program first runs
void setup()
{
	//start listening for incoming messages on the serial port
	//at 9600 baud
	Serial.begin(9600);

    //Send a message out that the program is initializing
    Serial.print("INITIALIZING");

    //Flash looks for this to know when the message is done
    //See the comments in the Flash file for more info.
    Serial.print(EOL_DELIMITER);

	//set the pin more for the digital ping that
	//the LED is connected to (can be INPUT or OUTPUT)
	pinMode(LED_PIN, OUTPUT);

	//blink the LED 5 times so we can visually see that the
	//program is running
	blinkLED(5);

    //Send out a message that initialization is complete.
    Serial.print("READY");
    Serial.print(EOL_DELIMITER);
}

//program loop. Called each timer tick (really fast)
void loop()
{
	//check if we should blink the LED
	if(shouldBlinkLED)
	{
		//if so, blink the LED
		blinkLED(1);
	}

	//check if there are any bytes available
	//on the Serial port
	if(Serial.available() &gt; 0)
	{
		//read a single byte.
		incoming = Serial.read();

		//Calling Serial.read() remove the byte from the Serial
		//buffer. In this case, if there are multiple bytes that
		//were sent, the next one will be handled on the next loop.
		//You could also grab it by calling Serial.read() again

		//check if the incoming byte was a command to toggle
		//the state of the LED blinking
		if(incoming == TOGGLE_LED_STATE)
		{
			//it was a command

			//toggle the state of the LED Blinking
      		shouldBlinkLED = !shouldBlinkLED;

			//send a message to the Serial port with the
			//new LED Blink state

			Serial.print("LED BLINK STATE: ");
			if(shouldBlinkLED)
			{
				Serial.print("ON");
			}
        	else
        	{
          		Serial.print("OFF");
        	}

			Serial.print(EOL_DELIMITER);

			//note, if you are using XMLSocket in Flash to read
			//the string data over the socket then we also have to send
			//a null byte with the following command:
			//
			//Serial.print(0, BYTE);
			//
			//However, in general, it is going to be easier, and more flexible
			//to use the Socket class (and not the XMLSocket class).
    	}
	}
}

//function to blink the LED
void blinkLED(int count)
{
	//loop through
	for(int i = 0; i &lt; count; i++)
	{
		//turn LED on
    	digitalWrite(LED_PIN, HIGH);

		//wait 500 ms
    	delay(500);

		//turn LED off
    	digitalWrite(LED_PIN, LOW);

		//wait 500 ms
    	delay(500);
	}
}
</code></pre>
<p>&nbsp;</p>
<p>The code is extensively commented and explains what is going on. Make sure that you read through them.</p>
<p>Basically, the code just listens for a single character (&#8220;t&#8221;) to be sent over the Serial port. When it receives a &#8220;t&#8221; then it toggles the blinking of the LED (from ON to OFF or vice versa). When the LED is toggled, the Arduino will send a message back to Flash with the current LED state. Finally, the code blinks the LED 5 times when it starts so you can see that it is loaded and is running correctly.</p>
<p>Once you have the code typed in, press the &#8220;Verify&#8221; button (looks like a Play button) (<em>Sketch > Verify / Compile</em>) in order to ensure that you don&#8217;t have any compilation errors. Once it compiles, then we are ready to upload it to the Arduino.</p>
<p>Make sure that the Arduino is connected, and then press the &#8220;Upload&#8221; button in the Arduino editor (<em>File > Upload to I/O Board</em>). This will upload the code to the Arduino. Note that even if you disconnect the Arduino, the code will still be on there, and begin running when the Arduino is connected (you dont have to re-upload it every time).</p>
<p>Before we write the ActionScript code, lets test the Arduino code we just wrote to ensure everything is working. We can do this by opening the Arduino Serial Monitor and send a message.</p>
<p>Open the Serial Monitor (<em>Tools > Serial Monitor</em>) and make sure that the baud rate is set to 9600 (in the bottom right of the window). Wait until you see the &#8220;INITIALIZING&#8221; and &#8220;READY&#8221; messages printed from the Arduino (we send these from our code). At the top, type in the letter &#8220;t&#8221; and click &#8220;Send&#8221;. This sends the message to the Arduino. You should see the LED blinking toggle state, and also see a message in the console from the Arduino with the LED blink status.</p>
<p>If something doesn&#8217;t work, then go back and check your code.  Make sure you have set the baud rate correctly in your code and in the Serial Monitor. Once everything is working, then we are ready to move on to the final step, which is to write some Flash code to control and communicate with the Arduino.</p>
<p><strong>Flash / ActionScript Code</strong></p>
<p>From the Flash side of things, integrating with the Arduino is pretty much the same as regular client / server development with Flash. Indeed, we will use the <a href="http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/net/Socket.html?allClasses=1">Socket</a> class to send and receive messages to the Arduino via TinkerProxy.</p>
<p>Here is our ActionScript code for interacting with the Arduino:</p>
<pre><code>/*
	Simple Example that connects to an Arduino (via TinkerProxy) and controls
	the blinking of an LED.

	Created by Mike Chambers

http://www.mikechambers.com

	The MIT License

	Copyright (c) 2010 Mike Chambers

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

package
{
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.net.Socket;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.utils.Endian;
	import flash.events.MouseEvent;

	public class FlashBlink extends Sprite
	{

		//command sent to the Arduino to toggle LED blinking state
		private static const TOGGLE_LED_STATE:String = "t";

		//Character that delineates the end of a message received
		//from the Arduino
		private static const EOL_DELIMITER:String = "\n";

		//socket we will use to connect to TinkerProxy
		private var _socket:Socket;

		//Address where TinkerProxy is located. Will usually be
		//localhost / 127.0.0.1
		private var _proxyAddress:String = "127.0.0.1";

		//port TinkerProxy is listening on
		private var _proxyPort:uint = 5331;

		//constructor
		public function FlashBlink()
		{
			//listen for when we are added to the stage
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

			//create a Sprite to add to the stage.
			//This will be a simple button
			var s:Sprite = new Sprite();

			//draw a green square in the Sprite
			s.graphics.beginFill(0x00FF00);
			s.graphics.drawRect(0,0, 200,100);
			s.graphics.endFill();

			//Add Sprite to the display list
			addChild(s);

			//position it
			s.x = 50;
			s.y = 50;

			//listen for when the user clicks the Sprite
			s.addEventListener(MouseEvent.CLICK, onClick);

			_socket = new Socket();

			//Register for socket events

			//socket connected
			_socket.addEventListener( Event.CONNECT, onConnect );			

			//socket closed
			_socket.addEventListener( Event.CLOSE, onClose );			

			//data received from socket
			_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );

			//Error connecting
			_socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );

			//Security Error
			_socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );

			//need to set Endianness for Socket. THIS IS IMPORTANT
			//If this is set incorrectly, the Arduino will not be able to understand
			//all of the data sent from Flash.
			//
			//See:
			//http://www.mikechambers.com/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/
			_socket.endian = Endian.LITTLE_ENDIAN;

			//connect
			_socket.connect(_proxyAddress, _proxyPort);
		}

		//called when we connect to the proxy server
		private function onConnect(event:Event):void
		{
			/*
				note, you cannot reliably write data to the socket here.

				Im not sure if this is a Flash player issue, or a timing issue
				with the proxy.
			*/
			trace("Socket Connected");
		}

		/*
			This function / event handler is called when data is received
			on the socket. 

			However, it is important to remember that the event is called as
			data is received, which means that not all of the data may be available
			when it is called.

			For example, if you sent the string "Hello World" from Arduino,
			then the event handler might be called twice. Once with "Hello Wo" and
			a second time with "rld".

			"Because of this, in most cases, you need to buffer the data, and parse
			out messages, looking for a character (that you specify) that delineates the
			end of a message.

			If you just want to send a single character back from Arduino, then this
			is not necessary. However, the handler below is generic, and already does all
			of the buffering, so you can just use it.
		*/

		//string to hold data as it comes in.
		private var buffer:String = "";

		//event called when data arrives on the socket from the
		//arduino
		private function onSocketData(event:ProgressEvent):void
		{
			//get the string sent from the Arduino. This could be any binary data
			//but in our case, we are sending strings.
			//In general, it is much easier to just always send strings from
			//Arduino, and then parse then in ActionScript
			var data:String = _socket.readUTFBytes( _socket.bytesAvailable );

			//copy the newly arrived data into the buffer string.
			buffer += data;

			//completed message from the server
			var msg:String;
			var index:int;

			//loop through the buffer until it contains no more
			//end of message delimiter
			while((index = buffer.indexOf(EOL_DELIMITER)) > -1)
			{
				//extract the message from the beginning to where the delimiter is
				//we don't include the delimiter
				msg = buffer.substring(0, index);

				//remove the message from the buffer
				buffer = buffer.substring(index + 1);

				//trace out the message (or do whatever you want with it)
				trace("Message Received from Arduino : " + msg);
			}				

		}

		//called when the user clicks the button on stage
		private function onClick(event:MouseEvent):void
		{
			trace("onClick");

			//make sure we are connected to the socket
			if(!_socket.connected)
			{
				//if not, don't do anything
				trace("You must be connected to send a command to the Arduino.");
				return;
			}

			//write the command to the server
			_socket.writeUTFBytes(TOGGLE_LED_STATE);

			//flush the socket. Not really necessary, but here for forward compatibility.
			_socket.flush();
		}

		//called when the socket is closed
		private function onClose(event:Event):void
		{
			trace("Socket Closed");
		}

		//called if an error occurs while connecting to the socket.
		private function onIOError(event:IOErrorEvent):void
		{
			trace("IOErrorEvent : " + event.text);
		}

		//called when there is a security error. Usually if you try to connect to a socket
		//when the SWF doesn't have permission.
		//See:
		//http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security_04.html
		//In most cases, when testing locally, this will not be an issue.
		private function onSecurityError(event:SecurityErrorEvent):void
		{
			trace("SecurityErrorEvent : " + event.text);
		}
	}
}</code></pre>
<p>&nbsp;</p>
<p>Now, that looks like a lot of code for a simple example, but remember that it is mostly comments.</p>
<p>Basically, when the SWF starts running, we connect to the TinkerProxy server. When the button / square on the stage is clicked, then we send a message to the Arduino, telling it toggle the blink state. If you watch the Arduino closely, you can see when it is receiving information as the RX LED button will blink. Also notice how the blinking of the LED is turned on / off each time you click the button in the SWF.</p>
<p>The code also listens for the Arduino to send messages back. In this case, it is just a simple string specifying whether the LED is in blinking mode. Pay particular attention to the code and comments where we get the message from the Arduino. We have to buffer the code coming from the Arduino, since the entire message may not always arrive in one piece. The function is pretty generic can be reused in any project. All of the messages from the Arduino are traced out.</p>
<p>Also, notice that we specifying the Endianness for the socket server: </p>
<p><code>_socket.endian = Endian.LITTLE_ENDIAN;</code></p>
<p>This is necessary to ensure that the Arduino can understand all of the data sent from Flash.</p>
<p>Make sure that the Arduino has initialized before you send any messages to it, or else the messages will be dropped. Normally this is not an issue, since the setup runs instantly, but in our case, we are blinking the LEDs first, so it takes a couple of moments for the setup to run.</p>
<p>I have uploaded a short video showing the Arduino running the code (click through the view the HD version).</p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/QtpuxLu4WmU&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/QtpuxLu4WmU&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<p>At this point, you should have your system setup for Flash / Arduino development, and have a basic understanding of how to send and receive data between Flash content and the Arduino. Of course, the real fun comes when you begin to hook up sensors and motors and real world data. Check out some of the pre-made shields created for the Arduino at <a href="http://www.adafruit.com/index.php?main_page=index&#038;cPath=17_21">Adafruit</a> and <a href="http://www.sparkfun.com/commerce/categories.php?c=103">SparkFun.com</a> to get an idea of what is out there.</p>
<p><strong>Addition Resources</strong></p>
<ul>
<li><a href="http://www.amazon.com/Getting-Started-Arduino-Make-Projects/dp/0596155514">Getting Started with Arduino Book</a> (Highly Recommended)</li>
<li><a href="http://arduino.cc/en/Tutorial/HomePage">Arduino Examples</a></li>
<li><a href="http://arduino.cc/en/Reference/HomePage">Arduino Reference</a></li>
<li><a href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?board=interfacing">Arduino / Flash Forum</a></li>
<li><a href="http://www.arduino.cc/playground/Interfacing/Flash">Arduino / Flash Playground</a></li>
<li><a href="http://blog.kevinhoyt.org/?cat=29">Kevin Hoyt&#8217;s Blog Posts on Flash / Arduino</a> (Really great resource)</li>
<li><a href="http://www.amazon.com/Getting-Started-Electronics-Forrest-Mims/dp/0945053282/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1280982691&#038;sr=8-1">Getting Started in Electronics</a> (Classic book, very approachable)</li>
<li><a href="http://www.amazon.com/MAKE-Electronics-Learning-Through-Discovery/dp/0596153740/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1280982679&#038;sr=8-1">MAKE: Electronics: Learning Through Discovery</a></li>
</ul>
<p>Again, you can download all of the code from this tutorial <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/FlashArduinoGettingStarted/">here</a>.</p>
<p>If you find any errors in the post, or run into any issues trying to get stuff to work, then post in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/08/04/getting-started-with-flash-and-arduino/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Quick Contest : Flash and Arduino</title>
		<link>http://www.mikechambers.com/blog/2010/08/04/quick-contest-flash-and-arduino/</link>
		<comments>http://www.mikechambers.com/blog/2010/08/04/quick-contest-flash-and-arduino/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 18:21:49 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[physical computing]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2161</guid>
		<description><![CDATA[I have been working with integrating Flash and Arduino on a project lately, and really having a blast. It was pretty easy to get started, yet there is a ton of extensibility and flexibility. Well, I dont want to be the only one having this much fun, so I figured I would put together a [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F04%2Fquick-contest-flash-and-arduino%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F04%2Fquick-contest-flash-and-arduino%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I have been working with integrating Flash and <a href="http://arduino.cc/">Arduino</a> on a project lately, and really having a blast. It was pretty easy to get started, yet there is a ton of extensibility and flexibility.</p>
<p>Well, I dont want to be the only one having this much fun, so I figured I would put together a quick contest. So, if you are interested in playing around with Flash and Arduino, then tell me why in the comment below. Let me know about what project you have in mind. Bonus points for creativity and visual aids.</p>
<p>The winner will receive the following:<br />
<span id="more-2161"></span></p>
<ul>
<li><a href="http://www.adafruit.com/index.php?main_page=product_info&#038;cPath=17&#038;products_id=68">Arduino Duemilanove w/Atmega328 Starter Pack</a></li>
<li><a href="http://www.amazon.com/Getting-Started-Arduino-Make-Projects/dp/0596155514/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1280945812&#038;sr=8-1">Getting Started with Arduino Book</a></li>
<li><a href="http://www.amazon.com/MAKE-Electronics-Learning-Through-Discovery/dp/0596153740/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1280946050&#038;sr=8-1">Make Electronics : Learning Through Discovery</a></li>
<li><a href="http://www.adobe.com/products/flashbuilder/">Flash Builder 4</a></li>
</ul>
<p>I will pick a winner on Thursday morning, August 5th, 2010 (PST).</p>
<p>Post your submissions in the comments.</p>
<p>Update : Contest is now closed.</p>
<p>Update : Winners Announced</p>
<p>Wow!!! A ton of great ideas were posted, but one really stood out. So, the winner of the quick contest is:</p>
<p>Bryan Yen, for his <a href="http://blog.devdeploy.com/2010/08/04/quick-contest-flash-and-arduino/">Adobe Flash Garden Data Visualization</a> idea. Not only is it a really interesting idea (which is completely doable, but Bryan went the extra mile with a great presentation. Bryan wins the Arduino Starter kit from AdaFruit, books and copy of Flash Builder.</p>
<p>However, as I mentioned above, I was really surprised by how many people were interested in this area. I can&#8217;t let all of these dreams and aspirations go unrealized, so (with some help from the Flash Platform budget people), everyone who entered an idea into the contest will receive an <a href="http://www.adafruit.com/index.php?main_page=product_info&#038;cPath=17&#038;products_id=193">Arduino Budget pack</a>.</p>
<p>I will send an email to everyone who won in order to get your shipping information. Please respond immediately so I can send everything out. Again, thanks to the Flash team, and to <a href="http://www.adafruit.com">Adaruit.com</a> for making this possible.</p>
<p>Make something cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/08/04/quick-contest-flash-and-arduino/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
		<item>
		<title>Sending Multibyte Numbers from ActionScript to Arduino</title>
		<link>http://www.mikechambers.com/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/</link>
		<comments>http://www.mikechambers.com/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 06:03:25 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[physical computing]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2152</guid>
		<description><![CDATA[If you follow me on twitter, then you have probably noticed that I have been learning about Flash, physical computing and electronics lately. I started out working with Phidgets, and have recently begun working with the Arduino (Ill write up Flash / Arduino getting started tutorial shortly). I am currently working on a project where [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F01%2Fsending-multibyte-numbers-from-actionscript-to-arduino%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F08%2F01%2Fsending-multibyte-numbers-from-actionscript-to-arduino%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>If you follow me on twitter, then you have probably noticed that I have been learning about Flash, physical computing and electronics lately. I started out working with <a href="http://www.mikechambers.com/blog/2010/07/26/getting-started-with-phidgets-and-actionscript/">Phidgets</a>, and have recently begun working with the <a href="http://Arduino.cc/">Arduino</a> (Ill write up Flash / Arduino getting started tutorial shortly). I am currently working on a project where I need to send data from Flash to the Arduino, and quickly discovered that it is not as easy as I thought it would be. In this post, I will show an example of how to send multibyte Numbers from ActionScript to Arduino.<br />
<span id="more-2152"></span><br />
When you send data to Arduino over the Serial port, Arduino reads that data byte by byte. Thus, if you want to send a string to the Arduino, you have to read the string byte by byte and reconstruct it which is a bit of a hassle, but nothing too difficult. However, what if you want to send a number? If you just need to send a small whole number (up to 127), then you can simply write the number to the socket:</p>
<pre><code>socket.writeByte(127);</code></pre>
<p>&nbsp;</p>
<p>and then read the same number in Arduino by reading the byte:</p>
<pre><code>value = Serial.read();
Serial.println(value, DEC); //will send 127</code></pre>
<p>&nbsp;</p>
<p>However, if you need to send a number larger than 127, then things get a bit tricky. This is because numbers larger than 127 require multiple bytes to send, which means that you cannot simply read in the number on the Arduino side (since you have to read byte by byte). One solution (suggested to my by <a href="http://bytearray.org/">Thibault Imbert</a>) is to multiply the number by some ratio. This would allow you to represent numbers larger than 127, but at the price of accuracy.</p>
<p>So, after much searching, I found a <a href="">post</a> on the Arduino forums that shows how to build a float from a byte array using a union. With that code as a guide, I was able to figure out how to send a float from ActionScript to Arduino.</p>
<p>I have uploaded a <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/MultibyteNumbersFromFlash">simple example</a> into my github example code repository. The Flash part of the example is done in Flex and ActionScript, but the ActionScript code is the same regardless of what you are using to build your Flash content.</p>
<p>Note, all of the code in the examples is released under an <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</p>
<p>First, lets look at the Arduino sketch:</p>
<pre><code>
//union that we will use
//the construct the float
//from the individual bytes
//sent from Flash / ActionScript
union u_tag {
    byte b[4];
    float ival;
} u;

float value;

void setup()
{
  Serial.begin(9600);
}

void loop()
{  

  //this example assumes only floats / Numbers
  //are being sent. So we just look for data in 4 byte
  //increments
  if(Serial.available() > 3)
  {

    //read the 4 bytes into the union
    u.b[0] = Serial.read();
    u.b[1] = Serial.read();
    u.b[2] = Serial.read();
    u.b[3] = Serial.read();

    //retrieve the float value of the union
    //(based on the bytes passed in)
    value = u.ival;

    //send the reconstructed float back to the Serial
    //flash
    Serial.print(value, DEC);

    //write out a null byte, (the Flash Socket
    //class looks for this)
    Serial.print(0, BYTE);
  }

}</code></pre>
<p>&nbsp;</p>
<p>Now, lets look at how we send the Number from ActionScript:</p>
<pre><code>
private function onApplicationComplete():void
{
	//only allow numbers, period and minus sign
	numberInput.restrict = ".0-9\\-";

	socket = new Socket()
	socket.addEventListener(Event.CONNECT, onConnect);
	socket.addEventListener(Event.CLOSE, onClose);
	socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
	socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
	socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );

	//disable until we connect
	this.enabled = false;

	//this is important! If you dont set this to
	//little endian, then Arduino wont understand
	//the bytes
	socket.endian = Endian.LITTLE_ENDIAN;

	socket.connect(SERVER_ADDRESS, PORT);
}

private function onSendClick():void
{
	//get the number that the user input
	var out:Number = Number(numberInput.text);

	//write it as a float to the server.
	//this is important.
	socket.writeFloat(out);

	//if number is too big, then it will overflow on
	//the Arduino, and probably come back as 0.00000
}</code></pre>
<p>&nbsp;</p>
<p>Again, you can <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/MultibyteNumbersFromFlash">download the complete example from my GitHub repository</a>.</p>
<p>A couple of things to take note of. Notice that we set the Endianness of the socket to LITTLE_ENDIAN. This is necessary for Arduino to be able to understand the number we are sending.</p>
<p>Second, notice that we send the number by calling <code>socket.writeFloat</code>. I initially tried <code>writeInt</code>, but ActionScript ints are 4 bytes, where the Arduino int is 2. Thibault suggested I send the number using <code>writeShort</code>, but that also did not work.</p>
<p>Basically, the only way I was able to get it to work, was to use <code>writeFloat</code>. In practice this should not matter, but it is something you should keep in mind.</p>
<p>Now, I am pretty sure it is possible to send other ActionScript multibyte number types using <code>writeInt</code>, <code>writeUnsignedInt</code> and <code>writeShort</code>, since we are just sending raw bytes over the socket. However, I have not figured out how to reconstruct them on the Arduino side yet. If anyone gets additional types working, then post a note in the comments.</p>
<p>UPDATE : I have <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/MultibyteIntFromFlash/">posted another exampl</a>e that shows how to send ints / shorts from ActionScript to Arduino. You can view the code <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Arduino/MultibyteIntFromFlash/">here</a>.</p>
<p>Being able to easily send multibyte numbers make communication significantly easier, because, now, among other things, we can easily send packets of complex data. Ill leave that for another post though.</p>
<p>If you have any suggestions or corrections, then leave them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/08/01/sending-multibyte-numbers-from-actionscript-to-arduino/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>as3corelib Library moved to GitHub</title>
		<link>http://www.mikechambers.com/blog/2010/07/29/as3corelib-library-moved-to-github/</link>
		<comments>http://www.mikechambers.com/blog/2010/07/29/as3corelib-library-moved-to-github/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 18:12:24 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3corelib]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2148</guid>
		<description><![CDATA[I have just moved the as3corelib library over to GitHub (at the urging of Darron Schall). This should make the project a little easier to manage, and in particular, make it easier for developers to contribute patches, and for me to review and accept them. All files, issues, source, and wiki pages have been moved [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F29%2Fas3corelib-library-moved-to-github%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F29%2Fas3corelib-library-moved-to-github%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I have just moved the <a href="https://github.com/mikechambers/as3corelib">as3corelib</a> library over to GitHub (at the urging of <a href="http://twitter.com/darronschall/status/19742131674">Darron Schall</a>). This should make the project a little easier to manage, and in particular, make it easier for developers to contribute patches, and for me to review and accept them.</p>
<p>All files, issues, source, and wiki pages have been moved from the old site to the new one. The old site is still up, but directs people to the new site, and will no longer be updated.</p>
<p>Make sure to update your bookmarks to:<br />
<a href="https://github.com/mikechambers/as3corelib">https://github.com/mikechambers/as3corelib</a></p>
<p>Thanks to Darron Schall who help with the transition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/07/29/as3corelib-library-moved-to-github/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting Started with Phidgets and ActionScript 3</title>
		<link>http://www.mikechambers.com/blog/2010/07/26/getting-started-with-phidgets-and-actionscript/</link>
		<comments>http://www.mikechambers.com/blog/2010/07/26/getting-started-with-phidgets-and-actionscript/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 21:41:00 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[physical computing]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2127</guid>
		<description><![CDATA[Phidgets are a set of devices and sensors that provide a simple way for developers to create applications that both send and receiving information from external sensors, motors and pretty much anything else you can hook up via electronics. They are similar to the open source Arduino electronic platform. Im not going to do a [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F26%2Fgetting-started-with-phidgets-and-actionscript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F26%2Fgetting-started-with-phidgets-and-actionscript%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.phidgets.com/index.php">Phidgets</a> are a set of devices and sensors that provide a simple way for developers to create applications that both send and receiving information from external sensors, motors and pretty much anything else you can hook up via electronics. They are similar to the open source <a href="http://www.arduino.cc/">Arduino</a> electronic platform. </p>
<p>Im not going to do a big comparison between Arduino and Phidgets in this post. I plan to do that in another post (along with an Arduino / Flash hello world article). Suffice it to say though, that one of the major benefits of Phidgets, is that the Phidgets devices are a bit higher level, both on the hardware, and software side and thus can be a little easier for a developer new to electronics to get started with.<br />
<span id="more-2127"></span><br />
If you are a Flash developer, Phidget&#8217;s are very attractive, as they provide a very well developed and documented <a href="http://www.phidgets.com/programming_resources.php">ActionScript API for working with the Phidgets devices</a>. The API works by connecting via a socket from Flash content, to a Phidgets local (or remote) server. The Phidgets server connects directly to the Phidgets devices over USB, and proxies communication between the Flash / ActionScript content, and the Phidgets devices and electronics.</p>
<p>In this post, I will cover everything you need to know in order to get started integrating Phidgets electronics and Flash / ActionScript content.</p>
<link rel="stylesheet" href="/styles/lightbox.css" type="text/css" media="screen" /><script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script><script type="text/javascript" src="/scripts/scriptaculous.js?load=effects,builder"></script><script type="text/javascript" src="/scripts/lightbox.js"></script><br />
You can grab all of the code for the example from my <a href="http://github.com/mikechambers/ExamplesByMesh/tree/master/Phidgets/PhidgetsHelloWorld/">GitHub Example repository</a>.</p>
<p>First, here is what you will need to get started:</p>
<ul>
<li>An ActionScript 3 compiler (such as <a href="http://www.adobe.com/products/flashbuilder/">Flash Builder</a>, <a href="http://www.adobe.com/products/flash/">Flash Professional</a>, or the open source MXMLC compiler included in the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">Flex 4 SDK</a>).</li>
<li>An IDE or text editor to edit ActionScript (such as Flash Builder, Flash Authoring, FDT, TextMate or any other general text editor).</li>
<li>The Phidgets ActionScript 3 library. You can find the library&#8217;s SWC in Flex 3 Code Sample ZIP file, and can find the source in the Flash AS3 Code Sample Zip file, both on the <a href="http://www.phidgets.com/programming_resources.php">Phidgets programming resources page</a>.</li>
<li>The Phidgets drivers for your platform. This will install the drivers for the devices, as well as the proxy application / server that Flash will use to communicate with the devices. You can download these from the <a href="http://www.phidgets.com/drivers.php">Phidgets driver page</a>.</li>
<li>At least one Phidgets device that can connect directly to your computer USB. If you are just getting started, then I would recommend the <a href="http://www.phidgets.com/products.php?category=0&#038;product_id=1018">1018 &#8211; PhidgetInterfaceKit 8/8/8</a> as it is the most flexible and extensible. This article will assume you are using this kit.</li>
<li>One LED to test the kit with.</li>
</ul>
<p>For this article I will be using <a href="http://macromates.com/">TextMate</a> on a Mac and compiling with the free mxmlc compiler included in the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">Flex 4 SDK</a>. We will be running the example via Adobe AIR (via ADL included in the Flex 4 SDK) (although the SWF we create will also work in the Flash Player in the browser).</p>
<p><strong>Install Drivers</strong></p>
<p>First, download and install the <a href="http://www.phidgets.com/drivers.php">Phidgets drivers for your platform</a>. This will install the drivers to interface with the devices via USB, and will also install a standalone application / server that will proxy calls between Flash and the devices.</p>
<p><strong>Test Installation</strong></p>
<p>Once you have installed the drivers, download  the <a href="http://www.phidgets.com/documentation/Phidgets/1018.pdf">manual for the 1018 &#8211; PhidgetInterfaceKit 8/8/8</a>. I strongly recommend you read through the entire PDF document, but at a minimum, you will want to read the &#8220;Product Features&#8221; and &#8220;Getting Started&#8221;" sections.</p>
<p>Connect your Phidget via USB to your computer and then read and follow the steps under the <em>Getting Started > Testing</em> section in the 1018 docs. This will show you how to ensure that your Phidgets and software are setup and functioning correctly. Make sure to run the sample program as described in the docs. </p>
<p><strong>Download Phidget ActionScript Library</strong></p>
<p>In order to communication with the Phidgets devices / proxy application via ActionScript, we need to download the Phidgets ActionScript library. There are two Flash related libraries on the <a href="http://www.phidgets.com/programming_resources.php">Phidgets Programming Resources page</a>. The Flash AS3 download contains example FLAs to be run in Flash Authoring (4 or 5), as well as the source code for the Phidgets ActionScript library. The Flex AS3 download contains both Flex / MXML and pure ActionScript examples, as well as the Phidget AS3 Library SWC. You can also download API references and a getting started guide on the page.</p>
<p>You can use either the Phidget library source from the Flash AS3 download, or the Phidget library SWC from the Flex AS3 download. Just make sure to link them in when compiling your ActionScript. For this article I will be using the compiled SWC.</p>
<p><strong>Configure the Phidget</strong></p>
<p>For our simple hello world example, we are going to connect an LED to our Phidget and then turn it on via ActionScript. In order to do this, we need to connect an LED to the PhidgetInterfaceKit. Connect an LED to output pins G (Ground) and 0. Make sure that the long wire (anode) of the LED is connected to pin 0, and the short wire (cathode) is connect to the ground (G).</p>
<p><a href="/blog/images/phidgets/phidget_led.jpg" rel="lightbox[phidgets]" title="Interface Board with LED Attached"><img src="/blog/images/phidgets/phidget_led_sm.jpg"  width="400" height="290"/></a></p>
<p>Next, make sure that the Phidget is connected to the computer via USB. You can test that everything is connected and hooked up correctly by opening the Phidget preferences, and selecting output 0 on the Phidget Interface Kit screen. This should turn on the LED (make sure to turn it off once you are done).</p>
<p><a href="/blog/images/phidgets/phidget_mac_settings.png" rel="lightbox[phidgets]" title="Interface Kit Screen in Preferences"><img src="/blog/images/phidgets/phidget_mac_settings_sm.png"  width="500" height="452" /></a></p>
<p><strong>Turn on Phidget Proxy / Server</strong></p>
<p>The last step before we can write our code is to make sure that the Phidget proxy / web server is running. This is a simple socket server that runs locally and proxies calls between Flash and the Phidget.</p>
<p>On Mac, you can start this in <em>System Preferences > Phidgets > Web Service > Start Web Service</em>. Check the <a href="http://www.phidgets.com/documentation/Tutorials/Working_With_PhidgetWebservice.pdf">documentation</a> for how to start it on other platforms. </p>
<p>For now you can leave the ServerID and Password blank, and leave the port at its default value of &#8220;5001&#8243;.</p>
<p><strong>Write ActionScript Code</strong></p>
<p>We are finally ready to write the ActionScript code to interface with the Phidget.</p>
<p>Open you ActionScript editor, and type in the following code in a file named PhidgetsHelloWorld.as. The code below is fully commented and explains what is going on.</p>
<pre><code>/*
	The MIT License

	Copyright (c) 2010 Mike Chambers

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

package
{
	import flash.display.Sprite;
	import flash.events.Event;

	//import the Phidget classes we need for this example
	//from the Phidget's library / SWC
	import com.phidgets.Phidget;
	import com.phidgets.PhidgetInterfaceKit;
	import com.phidgets.events.PhidgetEvent;	

	public class PhidgetsHelloWorld extends Sprite
	{		

		//reference to the Phidget Interface Kit we are connecting to
		private var interfaceKit:PhidgetInterfaceKit;

		//constructor
		public function PhidgetsHelloWorld()
		{
			//listen for when we are added to the stage
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

			//create instance of PhidgetInterfaceKit
			interfaceKit = new PhidgetInterfaceKit();

			//listen for event that we need for this example

			//connected to proxy
			interfaceKit.addEventListener(PhidgetEvent.CONNECT,	onConnect);

			//device attached
			interfaceKit.addEventListener(PhidgetEvent.ATTACH,	onAttach);

			//device detached
			interfaceKit.addEventListener(PhidgetEvent.DETACH,	onDetach);

			//disconnected from proxy
			interfaceKit.addEventListener(PhidgetEvent.DISCONNECT, onDisconnect);

			//proxy address. Usually localhost / 127.0.0.1, but can be any server
			//on any computer. The proxy returns proper security policy file
			var serverAddress:String = "127.0.0.1";

			//port the proxy is listening on
			var serverPort:uint = 5001;

			//connect to the proxy and device
			interfaceKit.open("127.0.0.1", 5001);
		}

		//called when we connect to the proxy server
		private function onConnect(e:PhidgetEvent):void
		{
			//note, we cannot access any devices here yet, since
			//we have not connected to them. You have to wait for the
			//onAttach event
			trace("-------onConnect------- ");
		}

		//called when we attach / connect to the actual Phidget
		private function onAttach(e:PhidgetEvent):void
		{
			trace("-------onAttach------- ");

			//get a reference to the device we connected to
			var device:Phidget = e.Device;

			//print out a bunch of information about it
			trace(device.Name + " : " + device.Label);
			trace("Version : " + device.Version);
			trace("Serial : " + device.serialNumber);

			//output number / pin that the LED is connected to
			var outputPin:uint = 0;

			//state to set the output pin. true = on, false = off
			var state:Boolean = true;

			//set the output state for pin 0 to true / on.
			//this will turn on the LED
			interfaceKit.setOutputState(outputPin, state);

			//note, you have to explicitly turn it off, or else
			//it will stay on, even after the program has stopped running
		}

		//called when a device is detached. i.e. the USB cable is disconnected
		//while the program is running
		private function onDetach(e:PhidgetEvent):void
		{
			//you cannot access the devices here.
			trace("-------onDetach------- ");
		}		

		//called once the connection to the proxy server is closed.
		private function onDisconnect(e:PhidgetEvent):void
		{
			//you cannot access the devices here
			trace("-------onAttach------- ");
		}
	}
}</code></pre>
<p>&nbsp;</p>
<p>I am going to run this via ADL (Adobe AIR) included in the Flex SDK, and thus also need the following PhidgetHelloWorld-app.xml file:</p>
<pre><code>&lt;?xml version =&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;application xmlns=&quot;http://ns.adobe.com/air/application/2.0&quot;&gt;
	&lt;id&gt;PhidgetsHelloWorld&lt;/id&gt;
	&lt;version&gt;1.0&lt;/version&gt;
	&lt;filename&gt;PhidgetsHelloWorld&lt;/filename&gt;
	&lt;description&gt;&lt;/description&gt;
	&lt;name&gt;PhidgetsHelloWorld&lt;/name&gt;
	&lt;copyright&gt;&lt;/copyright&gt;
	&lt;initialWindow&gt;
		&lt;content&gt;PhidgetsHelloWorld.swf&lt;/content&gt;
		&lt;systemChrome&gt;standard&lt;/systemChrome&gt;
		&lt;transparent&gt;false&lt;/transparent&gt;
		&lt;visible&gt;true&lt;/visible&gt;
		&lt;fullScreen&gt;false&lt;/fullScreen&gt;
		&lt;autoOrients&gt;false&lt;/autoOrients&gt;
		&lt;aspectRatio&gt;portrait&lt;/aspectRatio&gt;
		&lt;renderMode&gt;auto&lt;/renderMode&gt;
	&lt;/initialWindow&gt;
	&lt;customUpdateUI&gt;false&lt;/customUpdateUI&gt;
	&lt;allowBrowserInvocation&gt;false&lt;/allowBrowserInvocation&gt;
	&lt;icon&gt;
	&lt;/icon&gt;
&lt;/application&gt;
</code></pre>
<p>&nbsp;</p>
<p>I am running it via ADL / Adobe AIR as it will easily allow me to see the trace output on the command line.</p>
<p>Here is the command to compile the program using MXMLC:</p>
<p><code>mxmlc -library-path+=Phidget21Library.swc PhidgetHelloWorld.as</code></p>
<p>&nbsp;</p>
<p>You may receive a warning about runtime shared libraries. You can safely ignore this.</p>
<p>If you get an error that mxmlc cant be found, then make sure you have added the FLEXSDK/bin directory to your system&#8217;s path.</p>
<p>If you receive any errors, make sure you are linking in the Phidget library correctly. Once it compiles, you can test it on the command line with the following command:</p>
<p><code>adl PhidgetHelloWorld-app.xml</code></p>
<p>&nbsp;</p>
<p>When I run this on my machine the LED turns on on the Phidget, and I get the following output on the command line:</p>
<pre><code>-------onConnect-------
-------onAttach-------
Phidget InterfaceKit 8/8/8 :
Version : 900
Serial : 116799</code></pre>
<p>&nbsp;</p>
<p>If you run the SWF in the player, then you should see the LED turn on. If you run in Flash Authoring (FLA included) or FlashBuilder, then you should also see the trace output in the output panel.</p>
<p>Notice that once you close the program, the LED is still on. You have to explicitly turn off the LED. You can do this by modifying the code to turn it off, and then run it again.</p>
<p>This is an important point. The device may maintain its state even after the program has stopped running. Because of this, you should always set the device to a known state when your application starts up, and make sure it is in the correct state when you shut down your program.</p>
<p>Once you are at this point, it is just a matter of hooking up additional sensors, motors and outputs. The Phidgets ActionScript library is very well written, and makes it easy to work with additional Phidgets.</p>
<p>Make sure to check out all of the Phidgets and ActionScript related <a href="http://www.phidgets.com/programming_resources.php">docs</a> on the Phidgets site, as well as the <a href="http://www.phidgets.com/phorum/viewforum.php?f=10">Phidgets ActionScript forum</a>.</p>
<p>If you find any errors, or have any questions or suggestions, then please leave them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/07/26/getting-started-with-phidgets-and-actionscript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>FITC San Francisco</title>
		<link>http://www.mikechambers.com/blog/2010/07/15/fitc-san-francisco/</link>
		<comments>http://www.mikechambers.com/blog/2010/07/15/fitc-san-francisco/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 18:42:56 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2116</guid>
		<description><![CDATA[I am really excited about the FITC San Francisco Flash conference, coming up in about a month. First, it is a FITC conference which means all of the top Flash designers and developers in the world will be speaking and hanging out. But more significantly, it is FITC in SAN FRANCISCO, the first major Flash [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F15%2Ffitc-san-francisco%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F15%2Ffitc-san-francisco%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I am really excited about the <a href="http://www.fitc.ca/events/about/?event=110">FITC San Francisco</a> Flash conference, coming up in about a month. First, it is a FITC conference which means all of the top Flash designers and developers in the world will be speaking and hanging out. But more significantly, it is FITC in SAN FRANCISCO, the first major Flash conference in San Francisco in quite a while. As you probably know San Francisco is the <a href="http://maps.google.com/maps?f=q&#038;source=s_q&#038;hl=en&#038;geocode=&#038;q=601+Townsend+Street,+San+Francisco,+CA&#038;sll=37.771425,-122.402023&#038;sspn=0.043082,0.072098&#038;gl=us&#038;ie=UTF8&#038;hq=&#038;hnear=601+Townsend+St,+San+Francisco,+California+94107&#038;ll=37.770918,-122.401664&#038;spn=0.011195,0.018024&#038;z=16">home</a> to the Flash authoring and player teams, as well as some of the top creative agencies and developers in the world. This makes it an amazing opportunity to not only see some of the coolest stuff going on in Flash today, but also hang out with all of the people making it happen.</p>
<p>I am doing a <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1230">session</a> talking about some of the lessons I learned around mobile optimizations while working on my <a href="http://www.mikechambers.com/blog/2010/07/14/source-code-for-pewpew-released/">PewPew</a> game and other mobile projects over the past year or so. This will focus primarily on Android development, but is relevant for other Flash Player 10.1 mobile platforms, as well as desktop development.</p>
<p>So, what sessions and speakers am I most looking forward to?<br />
<span id="more-2116"></span><br />
<strong>Adobe Keynote</strong> : Kevin Lynch, CTO of Adobe, is driving the keynote this year, which means that we will get some deep insight to how Adobe views Flash&#8217;s role on the web, both today, and in the future.</p>
<p><strong>Mario Klingemann</strong> : <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1208">Welcome to the Sideshow</a> : Mario is doing some of the most <a href="http://www.flickr.com/photos/quasimondo/">amazing stuff</a> on the web today, and I think he has been giving the best talks of anyone else in 2010. This is a must see session.</p>
<p><strong>Yugo Nakamura</strong> : <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1209">Long Time No See</a> : Yugo is one of my original Flash heroes who really got me into Flash. I havent seen him speak in years, so I am looking forward to seeing what he has been working on.</p>
<p><strong>Andre Michelle</strong> : <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1217">Pulsatile Crackle</a> : As one of the developers behind <a href="http://www.audiotool.com">AudioTool.com</a>, probably the single most advanced application on the web today, this is a session you cant miss. Couple that with the fact that Andre continues to impress by creating dynamic and beautiful audio devices and experiments within Flash, and this session is a no brainer.</p>
<p><strong>Ralph Hauwert</strong> : <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1214">Skunkworks</a> : I have to be honest and admit I dont understand how Ralph pulls of most of the graphics programming and projects that he does, but it still makes for an amazing session that shows off just how far the Flash player can be pushed today.</p>
<p><strong>Erik Natzke</strong> : <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=110&#038;presentation_id=1211">Art of Play</a> : Natzke simply makes beautiful things. You cant go wrong with this session where he talks about his creative process and evolution.</p>
<p>You can find more information on FITC San Francisco, as well as purchase tickets on the <a href="http://www.fitc.ca/events/about/?event=110">FITC San Francisco site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/07/15/fitc-san-francisco/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Source code for PewPew Released</title>
		<link>http://www.mikechambers.com/blog/2010/07/14/source-code-for-pewpew-released/</link>
		<comments>http://www.mikechambers.com/blog/2010/07/14/source-code-for-pewpew-released/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 18:43:36 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2110</guid>
		<description><![CDATA[I have finally released the source code for my PewPew game. PewPew is a game I started working on over a year ago to help me understand what optimizations would be required to convert a web based Flash game to run on the iPhone. I had planned to release the code earlier, but a little [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F14%2Fsource-code-for-pewpew-released%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F07%2F14%2Fsource-code-for-pewpew-released%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I have finally released the <a href="https://github.com/mikechambers/pewpew">source code for my PewPew game</a>. PewPew is a game I started working on over a year ago to help me understand what optimizations would be required to convert a web based Flash game to run on the iPhone. I had planned to release the code earlier, but a little <a href="http://www.mikechambers.com/blog/2010/04/20/on-adobe-flash-cs5-and-iphone-applications/">hiccup</a> around iPhone support delayed that. Now that <a href="http://labs.adobe.com/technologies/air2/android/">Adobe AIR for Android is in public pre-release</a>, and I have had time to comment all of the code, it is ready to be released.<br />
<span id="more-2110"></span><br />
<a href="http://github.com/mikechambers/pewpew">Download PewPew Source from GitHub</a></p>
<p>First, a couple of notes about the code and game.</p>
<p>This is not a complete, ready to ship game. The game engine is pretty solid, but some more work needs to be done on adding more complexity / new enemies at high levels. In general, the menus and UI needs a lot of polish, and support added for high scores, profiles, etc&#8230;</p>
<p>This was the first game I have written in ActionScript 3 (my last game was written in Flash 4), and to be honest, I think it is a bit over engineered. I wrote the entire game from scratch, not using any existing frameworks. I did spin off some of the reusable code in my own <a href="http://github.com/mikechambers/Simple-Game-Framework">Simple Game Framework</a>, but that is really only useful as sample code, or for putting together some quick ideas. For my next game I build, I will be using an existing framework, and will probably not rely so heavily on inheritance (it really adds to the complexity of the code as the game is developed).</p>
<p>In addition, this code has been worked on and optimized (and re-optimized) during the entire development cycle of AIR for iPhone (started before engineering work had even begin on the project). Most of the code has been updated and optimized to work on the release runtimes, but there may be some weird code in there left over from obsolete optimizations. Again, the code is fully commented, and I have put notes where there are optimizations, or why I may have done something in a weird way.</p>
<p>The game was originally developed for a desktop web browser, with mouse input. However, I eventually changed game input to work better on touch devices (the original mouse input worked, but wasn&#8217;t optimal for touch screens). Most of the code for mouse optimized input is still in the code base, but is commented out or not used.</p>
<p>Currently, the game assets are compiled via Flash CS5 (in an FLA). It would not be too much work to make this an ActionScript only project, although having it in FLA / CS5 makes it pretty easy to work with the graphics and layout.</p>
<p>The current version is sized for running on a Nexus One Android device (I need to add dynamic layout support to run better on multiple devices). The code also has full support for hardware acceleration on the iPhone and AIR for Android (although i need to test it again on Android).</p>
<p>The source code for PewPew is released under an <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>. The current version is set to run on AIR 2.0. I am working on an issue that is preventing it from running on AIR 2.5 for Android and as soon as it is resolved, I will update the source to target AIR 2.5. </p>
<p>Again, the code is released under an MIT license, so you are free to do whatever you want with it, including building and releasing your own games. I am open to taking changes / patches / submissions directly into the project, so if you want to make some changes, then read the information on <a href="http://help.github.com/forking/">how to fork a project and submit changes on GitHub</a>. </p>
<p>I plan on doing a separate post talking about the optimization and changes that I had to make over the development cycle of PewPew, first to convert it from browser based content, to mobile, and then to ensure that I got good performance on the generally slower mobile devices.</p>
<p>Anyways, have fun with the code, and I hope that people find it useful (even if it is just as a learning tool). Feel free to post any comments, suggestions in the comments here, and if you find any bugs, then <a href="https://github.com/mikechambers/pewpew/issues">log an issue on the project&#8217;s github page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/07/14/source-code-for-pewpew-released/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>New Release of as3corelib Library</title>
		<link>http://www.mikechambers.com/blog/2010/05/28/new-build-of-as3corelib-library/</link>
		<comments>http://www.mikechambers.com/blog/2010/05/28/new-build-of-as3corelib-library/#comments</comments>
		<pubDate>Fri, 28 May 2010 19:50:23 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[as3corelib]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=2102</guid>
		<description><![CDATA[I have just posted a new release (.93) of as3corelib. The corelib project is an ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript? 3. You can view a complete list of changes in the change list (items 75 &#8211; 106). You can download the updated SWC and source [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F05%2F28%2Fnew-build-of-as3corelib-library%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2010%2F05%2F28%2Fnew-build-of-as3corelib-library%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I have just posted a <a href="http://code.google.com/p/as3corelib/downloads/list">new release (.93) of as3corelib</a>. The corelib project is an ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript? 3. </p>
<p>You can view a complete list of changes in the <a href="http://code.google.com/p/as3corelib/source/list?num=75&#038;start=116">change list (items 75 &#8211; 106)</a>.</p>
<p>You can download the updated SWC and source from the <a href="http://code.google.com/p/as3corelib/downloads/list">project&#8217;s download page</a>.</p>
<p>Thanks to <a href="http://blogs.adobe.com/cantrell/">Christian Cantrell</a> for helping build the release.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2010/05/28/new-build-of-as3corelib-library/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
