Home > database >  Can't Send Command from Arduino to Flash AS3
Can't Send Command from Arduino to Flash AS3

Time:11-08

I can control Arduino From Flash its working fine for me as attached but I need to make it reverse, I need to control the flash from Arduino if I press the push button (pin13) in the flash it will gotoAndStop Frame (2) or (1). Please help Thanks

here is all the source file https://drive.google.com/file/d/1PvoD41Is8_gcl5bJpVWlhqtvre3twENK/view?usp=sharing

working video https://drive.google.com/file/d/15Z2onMf301p5DHEFhetYEXKrjMQAKAWP/view?usp=sharing

Arduino Connection Code Working fine

import flash.net.Socket;

var _socket:Socket = new Socket();
var _proxyAddress:String = "127.0.0.1";
var _proxyPort:uint = 5331;
_socket.addEventListener(Event.CONNECT, onConnect);
_socket.connect(_proxyAddress, _proxyPort);
      
function onConnect(event:Event):void{
        trace("Socket Connected");
    }

Flash Code for Arduino on and off

Frame 1
MovieClip(root)._socket.writeUTFBytes("f");
MovieClip(root)._socket.flush();

Frame 2
MovieClip(root)._socket.writeUTFBytes("n");
MovieClip(root)._socket.flush();

Flash Button Code

onBtn.addEventListener(MouseEvent.MOUSE_DOWN,btn1);
function btn1(evt:MouseEvent):void
{
    gotoAndStop(1);
}

offBtn.addEventListener(MouseEvent.MOUSE_DOWN,btn2);
function btn2(evt:MouseEvent):void
{
    gotoAndStop(2);
}

Arduino Code

#define BULB 13
int incoming = LOW;

void setup(){
Serial.begin(57600);
Serial.println("INITALIZING");
pinMode(BULB, OUTPUT);
Serial.println("READY");
}

void loop(){
  
if(Serial.available () > LOW ){
incoming = Serial.read();

if(incoming == 'n'){
digitalWrite(BULB, LOW);
}
else if(incoming == 'f'){
digitalWrite(BULB, HIGH);
}

  }
}

CodePudding user response:

It's been more than a decade since I used actionscript and I no longer have Flash license, so unfortunately I won't be able to replicate your setup.

That being said, I might have a few hopefully helpful pointers:

Double check serproxy.cfg is properly configured. Your serproxy.cfg file is configured to connect to an Arduino via Windows on port COM4 with baudrate 57600. Double check:

  • the port and buad rate match
  • the device is connected via USB
  • the port isn't busy/already open in Arduino's Serial Monitor (or other tools reading/writing to the same COM port)

When I last used serproxy I was using OSX and had to configure serial_device instead of comm_ports. Not 100% sure this will work, but if COM4 is indeed your Arduino's port, you can try settings serial_device=COM4.

I am not sure why, but not all serproxy downloads worked for me. I had luck with the version that shipped with as3glue. (FWIW, you can also check out my Arduino & Actionscript talk resources from 2010. It does not include serproxy, but does have a few Arduino/Flash examples and hopefully helpful slides).

Personally, as much as I've enjoyed actionscript, I'd recommend switching languages given Flash Player is no longer supported.

The closest syntax I can think of is javascript and p5.js is a creative friendly library. Its drawing functions (rect(), image(), etc.) as well as DOM (e.g. createButton())) should help reacreate your buttons (onBtn, offBtn).

In terms of serial control, you can use the p5.serialport library. It uses p5.serialcontrol similar to serproxy (but using WebSockets instead of raw TCP sockets). Once you've configured your serial port correctly you can start adapting their writeExample. It writes 'L'/'H', you can 'n','f' and use two buttons instead of mousePressed().

(If you're feeling more adventurous you can try Python and pyserial instead. The syntax is quite different from actionscript, but also a lot less verbose. For the UI, if it's just two buttons the tkinter might suffice, otherwise there are plenty of other snazzy GUI libraries such as kivy)

Update Viewing your video and Re-reading your question made this section stand out:

I need to make it reverse, I need to control the flash from Arduino if I press the push button (pin13) in the flash it will gotoAndStop Frame (2) or (1).

The first step is to change the Arduino code so it reads a button instead of lighting an LED:

  • change pinMode(13, INPUT)
  • use digitalRead() instead of digitalWrite()
  • one idea is to simply write a single byte: a 1 or a 0 depending on the button pin state (returned by digitalRead()).

Here's a basic example:

#define BUTTON 13

void setup(){
    Serial.begin(57600);
    pinMode(BUTTON, INPUT);
}

void loop(){
    Serial.write(digitalRead(BUTTON));
    // wait 40ms ~= 25fps
    delay(40);
}

Note:

  • Serial.write is not the same as Serial.println(): it doesn't send a string nor the end line character (\n). Be aware you might not see this character in Serial Monitor as plain ASCII, but it should be visible as hex. (for example CoolTerm has this feature)
  • delay() is added to not flood serial too many messages too often (limiting output to ~25fps)
  • this sketch is continuously outputing button state: you may want to "debounce": check if the state has changed (from off to on or the reverse) to only send a serial message when the state changed

Here's a basic example demonstrating debouncing:

#define BUTTON 13

bool wasOn, isOn;

void setup(){
    Serial.begin(57600);
    pinMode(BUTTON, INPUT);
}

void loop(){
    isOn = digitalRead(BUTTON);

    if(!wasOn && isOn){
        wasOn = true;
        Serial.write(1);
    }
    if(wasOn && !isOn){
        wasOn = false;
        Serial.write(0);
    }
    // wait 40ms ~= 25fps
    delay(40);
}

The above code isn't tested with an Arduino, so it may require testing debugging. (CoolTerm's hex view will be useful).

The other thing you might need to double check is digitalRead() returns 1 if the button is pressed or 0 (and adjust what Serial.write if the expected state appears flipped in actionscript). Alternatively change the gotoAndStop() frame in actionscript.

Regarding as3 code, you might need to use Socket's socketData event and readBoolean(). Again, without Flash installed, this is untested, but hopefully a useful guide towards how you might to implement this (meaning debugging/tweaking may be required):

import flash.net.Socket;
import flash.events.Event;
import flash.events.ProgressEvent;

var _socket:Socket = new Socket();
var _proxyAddress:String = "127.0.0.1";
var _proxyPort:uint = 5331;
_socket.addEventListener(Event.CONNECT, onConnect);
// listen to incoming Arduino data
_socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
_socket.connect(_proxyAddress, _proxyPort);
      
function onConnect(event:Event):void{
    trace("Socket Connected");
}
// called when new Arduino data arrives
function onData(event:ProgressEvent):void{
    // read the 1 or 0 from Arduino
    var isOn:Boolean = _socket.readBoolean();
    // print received data
    trace("arduino data event", event, "isOn =",isOn);
    // change frames according to received boolean value 
    if(isOn){
        gotoAndStop(1);
    }else{
        gotoAndStop(2);
    }
}
  • Related