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 tinker.it) 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 SerialPort.as 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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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("127.0.0.1", 5331);
arduino.addEventListener(DataEvent.DATA, onArduinoData );
arduino.connect();

// 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);
timer.start();

// 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
    }
    else
    { // 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);
    addChild(ball);
}

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

    /* The values from Arduino Messenger are space seperated values. We will
       store each value in the receivedValues array.    
    */

    receivedValues = event.data.split(" ");
   
    // 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; 
    }  
}