Hi, everyone:
I'm now working on a project about connecting Arduino with Flash AS3. Here is a reference about how to control 4 LEDs by using Flash interface.
What confuse me is:
if(Serial.available() > 0) {
activeLED = Serial.read();
}
if(activeLED >= 50 && activeLED <= 57) {
int outputPort = activeLED - 48;
I don't know where dose these numbers come from, and what kind of information dose Flash really give to Arduino?
I need your help to solve the problem.
Thank you~~~
Arduino
int activeLED ;
int switchState = 0;
int lastSwitchState = 0;
int switchPin = 13;
void setup()
{
Serial.begin(19200);
pinMode(switchPin, INPUT);
for (int i = 2; i <= 9; i++) {
pinMode(i, OUTPUT);
}
}
void loop ()
{
switchState = digitalRead(switchPin);
if (switchState != lastSwitchState) {
if (switchState == 1){
sendStringToFlash("switchOn");
} else if (switchState == 0){
sendStringToFlash("switchOff");
}
}
lastSwitchState = switchState;
if(Serial.available() > 0) {
activeLED = Serial.read();
}
if(activeLED >= 50 && activeLED <= 57) {
int outputPort = activeLED - 48;
Serial.print("LED port ");
Serial.println(outputPort);
if(outputPort >= 2 && outputPort <= 9) {
for (int i = 2; i <= 10; i++) {
digitalWrite(i,LOW);
}
digitalWrite(outputPort, HIGH);
}
activeLED = 0;
} else if (activeLED) {
Serial.print("Invalid LED port ");
Serial.write(activeLED);
}
delay(50);
}
void sendStringToFlash (char *s)
{
while (*s) {
Serial.write(*s ++);
}
Serial.write(byte(0));
}
Arduino.as
import mx.events.EventDispatcher;
class Arduino extends XMLSocket {
private var _connected :Boolean = false; // is connected?
private var _host :String = "127.0.0.1"; // Host name or IP address
private var _port :Number = 5332 //read the config file of the socket server for this one
//EVENT DISPATCH MIXIN
function addEventListener(){}
function removeEventListener(){}
function dispatchEvent(){}
function dispatchQueue(){}
//constructor - provide a port number and a host ip adress
//read the documentation of the SerialProxy to better understandwhat this means
function Arduino(port, host) {
//initialize
super();
mx.events.EventDispatcher.initialize(this);
//trace("** Arduino ** initilizing constructor");
//check if the selected port is correct or set default
if(port == undefined) {
trace("** Arduino ** default port:"+_port+" initialized! to change it use new Arduino(onPortNumber)")
} else if ((_port < 1024) || (_port > 65535)) {
trace("** Arduino ** Port must be from 1024 to 65535 ! read the Flash Documentation and the serProxy config file to better understand")
} else {
_port = port;
}
//check for host or set default
if(host != undefined) {
_host = host;
}
//register and override responder functions
this.onConnect = onConnectToSocket;
this.onClose = onDisconnectSocket;
this.onData = onDataReceived;
//autoconnect
connect();
}
//---------------------------------------
// CONNECT and DISCONNECT + responders
//---------------------------------------
//connect to the XMLsocket
public function connect () {
trace("** Arduino ** Connecting to "+_host+":"+_port+" . . .");
super.connect(_host,_port);
}
//disconnects from the xmlsocket (not Arduino itself)
public function disconnect () {
if (_connected) {
trace("** Arduino ** disconnecting");
this.close()
_connected = false;
}
}
//XMLsocket responders
private function onConnectToSocket (success) {
//trace("** Arduino ** onConnectToSocket");
if (success) {
_connected = true;
//launch event
trace ("** Arduino ** Connection established.")
e_connectToSocket();
} else {
trace ("** Arduino ** Connection failed! you must launch the serialProxy first");
e_connectToSocketError();
}
}
//XMLsocket responders
private function onDisconnectSocket (success) {
_connected = false;
trace ("** Arduino ** onDisconnectSocket ** Connection closed by remote host.");
//launch event
e_disconnectSocket()
}
//---------------------------------------
// SEND and receive data from server
//---------------------------------------
//sends data to arduino
public function send(dataStr:String) {
trace("** Arduino ** send:" + dataStr)
if (_connected) {
if (dataStr.length) {
trace("** Arduino ** Sending \""+dataStr+"\"");
super.send(dataStr);
}
}
}
//overrides XMLSocket.onData in order to have pure string and not the full XML object
private function onDataReceived (str:String) {
trace("** Arduino ** onDataReceived str:"+str);
//launch event
e_onReceiveData(str)
}
//---------------------------------------
// EVENTS
//---------------------------------------
private function e_connectToSocket(){
var evt = {target:this, type:"onConnect"};
dispatchEvent(evt);
}
private function e_connectToSocketError(){
var evt = {target:this, type:"onConnectError"};
dispatchEvent(evt);
}
private function e_disconnectSocket(){
var evt = {target:this, type:"onDisconnect"};
dispatchEvent(evt);
}
private function e_onReceiveData (str:String){
var evt = {target:this, type:"onReceiveData"};
evt.data = str;
dispatchEvent(evt);
}
}
fla file
import Arduino;
var port:Number = 5331;
var a:Arduino = new Arduino(port);
aListener = new Object();
aListener.onConnect = function() {
setupBoard();
}
aListener.onReceiveData = function(evtObj:Object){
var str = evtObj.data
trace("Arduino: " + str);
if (str == "switchOff") {
_root.switchButton.gotoAndStop(1);
} else if (str == "switchOn") {
_root.switchButton.gotoAndStop(2);
}
}
a.addEventListener("onConnect", aListener);
a.addEventListener("onReceiveData", aListener);
function setupBoard() {
trace("Setting up LEDs...");
for (var i=2;i<=9;i++) {
var mc:MovieClip = _root["led"+i];
mc.pin = i.toString();
var owner = this;
mc.onPress = function() {
owner.a.send(this.pin);
for (var i=2; i<=10; i++) {
_root["led"+i].gotoAndStop(1);
}
_root["led"+this.pin].gotoAndStop(2);
}
}
}