Arduino and Flash: modify your existent Flash games to work with a joystick
After you have configured your Arduino board to properly work with your Flash applications you could quickly update your existent Flash or Flex games to get input from a joystick.
I have modified three prototypes, 2 cars games and 1 platform, to work with a mini-stick, spending no more than 5 minutes to update each game.
Watch the following video to understand what I have done and check the related content at the end of this article to get more info about Arduino and Flash connection.
Thanks to Flash Game University to allows me using their games from the book Actionscript 3.0 Game Programming University
To put in place this stuff I only added the following code:
1) Initializing the Arduino Event listeners and managing the socket connection
import net.eriksjodin.arduino.Arduino; import net.eriksjodin.arduino.events.ArduinoEvent; import net.eriksjodin.arduino.events.ArduinoSysExEvent; import flash.utils.ByteArray; import flash.events.Event; var a:Arduino; var numEvents:Number=0; // connect to a serial proxy on port 5331 a = new Arduino("127.0.0.1", 5335); // com 5 (First Arduino Board) //a = new Arduino("127.0.0.1", 5336); // com 6 (Second Arduino Board) // listen for connection a.addEventListener(Event.CONNECT,onSocketConnect); a.addEventListener(Event.CLOSE,onSocketClose); // listen for firmware (sent on startup) a.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onReceiveFirmwareVersion); // listen for data a.addEventListener(ArduinoEvent.ANALOG_DATA, onReceiveAnalogData); a.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData); //listen for sysex messages a.addEventListener(ArduinoSysExEvent.SYSEX_MESSAGE, onReceiveSysExMessage); // triggered when a serial socket connection has been established function onSocketConnect(e:Object):void { trace("Socket connected!"); // request the firmware version a.requestFirmwareVersion(); } // triggered when a serial socket connection has been closed function onSocketClose(e:Object):void { trace("Socket closed!"); } // trace out data when it arrives... function onReceiveDigitalData(e:ArduinoEvent):void { trace((numEvents++) +" Digital pin " + e.pin + " on port: " + e.port +" = " + e.value); } // trace incoming sysex messages function onReceiveSysExMessage(e:ArduinoSysExEvent) { trace((numEvents++) +"Received SysExMessage. Command:"+e.data[0]); } // the firmware version is requested when the Arduino class has made a socket connection. // when we receive this event we know that the Arduino has been successfully connected. 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."); } // the port value of an event can be used to determine which board the event was dispatched from // this is one way of dealing with multiple boards, another is to add different listener methods trace("Port: " + e.port); // do some stuff on the Arduino... initArduino(); } function initArduino():void { trace("Initializing Arduino"); a.setAnalogPinReporting(4, Arduino.ON); a.setAnalogPinReporting(5, Arduino.ON); }
2) managing the car movement with a joystick, changing the existent keyboard scripts with the following:
// Control Car movement from Joystick private function onReceiveAnalogData(e:ArduinoEvent):void { // Up/Down if (Number(e.pin == 5)) { if (a.getAnalogData(5) > 550) { arrowUp = false; arrowDown = true; } else if (a.getAnalogData(5) < 500) { arrowUp = true; arrowDown = false; } else { arrowUp = false; arrowDown = false; } } // Left/Right if (Number(e.pin == 4)) { if (a.getAnalogData(4) > 550) { arrowLeft = false; arrowRight = true; } else if (a.getAnalogData(4) < 500) { arrowLeft = true; arrowRight = false; } else { arrowLeft = false; arrowRight = false; } } }
Naturally you could improve my code to manage speed getting values from the joystick positions; using two sticks, one for horizontal movement and one for vertical; using buttons to accellerate and brake and so on.
GET GAME SAMPLES
You can get all sample code buying the book Actionscript 3.0 Game Programming University.
RELATED CONTENT
1) Arduino and Flex: connect a joystick to your application
2) Arduino: control a LED and one servo motor using a joystick
3) Arduino and Flash: control your Flash applications from external hardware









hello,
vedo che la tua nuova passione cresce e da nuovi frutti ogni giorno. Complimenti.
Ciao, Roberto.
Yes yes. I love it : )
Hello!
I am looking for an example where I can print/send a string from arduino to flash and vice-versa.
Arduino is set with firmata2.
Do you think I must change the code in arduino so it can send string messages to flash, or there is a way to make it within flash?
thanks in advance
No changes on your sketch file are required.
You can use one of firmata 2 firmwares (check my samples to see what i have used) in your Arduino and creare your Flash/Flex application using for example the AS3 Glue library (http://code.google.com/p/as3glue/)
Furthermore you also need to use a socket proxy to share data between Flash and Arduino using the XML socket flash class (you can get a good serial proxy aplication following the AS3 Glue Library reference).
Naturally, you could creare your own AS3 classes using XML Socket but AS3GLUE is ready to work allowing to send analog / digital input from Flash to Arduino and viceversa (I tested it and it works well) and use servos (but i had some problems ). This library seems not final and it’s a bit limited for complex stuff but I should spend more time to test it (i’m sorry but my real job is a Flash/Flex developer and electronic stuff at the moment it’s only a passion and I can work on it only during my free time)
Hey Fabio, thanks for the comments!
Do you know the funnel class for flash? thats really amazing to see the video. Type: funnel arduino getting started at google and watch the video.
Well. I forgot to say I am already using firmata2 and the glue class in flash
I have tested some samples with buttons, led, or, digital in/outputs and its all ok.
The problem is while trying to send messages.
Let me put a very basic example:
supose this code running in arduino:
———————–
int id = 3; //could be any number
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print(id);
}
————————
if you go to Arduino serial monitor you will see it printing (3 3 3 3 3 ….)
now, wonder flash reading serial (with serproxy, for example) and tracing it (3 3 3 3…) inside a textBox.
in other words, I just would like to get the printed values in serial in flash and vice-versa.
Supose there is a way flash can print a value to serial too.. and Arduino would get this to turn a led on.
(I know I could send it using digitalWrite in this example, but in my case I must send a ‘int’ or ’string’ from flash, like a message)
for example:
——————————
//in flash
arduino.sendString(3);//here is my int
——————————
//in arduino
if (Serial.read() == 3){
//TURN LED 3 ON
}
——————————
what do you think!?
Thanks one more time!
ciao!
Hi Anderson, i have tried the same stuff without success with AS3Glue but honestly i didn’t waste a lot of time about it.
With AS3Glue i have also tried the class ArduinoWithServo but it didn’t work for me so this library is a cool idea but seems still limited.
In the past I tried some actionscript XML socket class and I remember that i was able to send msg from arduino to Flash and viceversa (but it was AS2).
So i think it’s possible but i would like to build my own class in AS3 to be free to do what i want.
I’m only afraid that I don’t know a lot of technical stuff about arduino communication.
Checking AS3Glue class (very simple) I see in the code some addresses that I don’t understand so i’m not sure to be able to accomplish this task.
I have to say that at the moment, arduino for me is only a nice game and I can’t investigate about these issues spending a lot of hours.
But at the same time I would like to do it… if you have some ideas and you want test or share something with me, i’m 100% available : )
Fabio
I wrote a new post about Flex and Arduino communication using As3 XML Socket: http://www.fabiobiondi.com/blog/2009/11/arduino-flex-3-and-xmlsocket-class/
Honestly i have new ideas from that post and I think I will do other tests in the next weeks.
Leave your response!
Archives
Categories
Blogs
3D ActionScript Adobe AIR AIR for Android android Arduino Binding burrito hero 4.5 TabbedMobileApplication Chat Chrome Cocomo code optimization components custom/generic components custom components DataList developer central Emitter FLARToolkit Flash Flash Builder Flex HTTPService illustrator Infrared Interactions itemRenderer Joystick Loader mobile Papervision PHP Rss Servo singleton Skin Skinning Transitions Tween URLLoader URLRequest Vertical Scrollbar VScrollbar youtube
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
Recent Posts
Develop Mobile apps with Catalyst?
Code optimization
TextInput Component
Interactions and Transitions
Most Commented
Recent Comments
how to call methods between swf files