Arduino & Flash AS3 (part 5)

(continued from Arduino & Flash AS3 (part 4)).

At this point you should have the Arduino communicating with Flash.
Unplug the Arduino from the USB cable.

We are now going to build this simple button circuit :

Grab your bread board, button, resistor and jumper wires and lets build a button.

The button schematic is quite simple, but for some it may seem daunting, so I broke down the symbols.

The following represents a resistor :

And the button (switch) is represented by :

All we have to do is make sure that we create the circuit in accordance to the schematic, here’s my breadboard:

Here’s a step by step:

(1) Connect a jumper wire from the GND pin on the Arduino to a horizontal row on the breadboard.
In my example its the green wire connected from the Arduino to the bottom horizontal row on my breadboard. The entire bottom horizontal row is now a grounded row.

(2) Connect a jumper wire from the 5V pin on the Arduino to a horizontal row on the breadboard.
In my example its the orange wire connected to the row above the ground horizontal row on my breadboard. The entire horizontal row now is a powered row.

(3) Connect the 10 k resistor to the grounded row to an empty vertical row.

(4) Connect a jumper wire from the same vertical row of the resistor to the Arduino digital pin 2 (aka data pin).
In my example its the red wire placed above the resistor on the breadboard connected to the Arduino board.

(5) Add a post from your momentary button to the same vertical row of the resistor and digital pin wire to complete the grounded circuit.

(6) Add the other end of your button post to an empty vertical row.

(7) Add a jumper wire from the button post which is placed in the new empty vertical row to the powered horizontal row on the breadboard, this completes the powered circuit.

Plug the USB cable back into the Arduino.

Open up the actionscript file and navigate to the onReceiveFirmwareVersion method and uncomment the line of code //setUpArduino() to read:

private 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.");
            } else {
               setUpArduino();
                startSound();
            }
        }

Navigate to the actual method setUpArduino() :

private function setUpArduino():void {
            _arduino.enableDigitalPinReporting();
            _arduino.setPinMode(2, Arduino.INPUT);
}

The first line of code does what it says, it enables digital pin reporting.
The second line of code directs the Arduino to listen for pin 2 INPUT.

The listener we set up in the initArduino() method will be called when ever digital data has been received from the Arduino board.

_arduino.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData);

In the onReceiveDigitalData method, I have written logic to handle the button data. It simply turns on or off the mp3 based on the pins value 0 or 1 (aka “high” and “low”).

Start up the serial proxy application, and test your flash movie.
You should hear your music playing and upon pressing the button the music should stop. Upon releasing the button the music should start up again.

This concludes my lengthy tutorial/posting on Arduino & Flash AS3.

As you can see the possibilities of Flash and Arduino are endless, next time I will cover the ability to use an Ultra Sonic Sensor to scale images in flash.

Last but not least I must thank:
Erik Sjodin and Bjoern Hartmann. for providing the AS3Glue library and preventing me from re-inventing the wheel. I also would like to thank the Arduino community for their endless support!

Till then happy tinkering.

Download Source:
Flash AS3Glue Button Example (978)

4 thoughts on “Arduino & Flash AS3 (part 5)

  • March 10, 2010 at 8:00 am
    Permalink

    I am not going to be original this time, so all I am going to say that your blog rocks, sad that I don’t have suck a writing skills

    Reply
  • March 13, 2010 at 1:43 am
    Permalink

    I read a article under the same title some time ago, but this articles quality is much, much better. How you do this?

    Reply
  • May 26, 2011 at 4:24 pm
    Permalink

    This is awesome!

    Had to change some stuff for the Arduino UNO.

    In serproxy.cfg I’ve set the modem to
    serial_device1=/dev/cu.usbmodem1d21
    Using a Mac Pro.

    And apparently the Firmware checking had to be changed to. From 2 to 22 (alter that if it doesn’t work).

    Here’s my code
    http://www.cl.ly/0j3q0M3f243C1y422U2m

    And for the Arduino, I just connected two switches instead of one. To digital pin 2 and 4.

    Reply
  • October 17, 2012 at 8:37 am
    Permalink

    Thank you sooo much for this tutorial!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: