Arduino & Flash AS3 (part 4)

(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.

8 thoughts on “Arduino & Flash AS3 (part 4)

  • Pingback: Track Hacker

  • Pingback: Track Hacker

  • May 4, 2011 at 12:35 pm
    Permalink

    When I test it in Flash, the outliner says “Socked Connected!
    Socked Closed!”

    My Terminal running TinkerProxy says:

    Failed to open comm port – connection refused

    Any idea what’s going wrong here?

    Reply
    • May 9, 2011 at 9:09 pm
      Permalink

      Hi Dan,
      I need more info to help you diagnose your particular issue. Did you follow the steps in my tutorial? How far did you get before you noticed the issue? Which version of Arduino are you using? Which Arduino board are you using? Which version of the Firmata do you have running?
      M

      Reply
  • September 16, 2012 at 3:57 pm
    Permalink

    hi Manuel – First, thanks for your how-to posts – this was very helpful for my quick Saturday afternoon Arduino session. I’m glad to see a Flash/AS3 resource like this, as we move on into the post-browser AIR space.

    Just a quick note on something that stumped me briefly, this line was throwing an error:

    if (int(e.value)!=2) {
    trace(“Unexpected Firmware version encountered! This Version of as3glue was written for Firmata2.”);

    Now that the firmware is up to 2.3, this value displays as 23 (for some odd reason). I just adjusted my code to this, and everything proceeded smoothly.

    if (int(e.value)!=23) {…

    Thanks and keep on posting!

    Reply
  • February 1, 2013 at 4:04 pm
    Permalink

    Hello, first of all, thank you for the excellent tutorial. At this stage when I export the movie in Flash, I get “Socket Connect!” and nothing more.
    I have the board Mega 2560, Arduino 1.0.3, and already tested with Firmata 2.2 and 2.3.3. Version 2.1 gives a series of errors, can you help me?

    Reply
    • February 28, 2013 at 1:02 pm
      Permalink

      Hi Luis,
      Unfortunately I do not own the Mega 2560, so I can’t really help you debug the connection. What I recommend is the following:
      (1) Read this and see if it applies to your issues. http://code.google.com/p/as3glue/wiki/FAQ
      (2) Start adding trace statements to the AS3Glue class to see where it is failing.

      Goodluck!

      Reply
  • December 11, 2016 at 11:21 pm
    Permalink

    Hello!
    I followed the tutorial using an Arduino Uno and the error message showed up:

    Error #2044: Unhandled IOErrorEvent:. text=Error #2031: Socket Error.
    at Prototype_1/initArduino()
    at Prototype_1()

    Do you have an idea of how I can fix the error?

    Thank you!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: