(continued from Arduino & Flash AS3 (part 3)).
Bring the NOISE!
This post originally started from a conversation I had with a fellow Flash engineer who attended my past exhibition. In my last exhibition I created an Arduino based synth in which users could interact with. So in keeping with the audio spirit, I decided to show you guys how you could easily control an mp3 in Flash with the Arduino micro-controller and a button. Lets start with the flash files.
Simply create a new fla. and import an mp3 into the library. Right-click the sound in the library and export it for ActionScript. Give it a class name of ‘test_track’ and click OK.
Lets create the document class. I named mine Prototype_1. I’m not going to explain the code in detail, if you are a flash engineer it’s pretty straight forward, here’s the code:
/******************
* Manuel Gonzalez *
* Coding Color *
* www.stheory.com *
*******************/
package {
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import net.eriksjodin.arduino.Arduino;
import net.eriksjodin.arduino.events.ArduinoEvent;
import net.eriksjodin.arduino.events.ArduinoSysExEvent;
import flash.utils.getDefinitionByName;
import flash.events.Event;
public class Prototype_1 extends MovieClip {
private var _arduino:Arduino;
private var _soundArray:Array=new Array("test_track");
private var _currSound:Object;
private var _currChannel:SoundChannel;
private var numEvents:Number=0;
public function Prototype_1() {
initArduino();
}
private function initArduino():void {
_arduino=new Arduino("127.0.0.1",5331);
_arduino.addEventListener(Event.CONNECT,onSocketConnect);
_arduino.addEventListener(Event.CLOSE,onSocketClose);
_arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onReceiveFirmwareVersion);
_arduino.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData);
}
private function onSocketConnect(e:Object):void {
trace("Socket connected!");
_arduino.requestFirmwareVersion();
}
private function onSocketClose(e:Object):void {
trace("Socket closed!");
}
private function onReceiveDigitalData(e:ArduinoEvent):void {
updateApp({pin:e.pin,val:e.value});
}
private function updateApp(in_obj:Object):void {
trace("updateApp pin: " + in_obj.pin + " val " + in_obj.val);
switch (in_obj.pin) {
case 2 :
if (in_obj.val==0) {
startSound();
} else if (in_obj.val == 1) {
stopSound();
}
break;
}
}
private function onReceiveFirmwareVersion(e:ArduinoEvent):void {
trace("Firmware version: " + e.value);
if (int(e.value)!=2) {
trace("Unexpected Firmware version encountered! This Version of as3glue was written for Firmata2.");
} else {
//setUpArduino();
startSound();
}
}
private function setUpArduino():void {
_arduino.enableDigitalPinReporting();
_arduino.setPinMode(2, Arduino.INPUT);
}
private function startSound():void {
var theSound:Class=getDefinitionByName(_soundArray[0]) as Class;
_currSound =new theSound();
_currChannel=_currSound.play(0,999);
}
private function stopSound():void {
flash.media.SoundMixer.stopAll();
}
}
}
The most important method at the moment, is the onReceiveFirmwareVersion(). The method essentially checks the Firmata firmware on the Arduino against the minimum required version AS3Glue needs to communicate with the board. You will notice I call other methods as soon as I have received a valid firmware. Please *Note that I have commented out the call to the setUpArduino() method, we will discuss this method later.
We are going to do a quick test to make sure that the Serial Proxy and Arduino micro controller are communicating.
Navigate to your Serial Proxy application and double click to open it, you should see the following:

Most importantly – Serproxy – (C)1999 Stefano Busti, (C)2005 David A. Mellis – Waiting for clients
Now, navigate back to the Flash ide and test the movie. Ctrl->Enter.
You should see the following in the output window:

If your Flash code compiled and the test_track is in the Library, you should hear the mp3 playing.
Lets move onto the final portion of this tutorial. As mentioned we will be making a physical button to interact with the Flash player.
Finally! Part 5 hooking it all up.
Thanks for posting this article. I’m decidedly frustrated with struggling to search out pertinent and intelligent commentary on this issue. Everybody today goes to the very far extremes to either drive home their viewpoint that either: everyone else in the planet is wrong, or two that everyone but them does not really understand the situation. Many thanks for your concise, pertinent insight.
Using Arduino with Adobe Flash for Model Railroading: Part 2…
I really found your post interesting so I added a trackback to it on my Track Hacker blog
…
I really found your post interesting so I added a trackback to it on my Track Hacker blog
…