Tuesday, January 20, 2009

Communicating reading writing Arduino FLASH


Using TinkerProxy you can receive data from the Arduino doing something in FLASH.
(Using the socket server)
But you cannot send data back from FLASH to the Arduino
This can be done with Serproxy, but you have to change the config file to the right port:
(several links explaining for instance: http://www.tinker.it/en/Teaching/Flash )

when configured the Serproxy allows reading and writing.

so this Arduino script sends 0 and 100 alternately and listens for a value.
When this value comes it puts this value on ledport 9.

/* read and write script (combining two arduino sketchbook scripts
*
* blink without delay and send the values to a socket
* listen for data sent to the arduino and react on a ledPin 9
* from FLASH an int between 0-255 is sent
*
*/

int ledPin13 = 13; // LED connected to digital pin 13
int ledPin9 = 9;
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)

void setup()
{
pinMode(ledPin13, OUTPUT); // sets the digital pin as output

// begin the serial communication
Serial.begin(9600);
pinMode(ledPin9, OUTPUT);

}

void loop()
{

if (millis() - previousMillis > interval) {
previousMillis = millis(); // remember the last time we blinked the LED

// if the LED is off turn it on and vice-versa.
if (value == LOW)
{
value = HIGH;
Serial.println("100");
}
else
{
value = LOW;
Serial.println("0");
}

digitalWrite(ledPin13, value);

}

byte val;

// check if data has been sent from the computer
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255)
val = Serial.read();
// set the brightness of the LED
analogWrite(ledPin9, val);
}
}

Then the flash script is opening a socket port: (using a simple modified script found on the arduino playground)

function openArduinoSocket()
{
//Create a socket connection to serproxy

arduinoSocket = new Socket("localhost",5331);

arduinoSocket.addEventListener(Event.CLOSE, closeHandler); arduinoSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
}


reading the data from the port:
function socketDataHandler(event:ProgressEvent):void
{
// Get the value from the potentiometer (0-1024) and
// normalize it to (0-2) range for the fire component.
var nb : uint = arduinoSocket.bytesAvailable;

var bytes:ByteArray = new ByteArray();
var input:String = String(arduinoSocket.readUTFBytes(arduinoSocket.bytesAvailable));

var ST: String ="";

for (var i=0; i<> {
if ( !(input.charCodeAt(i)==10 || input.charCodeAt(i)==13))
ST += input.charAt(i);
} //we have to make a number from the chars or string getting in.....:-(
//right order????
var STNumber : Number = Number(ST.charAt(0))*100 + Number(ST.charAt(1))*10 + Number(ST.charAt(2))*1;

giveNumber.text = String(STNumber) ;
//writing it in a text field
//from arduino script coming out either 0 or 100
if( STNumber > 25 )
{
giveResult.text = "high";
}

else
{
giveResult.text = "low";
}
}


then comes the sending part: (just a timer which brightens up a led on the arduino)

var minuteTimer:Timer = new Timer(500);
minuteTimer.addEventListener(TimerEvent.TIMER, onTick);

minuteTimer.start();

var counter : int= 0;

function onTick(event:TimerEvent):void
{
counter +=10;
arduinoSocket.writeInt(counter % 250 ); //not higher then 250 arduinoSocket.flush();
}

ok next step is to take a rfid reader and make the rfid reader when seeing the right tag, sending a signal to the arduino, we need another USB port reader for this Phidget21 (we are using the phidget reader) ...

The Flash movie in connection with the rfid reader gave three mp3 sounds on three different tags. Then the functions of the Arduino socket were installed, the Arduino script a bit changed, adding a speaker to the Arduino.

The tags gave sounds in the FLASH movie, and at the same time the Arduino produced a tone on the speaker....an image (yeah, it is a shame you cannot here the sounds :-)





The Arduino can be seen, with the speaker, and the rfid reader with the tags, what cannot be seen are the Phidget21 and the serproxy, and the flash movie triggered by the rfid reader and sending data to the arduino...





0 comments: