Turn up the heat in Flash with Arduino

Flash Arduino

I just got back from a lovely FlashBrighton evening full of Arduino hacking and Wiimote fun.

Arduino is an open-source board that connects to your computer via USB. Add a breadboard, some LEDs, and other components and you can create all sorts of lovely things with it. You program it using the Arduino scripting environment which is based on (and almost identical to) Processing.

I can't wait for my own board and workshop kit to arrive tomorrow. You can order them in the UK from Tinker.it (which is where I ordered mine from).

So, back to tonight's FlashBrighton meet-up.

The evening kicked off with a presentation on the Wiimote and Flash by Kyle Jennings from Berkeley. We heard about how he used the Wiimote and Flash to create a virtual knitting application for Rachel Beth Egenhoefer who has an exbition at Lighthouse Brighton on the 27th of March.

Following Kyle's talk, the group split into two, with Seb presenting an introduction to the Arduino board and those of us with prior knowledge of the board (I'd sat through the introduction at BarcampBrighton2) forming break-out groups to try new things.

Paul and I got together and decided to make our Arduino board talk to Flash. The idea was that we would turn the knob on a potentiometer on the Arduino board to adjust the height of virtual flames in Flash.

Here's how we made the Arduino and Flash talk:

  1. In the setup() method of your Arduino application, set up the serial port: Serial.begin(9600);. 9600 is the baud rate we used.
  2. In the loop() method, read the the analog value from the potentiometer and print it out to the Serial port: Serial.println(knobValue);
  3. Download a serial proxy (we used serproxy, which you can download from the Arduino homepage) to proxy the values received on the serial port to a socket connection.
  4. Configure the serial proxy. For serproxy, we edited the serproxy.cfg file to add a reference to the USB port we had the Arduino connected on (you can get this information from Tools → Serial Port in the Arduino application, for us it was serial_device1=/dev/tty.usbserial-A60049Co). We also set the port's baud settings to 9600 in the config file (comm_baud=9600).
  5. Run serproxy. On my Mac, I chose Open With → Terminal on serproxy. You should see serproxy's welcome message: Serproxy - (C)1999 Stefano Busti, (C)2005 David A. Mellis - Waiting for clients
  6. Finally, we made a binary socket connection in Flash to the port that serproxy was running on (5331).

To make sure that the socket is working correctly, you can quickly download and run the Flashduino Flash demo by Brett Forsyth (just make sure you change the port in the code as that one's made for TinkerProxy, not serproxy).

Here's the Arduino code:

// Potentiometer to flash
int ledPin = 9;                // LED connected to pin 9
int knobPin = 0;               // Potentiometer connected to Analog pin 0
 
void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
 
  Serial.begin(9600);
}
 
void loop()                     // run over and over again
{
  // Get the value of the potentiometer
  int knobValue = analogRead(knobPin);
 
  // Write the value to the serial port
  Serial.println(knobValue);
 
  // Write out the value to a LED on the board too
  // so we know the knob's working.
  analogWrite(ledPin, knobValue/4);
 
  delay(50);
}

And the Flash code (there's an instance of Grant's Fire component on stage called fireFX):

// Arduino controlled Flash fire.
// FlashBrighton hack by Paul Booth and Aral Balkan
import flash.errors.*;
import flash.events.*;
import flash.net.Socket;
 
var arduinoSocket:Socket;
 
init();
 
function init()
{
	initFire();
	openArduinoSocket();
}
 
function initFire()
{
	var params:Array = ["distortion","distortionScale","fadeRate","flameHeight","flameSpread","smoke"]
 
	var l:uint = params.length;
	for (var i:uint=0; i<l; i++)
	{
		fireFX[params[i]] = 0.35;
	}
}
 
function openArduinoSocket()
{
	//Create a socket connection to serproxy
	arduinoSocket = new Socket("localhost",5331);
	arduinoSocket.addEventListener(Event.CLOSE, closeHandler);
	arduinoSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
}
 
function closeHandler(event:Event):void
{
	// For some reason, the socket gets closed sometimes, re-open it.
	trace("[Workaround] Socket closed, reopening...");
	openArduinoSocket();
}
 
function socketDataHandler(event:ProgressEvent):void
{
	// Get the value from the potentiometer (0-1024) and
	// normalize it to (0-2) range for the fire component.
	var value:Number = Number(arduinoSocket.readUTFBytes(arduinoSocket.bytesAvailable));
	value = value/512;
 
	// fireFX is the instance name of Grant's fire component on stage.
	fireFX.flameHeight = value;
}

Finally, here is the full settings file we used for serproxy (serproxy.cfg). The lines we added are marked with ***:

# Config file for serproxy
# See serproxy's README file for documentation

# Transform newlines coming from the serial port into nils
# true (e.g. if using Flash) or false
newlines_to_nils=true

# *** The USB port that the Arduino is connected to ***
serial_device1=/dev/tty.usbserial-A60049Co

# Comm ports used
comm_ports=1,2,3,4

# *** Set the baud rate ***
comm_baud=9600

# Default settings
comm_databits=8
comm_stopbits=1
comm_parity=none

# Idle time out in seconds
timeout=300

# Port 1 settings (ttyS0)
net_port1=5331

# Port 2 settings (ttyS1)
net_port2=5332

# Port 3 settings (ttyS2)
net_port3=5333

# Port 4 settings (ttyS3)
net_port4=5334

A big thank-you to everyone who lent us stuff to play with (Jo, Matt, Jamie, Mark), to Kyle for his presentation, and to Seb and the FlashBrighton crew for organizing yet another wonderful meet-up.

18 Responses to “Turn up the heat in Flash with Arduino”


  1. 1 Brett Forsyth

    Hello Aral,

    Thanks for the link up. Are you going to FITC TO? I can show you some of the other stuff going on with arduino if you are.

  2. 2 Aral

    Hi Brett,

    Your post helped us kick things off in no time, so thank _you_ :)

    Unfortunately, I’m not going to be at FITC Toronto but I hope I’ll catch you at some other event.

  3. 3 zenbullets

    Dammit Aral, I read this expecting you’d worked out a way I could control the central heating with my Arduino board.

  4. 4 Aral

    LOL, Matt! The scary thing is that someone probably is doing that as we speak :)

  5. 5 j4mie

    Hi Aral, good to meet you yesterday, and I’m impressed that you managed to blog this so quickly!

  6. 6 Alvin

    Thanks to your post I found a cool thing like the Arduino board, glad they are distributing it in Singapore, gonna grab one and hook it up to flash and see what I can play with it :D

  7. 7 Aral

    @j4mie: I’ve just got an uncontrollable blogging reflex.

    @Alvin: Have fun! :)

  8. 8 iestyn

    Hey aral :)
    i’m basically modifying your code to get the values of a potentiometer into flash, but i’m getting some odd readings when I trace() it.

    Instead of getting what i get in the Arduino IDE:
    1023
    1023
    1023

    i get:
    1023
    10
    23
    1023

    Did you see anything like this happening?
    I’ve posted on the arduino forum too.

  9. 9 Aral

    Hi Iestyn,

    I don’t remember if we saw them with the potentiometer on the night (we hacked that together pretty quickly and were learning as we went) but I did see similar readings when playing with it at home with a light sensor. I’m wondering if serproxy is at fault here.

  10. 10 iestyn

    Hey Aral, FYI I updated my post here.

    I’ll sum it up:
    - Seems OK with AS2.
    - AS3 and flash.net.Socket coupled with Serial.println() seems to have a problem.
    - Using as3glue it seems to work OK.

    When I have time i’ll try to get to the bottom of this and post it on my blog.

  11. 11 Aral

    Hi Iestyn,

    Thanks for the update. I’ll check out as3glue :)

  12. 12 iestyn

    Just a brief update, should anyone get the same problem. I’ve posted more info, including code samples, on my blog.

  13. 13 Owen Bennett

    Hi Aral,

    Finally got my Arduino working with Flash - seems that there is a problem with serProxy and older intel MBPs. I fixed it using Arduino2Flash serial to socket server instead. The set up is a bit fiddly, but it works like a charm :) I found it also helpful to upload the Firmata firmware, which should be the last time you need to upload anything to the Arduino itself (until they release an update) and then just control it with Flash.

    Owen

  14. 14 Brett Forsyth

    @iestyn: Thanks for the links. What you are running into isn’t a big deal to fix yourself. Relying on a message to show up in one packet can be dangerous. If you look at any of the example serial read scripts in arduino they typically loop over the buffer until an end character is found and then the message is processed. So you can do the same in flash. If you are sending data to flash with the Serial.println() function you can test for the new line character as the end of message at which point you can process it. Hope that helps.

  15. 15 Aral

    Thanks Iestyn, Owen, Brett :)

  16. 16 Alex

    did anyone got to work that flash app on a web browser, my app works on flash player itself, but it doesn’t when i get that inside a browser!

  1. 1 links for 2008-03-20 at adoption curve dot net
  2. 2 Getting started with Django at Aral Balkan

Leave a Reply






Bad Behavior has blocked 0 access attempts in the last 7 days.