The Arduino–Flash communication tutorial can be found here.
While the code for the flash side (action scripts) is as follow:
///////////////////////////
——————————-
fscommand("fullscreen", "true");
Mouse.hide();
//———————————————————————————————————— Basic Arduino
import Arduino;
var port:Number = 5331; // What socket port are we configured to use?
// See the net_port entries from serproxy.cfg
// Create a custom object from Flash's XMLSocket
var a:Arduino = new Arduino(port);
//———————————————————————————————————— Listeners
// You can trigger custom actions by adding listners to the Arduino object. For example, you can setup your
// ActionScript code to run a setup function when Flash successfully connects to the socket listener
aListener = new Object();
aListener.onConnect = function() {
trace("hello world");
}
aListener.onReceiveData = function(evtObj:Object){
//Store the received data in a string
var str = evtObj.data
trace("Arduino says — " + str);
if (vid1._currentframe != 1)
{
trace("key pressed1");
vid1._alpha = 0;
vid1.gotoAndStop(1);
vid2.play();
} else {
trace("key pressed2");
vid1._alpha = 100;
vid1.play();
vid2.gotoAndStop(1);
}
//Tests for a particular message from Arduino, and acts accordingly
/*if (str == "switchOff") {
vid1.play();
vid1._alpha=100;
vid2.gotoAndStop(1);
}
else if (str == "switchOn") {
vid2.play();
vid1._alpha=0;
vid1.gotoAndStop(1);
}*/
}
// Binds your custom listeners to the Arduino object (a)
a.addEventListener("onConnect",aListener);
a.addEventListener("onReceiveData",aListener);
//———————————————————————————————————— That's all!
//keyboard control
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
if (vid1._currentframe != 1) {
trace("key pressed1");
vid1._alpha = 0;
vid1.gotoAndStop(1);
vid2.play();
} else {
trace("key pressed2");
vid1._alpha = 100;
vid1.play();
vid2.gotoAndStop(1);
}
}
}
Key.addListener(keyListener);
——————————-
////////////////////////////
There're 2 major problems that I faced:
1,) using netstream functions, you cannot loop video's seamlessly, because they'll always have to reload the video when it ended before it could be played again, so there'll be about half a second of a lag, even when the file is stored locally.
2.) The arduino communication is based on a .as file stored outside of this code (that's why this is not long at all), however, when I started programming, I chose to try out Action Script 3 — the result is that it wouldn't work because the external .as file is written in AS2 and many syntax were changed only slightly — much enough for me to not be able to translate that whole page into AS3 at that time. Thus, I translated my AS3 code back into AS2, saved it as a AS2 file, it works.
While the code for the flash side (action scripts) is as follow:
///////////////////////////
——————————-
fscommand("fullscreen", "true");
Mouse.hide();
//———————————————————————————————————— Basic Arduino
import Arduino;
var port:Number = 5331; // What socket port are we configured to use?
// See the net_port entries from serproxy.cfg
// Create a custom object from Flash's XMLSocket
var a:Arduino = new Arduino(port);
//———————————————————————————————————— Listeners
// You can trigger custom actions by adding listners to the Arduino object. For example, you can setup your
// ActionScript code to run a setup function when Flash successfully connects to the socket listener
aListener = new Object();
aListener.onConnect = function() {
trace("hello world");
}
aListener.onReceiveData = function(evtObj:Object){
//Store the received data in a string
var str = evtObj.data
trace("Arduino says — " + str);
if (vid1._currentframe != 1)
{
trace("key pressed1");
vid1._alpha = 0;
vid1.gotoAndStop(1);
vid2.play();
} else {
trace("key pressed2");
vid1._alpha = 100;
vid1.play();
vid2.gotoAndStop(1);
}
//Tests for a particular message from Arduino, and acts accordingly
/*if (str == "switchOff") {
vid1.play();
vid1._alpha=100;
vid2.gotoAndStop(1);
}
else if (str == "switchOn") {
vid2.play();
vid1._alpha=0;
vid1.gotoAndStop(1);
}*/
}
// Binds your custom listeners to the Arduino object (a)
a.addEventListener("onConnect",aListener);
a.addEventListener("onReceiveData",aListener);
//———————————————————————————————————— That's all!
//keyboard control
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.SPACE)) {
if (vid1._currentframe != 1) {
trace("key pressed1");
vid1._alpha = 0;
vid1.gotoAndStop(1);
vid2.play();
} else {
trace("key pressed2");
vid1._alpha = 100;
vid1.play();
vid2.gotoAndStop(1);
}
}
}
Key.addListener(keyListener);
——————————-
////////////////////////////
There're 2 major problems that I faced:
1,) using netstream functions, you cannot loop video's seamlessly, because they'll always have to reload the video when it ended before it could be played again, so there'll be about half a second of a lag, even when the file is stored locally.
2.) The arduino communication is based on a .as file stored outside of this code (that's why this is not long at all), however, when I started programming, I chose to try out Action Script 3 — the result is that it wouldn't work because the external .as file is written in AS2 and many syntax were changed only slightly — much enough for me to not be able to translate that whole page into AS3 at that time. Thus, I translated my AS3 code back into AS2, saved it as a AS2 file, it works.