Arduino Flash communication AS3 – Messenger

Example how to setup serial communication between Flash and Arduino in combination with the Messenger library for Arduino. You can send whatever you want between Arduino and Flash (if you only want to receive data from inputs and set outputs it better to use Firmata / As3Glue combination).

For receiving of data on the Arduino I’ll choose the Messenger library. It has some nice simple functions and gives you the opportunity to check if a string is fully received (a ‘carriage return’ is send by Flash at the end of a serial message in the SerialPort class) by Arduino. Of course you can write your own serial protocol. You can find some thoughts about it at the Todbot blog : Arduino serial protocol design patterns.

You can send data to the serial port with the Serial.print() and Serial.println() commands.

Included is the SerialPort class for Flash that makes it easy to send and receive serial data from/to the Arduino. I’ve modified the original (from so it supports start and stop characters. As a stop character ‘carriage return’ is used (‘\r’, or char(13)) that is send when you do Serial.println(); from Arduino.

I advise using a start character (not used in the example below) as well, because sometimes (especially when sending on a high datarate) characters at the beginning if the string can get lost.

The download contains and example_messenger.fla (CS3).

Download : flash-arduino-as3-messenger

Step by step guide for Messenger library communication

  • Download the Messenger library and install it in your Arduino/libraries/ folder ( you can find it by default in your documents folder ). Restart Arduino if it was running.
  • Open basic_communication_old in my zip-file (basic_communication included in the Messenger examples has been changed).
    I’ve modified the old example in two ways. I’ve changed the serial speed to a baudrate of 57600, so you don’t have to change the Serproxy.cfg if you use it with Firmata as well and I have change the update interval to 40ms (instead of 20ms) for more stability.
  • Upload the Sketch to the Arduino.
  • Configure Serialproxy for your Arduino board and run it.
  • Open example_messenger.fla and run it.
  • If everything went fine you’ll see the led on pin 13 blinking and 6 balls, controlled by the analog inputs, moving on the screen.

Example_messenger code

var receivedValues:Array = new Array();

// Arduino connection

/* SerialPort(localhost, netport, start character, stop character)
   for more stability use a startcharacter so you can check if a message is complete
   default stop character is carriage return '\r' 
   A carriage return is send by the Arduino command Serial.println();

var arduino:SerialPort = new SerialPort("", 5331);
arduino.addEventListener(DataEvent.DATA, onArduinoData );

// Setup a timer to send data to the Arduino
// The timer object calls the timerEvent function 20 times a second (every 50ms)
var timer = new Timer(500);
timer.addEventListener("timer", timerEvent);

// toggle variable that is switched by the timer from true to false and back
var toggle:Boolean= true;

function timerEvent(event:Event):void 
    // change toggle boolean to the opposite
    toggle = !toggle;
    if(toggle == true)
    { // make ledPin 13 HIGH - onboard led on 
      //arduino.send("on"); // For Messenger checkString example
      arduino.send("13 1"); // For Messenger basic_communication example
    { // make ledPin 13 LOW - onboard led off
      //arduino.send("off"); // For Messenger checkString example
      arduino.send("13 0");  // For Messenger basic_communication example
    When you have a certain amount of values it will be smart to keep them
    in an array (so you only modify the array indexes that change.
    You can convert an array to a character seperated string (space character for 
    Messenger library) with the join function.
    Example :
    var sendValues:Array = new Array(2,4,8,16,32,64,128);
    arduino.send(sendValues.join(" "));

// add some balls to the stage to display incoming analog values
var ballArray = new Array();

for(var i:int = 0;i<6;i++)
    var ball:MovieClip = new Ball();
    ballArray[i] = ball;
    ball.width = ball.height = 50;
    ball.y = 10;
    ball.x = 10+(i*60);

// Called when data is received from the Arduino
function onArduinoData( event:DataEvent ):void
    trace("received string : ", );

    /* The values from Arduino Messenger are space seperated values. We will 
       store each value in the receivedValues array.    
    receivedValues =" "); 
    // change the y position of the balls on stage 
    for(var i:int = 0;i<6;i++)
    { var mc = ballArray[i];
      mc.y = receivedValues[i] * 350/1023;  

15 thoughts on “Arduino Flash communication AS3 – Messenger”

  1. Hi dear friend. I did exactly what you wrote on here and my arduino mega board didnt give me any response, no blinking no moving balls. Actually everythings are OK that serproxy.exe is working correctly as well as flash CS4.
    Thanks you will reply…

  2. I don’t know if the Messenger library works good on the Mega. Try to find an Arduino duemilanove board, to see if that works. I don’t have a Mega to test it now.

  3. Hi Kasper,

    Thanks so much for this tutorial, it’s helped me so much with my project, which you can view here, if you’re interested:

    I need to re-write my code very slightly and I’m not an expert on this – actionscript or arduino – so I was hoping you could help?!

    In one of the frames in my Flash animation, I use the actionscript code to send a signal to the arduino output:


    This works fine, but what I would like is for this to then delay for 1/12 second and then to switch off again. At the moment I am trying this code below, but it doesn’t like the ‘Delay’ command. Do you have any suggestions as to what I should write instead? Many thanks, Ellie Harrison

    delay(83); // wait for 1/12 second

  4. Hi Ellie,

    I thinks you used the Firmata example in this post or not?

    In Actionscript 3 you could use thesetTimeOut function or a Timer that runs one time (I’ve linked to the actionscript language reference).

    If you used the messenger script you could also program it in Arduino code with an interval: See programming interval or the blink without delay example in the Arduino IDE.

  5. Hi Kasper, Thanks so much for your help. Yes, sorry I posted on the wrong page – I was mainly following the Firmata example and attempting to my code in Actionscript 3.0 (though I do not really know what I am doing)!

    At the moment I am importing the following libraries in the 1st frame of the animation, where I also have your ‘Flash – Arduino Example script’ pasted:

    import flash.utils.getTimer;
    import flash.display.Sprite;

    Then on the frame in the animation where I am sending the signal to the Arduino I currently have this:

    // Set Arduino outputs

    var myTimer:Timer = new Timer(1000, 1); // 1 second
    myTimer.addEventListener(TimerEvent.TIMER, runOnce);

    function runOnce(event:TimerEvent):void {
    trace(“runOnce() called @ ” + getTimer() + ” ms”);

    // Set Arduino outputs

    I copied this code from another example I found online of the Timer function. At the moment this is not returning any errors, but it also not causing any delay between the output pin being on and off – it flashes momentarily and not for the 1 second delay I thought I had set above.

    If you have any suggestions about what I’m going wrong, that would be helpful! Does the frame rate of the movie affect this at all?

    Thanks again for your help, Ellie

  6. Hello, when compiling these errors appears

    basic_communication:14: error: ‘Messenger’ does not name a type
    basic_communication.pde: In function ‘void messageCompleted()’:
    basic_communication:21: error: ‘message’ was not declared in this scope
    basic_communication.pde: In function ‘void setup()’:
    basic_communication:32: error: ‘message’ was not declared in this scope
    basic_communication.pde: In function ‘void loop()’:
    basic_communication:38: error: ‘message’ was not declared in this scope

  7. I used your ActionScript 3.0 code to fetch the variable in the arduino with a code in arduino did and it worked, thanks for the help. When posting on my blog post here too.

    Once again thank you.

  8. In first time thanks kasper for all you do…
    but that the same thing for me :
    basic_communication:14: error: ‘Messenger’ does not name a type
    basic_communication.pde: In function ‘void messageCompleted()’:
    basic_communication:21: error: ‘message’ was not declared in this scope
    basic_communication.pde: In function ‘void setup()’:
    basic_communication:32: error: ‘message’ was not declared in this scope
    basic_communication.pde: In function ‘void loop()’:
    basic_communication:38: error: ‘message’ was not declared in this scope

  9.  Thanks Kasper..
    That is right, After trying to drop all my file missing in the librairy i have a new error :
    C:Documents and SettingsArnoMes documentsArduinolibrariesMessengerMessenger.cpp:8:22: error: WProgram.h: No such file or directory….etc etc
    I have succesfull after rename in “Messenger.cpp”:line 8
    #include “WProgram.h”      >with>        #include “Arduino.h”
    Succesfully…happyness…and  more thanks kasper i hope this soluce, help another people..
    Thanks again kasper you are the best!
    (excuse my english”i m français”)

  10. unfortunately flash(as3)don’t want to accept the communication…Do you have a solution for use messenger librairy with arduino 1.0?i did’nt arrived to transform basic_communication sketch and use it with CmdMessenger !if someone know a solution…
    (excuse my bad english i was french..thank)

  11. You have to dive a bit in the Cmd messenger protocol. Sorry, I cannot help you out because I don’t have time to do that research myself right know.
    Another solution is to work with an older version of Arduino (like 0023). I don’t know if you really need some functions of Arduino 1.0?

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.