Friday, January 9, 2009

Arduino and Flash Second Attempt

First in the Arduino program we created a simple program reading a light sensor (combined with a resistor).

Using this script:
//Arduino script
/*
int potPin = 0; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
Serial.print(val,DEC);
Serial.print("\n");
}
*/

Using tinkerproxy this time, configuring the COM-port

Then starting Flash, with a socket:

arduinoSocket = new Socket("localhost",5331);
arduinoSocket.addEventListener(Event.CLOSE, closeHandler);
arduinoSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);

then reading the incoming bytes we have to make from this string a number:

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);

}

var STNumber : Number = Number(ST.charAt(0))*100 + Number(ST.charAt(1))*10 + Number(ST.charAt(2))*1;

//we have to check if the order for one-decimal-hundred's is right??

ok this gave us a start.
but the values read were rather erratic

so we changed the script on the Arduino so that either a 0 or a 100 is sent:

//Arduino script
/*
int potPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
if (val > 100)
Serial.print(0,DEC);
else
Serial.print(100,DEC);
Serial.print("\n");
}
*/

then the reading becomes:

//from arduino script coming out either 0 or 100
if( STNumber > 25 )
{
giveResult.text = "high";
}
else
{
giveResult.text = "low";
if ( myVideo == null )
{
myVideo = new VideoExample( videoPlayer );//loading video in videoPlayer Sprite
}
}

so this starts a video, in a separate class (from a FLASH example)

but we dont want to start this video all the time, :-)

var now2:Date = new Date();
var timeDifference: Number = now2.getTime() - timeTag1;

//here give length in time of video : this blocks reading
var lengthOfVideoInSeconds = 15;

if ( timeDifference /1000 > lengthOfVideoInSeconds )
{
myVideo = null;
timeTag1 = now2.getTime();
}


This procedure can be altered, and should be optimized, but it worked.

1 comments:

José said...

This would be very useful to me and my students (I'm a teacher in lisbon). But I'm not beeing able to make it work.
Is this OK?


for (var i=0; i<>
{