switching off and on.

hi, i’ve came across a flash actionscript which will switch on the LED which connected to arduino whenever the button is depressed.

may i know how do i modify the script so that it will switch off when i press the second time on the same button?

//------------------------------------------------------------------------ Basic Arduino

import Arduino;

var port:Number = 5333; // 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() {

aListener.onReceiveData = function(evtObj:Object){
//Store the received data in a string
var str = evtObj.data
trace("Arduino says – " + str);

//Tests for a particular message from Arduino, and acts accordingly
if (str == “switchOff”) {
else if (str == “switchOn”) {


// Binds your custom listeners to the Arduino object (a)

//------------------------------------------------------------------------ That’s all!

function setupBoard() {
trace(“Connection established. Now setting up LEDs…”);

// Each physical LED gets a virtual copy in Flash
for (var i=2;i<=9;i++) {

// Associates a pin with each movieclip
var mc:MovieClip = _root[“led”+i];
// Stores the digital i/o pin number for later use
mc.pin = i.toString();

var owner = this;
mc.onPress = function() {
// Send the pin data to Arduino, telling it what pin to turn on

// Turn all virtual LEDs OFF
for (var i=2; i<=10; i++) {
// Now turn the selected virtual LED ON



/* ------------------------------------------------

  • Flash To airport signal test

  • ------------------------------------------------ */
    int activeLED ; // What LED has Flash requested we turn on? Numbers 2-9, for a total of 8 possible LEDs
    int switchState = 0; // What is the current switch state?
    int lastSwitchState = 0; // Was the switch previously on, or off?
    int switchPin = 13; // Which digital i/o pin is setup for the switch?

void setup() {
Serial.begin(19200); //setup serial conversation at 19200 bauds
pinMode(switchPin, INPUT); // sets the switch pin as input
for (int i=2; i<=9; i++) {
pinMode(i, OUTPUT); // sets the digital pins 2-9 as output

void loop () {

//------------------------------------------------------------------------ SWITCH LOGIC
// get the current state of the switch from our board
switchState = digitalRead(switchPin);
// if the switch has changed states, then let Flash know about it
if (switchState != lastSwitchState) {
if (switchState == 1){
else if (switchState == 0){
// now store the state for later comparison
lastSwitchState = switchState;

//------------------------------------------------------------------------ LED LOGIC

// checks if a serial message has arrived to the board
if(Serial.available() > 0) {
//must be a request to light up an LED
//activeLED should be a number 2-9, but in binary it will come in as 50-57
activeLED = Serial.read();

if(activeLED >= 50 && activeLED <= 57) {
//a valid LED signal has been received from Flash
// convert the byte to an int - getting ready for digitalWrite()
int outputPort = activeLED-48;

Serial.print(“Requesting LED port:”);

if(outputPort >= 2 && outputPort <= 9) {
// turns all the LEDs OFF
for (int i=2; i<=10; i++) {
// then turns your selected LED ON
digitalWrite(outputPort, HIGH);


// when we’re done, default the activeLED to nothing
activeLED = 0;
else if (activeLED) {
Serial.print(“Requesting invalid LED port:”);
int outputPort = activeLED-48;
if (outputPort <= 2 && outputPort >=9){
for (int i=2; i<=10;i++){
digitalWrite(outputPort, LOW);

//slows down the visualization: each sentence will be emitted every .5 seconds



/*send to flash

  • it simply adds the byte(0) at the end
  • needed by the XMLSocket to know that the transmission is over.
  • read the XMLSocket Actionscript Class documentation for more details
    void sendStringToFlash (char *s) {
    while (*s) {
    Serial.print(0); //notify the XMLSocket that the comunication is over

any1 able to help? this is the code for arduino

To enter code into a post click the button marked with a Hash # symbol in the toolbox. Then paste the code between those quotes.