Capture a button sequence in actionscript

Hi all,

I am using flash to control a toy rc car transmitter via an arduino microcontroller. I have written the code for the basic direction controls (see below). I would like to add more funtionality though by recording keypress times and have the car run through a set of movements at the click of a button.

I would appreciate a nudge in the right direction... Thanks.

package
{
import net.eriksjodin.arduino.Arduino;
import net.eriksjodin.arduino.events.ArduinoEvent;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

public class code extends MovieClip
{
var arduino:Arduino = new Arduino("127.0.0.1", 5331);

var forwardPin:Number = 1;
var reversePin:Number = 2;
var turnleftPin:Number = 3;
var turnrightPin:Number = 4;

var forward:Boolean = false;
var reverse:Boolean = false;
var turnleft:Boolean = false;
var turnright:Boolean = false;

public function code()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, OnPress);
stage.addEventListener(KeyboardEvent.KEY_UP, OnRelease);
stage.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
}

function onArduinoStartup(e:ArduinoEvent):void
{
arduino.setPinMode(forwardPin, Arduino.OUTPUT);
arduino.setPinMode(reversePin, Arduino.OUTPUT);
arduino.setPinMode(turnleftPin, Arduino.OUTPUT);
arduino.setPinMode(turnrightPin, Arduino.OUTPUT);

arduino.writeDigitalPin(forwardPin, Arduino.LOW);
arduino.writeDigitalPin(reversePin, Arduino.LOW);
arduino.writeDigitalPin(turnleftPin, Arduino.LOW);
arduino.writeDigitalPin(turnrightPin, Arduino.LOW);
}

public function OnEnterFrame(event:Event):void
{
if ( forward)
{
trace("forward");
arduino.writeDigitalPin(forwardPin, Arduino.HIGH);
forwardArrow.gotoAndStop("pressed");
}
else
{
arduino.writeDigitalPin(forwardPin, Arduino.LOW);
forwardArrow.gotoAndStop("released");
}


if ( reverse)
{
trace("reversing");
arduino.writeDigitalPin(turnleftPin, Arduino.HIGH);
reverseArrow.gotoAndStop("pressed");
}
else
{
arduino.writeDigitalPin(reversePin, Arduino.LOW);
reverseArrow.gotoAndStop("released");
}


if ( turnleft)
{
trace("turning left");
arduino.writeDigitalPin(turnleftPin, Arduino.HIGH);
leftArrow.gotoAndStop("pressed");
}
else
{
arduino.writeDigitalPin(turnleftPin, Arduino.LOW);
leftArrow.gotoAndStop("released");
}


if ( turnright)
{
trace("turning right");
arduino.writeDigitalPin(turnrightPin, Arduino.HIGH);
rightArrow.gotoAndStop("pressed");
}
else
{
arduino.writeDigitalPin(turnrightPin, Arduino.LOW);
rightArrow.gotoAndStop("released");
}

}

//this is the function the KeyboardEvent.KEY_DOWN listener uses
public function OnPress(event:KeyboardEvent):void
{

switch( event.keyCode )
{
case Keyboard.UP:
forward = true;
break;
case Keyboard.DOWN:
reverse = true;
break;
case Keyboard.LEFT:
turnleft = true;
break;
case Keyboard.RIGHT:
turnright = true;
break;
default:
break;
}
}
//this is the function the KeyboardEvent.KEY_UP listener uses
public function OnRelease(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
forward = false;
break;
case Keyboard.DOWN:
reverse = false;
break;
case Keyboard.LEFT:
turnleft = false;
break;
case Keyboard.RIGHT:
turnright = false;
break;
default:
break;
}
}
}
}

I guess I would have to use the timer class to record the legnth of the keypresses and the legnth between keypresses. No idea of where to start though.

You could start by editing your original post to put the sketch into a Code (use the # button on the posting editor's toolbar) box.

Done. Thanks for the advice!

Do you want the Arduino to record the time that it was doing various things, so that it can repeat a set of actions, or are you expecting actionscript to do that?

I figured it would be done in the actionscript... it would capture the legnth of and between the keypresses and then change the bolean with correct timing and sequence.

Perhaps you should find an actionscript forum, then. It doesn't appear that it's an Arduino issue that you have.

I've posted it in a couple actionscript forms, no luck tho. But you're right guess its not really and arduino issue. Thanks