Pages: [1]   Go Down
Author Topic: As3 to Arduino.  (Read 2057 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. I am trying to do something very simple using flash with actionscript 3 to control my arduino uno through the  Arduino 0022 IDE.

I am simply trying to send a character to my board from my flash program which is routed through Serproxy but although I am getting the blinking light telling me data is being sent through the serial to my arduino, the code is not being executed.
I am using the slightly modified sample software "physical pixel" to test this:

Code:
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    
    if (incomingByte == 1) {
      digitalWrite(ledPin, HIGH);
    }
  
    if (incomingByte == 0) {
      digitalWrite(ledPin, LOW);
    }
  }
}


here is the as3 code i am trying to use:

Code:
import flash.net.Socket;
import flash.events.*;


var socket:Socket = new Socket("127.0.0.1", 5331);



button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
socket.writeInt(1);
}





As you can see it is rather simple yet the arduino doesn't seem to recognise my commands. I have tried getting it to respond by removing the if stamemnt and that works so it must be something to do with the parsing.

any help would be massively appreciated.
Dan.


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It took me less than a minute with google to find this:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html
Quote
   
writeInt(value:int):void
Writes a 32-bit signed integer to the socket.
You are then reading one of the 4 bytes written to the port. I'm wondering why you are using a 32 bit int to send a single byte of data. The writeByte() function might be more appropriate.

Perhaps you should flash the LED whenever there is serial data to process, just to confirm that the Arduino IS getting serial data.

Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

u need to flush the as3 socket to send the byte   
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. As I said I did try removing the if condition so that the code is executed as soon as serial data is received and that worked. The problem is that I have multiple actions on my arduino and so need multiple signals to call them.
With regards to flushing the As3 socket. where would I put that code?
Thanks. Dan.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hi. As I said I did try removing the if condition so that the code is executed as soon as serial data is received and that worked. The problem is that I have multiple actions on my arduino and so need multiple signals to call them.
With regards to flushing the As3 socket. where would I put that code?
If you are getting data to the Arduino, then, it should be obvious that flushing anything is not required.

Tell us about these "multiple actions", and what "multiple signals" are needed, and how a signal calls anything.

Your as3 code sends a single value, now, whenever the f1_ClickToGoToAndStopAtFrame callback is called. It appears that either the callback needs to decode the mouse position, and send a value based on the mouse position, or you need multiple callbacks, each sending a different value.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay. Basically I have a series of lights and a servo motor hooked up to my circuit. I want to be able to activate these individually from my computer and so need to be able to send unique commands over serial (via serproxy) to do so.
What the command is is not important so I just used the number one to test it.
I'm not sure I understand what you mean about the mouse position. I have used the mouseEvent purely as a means to send the command.
The if statements in the arduino code have been able to decode commands when I send them over the serial monitor in the arduino application but never over flash. This is the problem.
So I'm thinking that if I can send commands over serial via the serial monitor I need to format them the same as the serial monitor. Any idea how this might be done?
I just need to send over the data which my arduino is looking for in its if statement so it can execute the relevant code.
I hope that makes things slightly clearer...?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

in all my as3 code talking to the socket i use    socket.flush(); to be sure its flushed the socket and sent the data


like mentioned above i would stick to just sending bytes as commands. 0-255

then on the arduino i would have a switch statement for each command sorta like

  if (Serial.available() > 0)
  {
     // read byte in
     byte inByte = Serial.read();
     
     switch (inByte){
       case 0:
         // first command
         break;
       case 1:
         // second command
         break;
       case 2:
         // third command
         break;
     }
  }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi. The case command will work very well for my needs once I get it to recognise what I send.I tried the code you suggested and it did not work until I added quotations. (I was testing this in the serial monitor by the way)
So what does this mean? The arduino will only look for strings? So now I just need to send a string in the same format that the arduino recognises which is ascii right? What would be the correct method to format the socket payload correctly? I have tried WriteByte and this doesnt seem to work...
Thank you again for any help.
Dan.


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HAHA!!!!!! Ive bloomin done it I tells ya!! Basically I had the darn comm baud rate in the config file of serproxy set wrong. I synchronised that with my arduino program and sent the ascii number of the command in a writeByte method (72 for H). Phew, that was beginning to worry me. Cheers for all the help guys.
Dan.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i was about to say, the ardunio serial is just a basic UART it just sends/receives bytes simple bytes 0-255 lol

so flash socket.writeByte() should be ideal!
Logged

Pages: [1]   Go Up
Jump to: