Using Phidget Sensors to trigger an animation in as3.

After recently purchasing a Phidget 8/8/8 Interface kit I started to have some problem using these sensors to drive a flash as3 animation. The Phidget as3 examples are useful but don’t really give ‘real’ world examples for starting to use sensors to drive a flash animation.
The solution was to create my own event handerler, this event handler is called in the onSensorChangeFunction, which triggers when there is a onSensorChange Event.
You can download my a3 phidget slider example Phidget Magnet Example .

import com.phidgets.*;
import com.phidgets.events.*;
import caurina.transitions.*;var phid:PhidgetInterfaceKit;
phid = new PhidgetInterfaceKit();
phid.addEventListener(PhidgetEvent.CONNECT,onConnect);
phid.addEventListener(PhidgetEvent.DISCONNECT, onDisconnect);
phid.addEventListener(PhidgetEvent.DETACH,onDetach);
phid.addEventListener(PhidgetEvent.ATTACH,onAttach);
phid.addEventListener(PhidgetErrorEvent.ERROR, onError);
phid.addEventListener(PhidgetDataEvent.INPUT_CHANGE, onInputChange);
phid.addEventListener(PhidgetDataEvent.OUTPUT_CHANGE, onOutputChange);
phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);phid.open(”localhost”, 5001);
function onError(evt:PhidgetErrorEvent):void {
trace(evt);
}
function onAttach(evt:PhidgetEvent):void {
trace(evt);
}
function onDetach(evt:PhidgetEvent):void {
trace(evt);
}
function onConnect(evt:PhidgetEvent):void {
trace(evt);
}
function onDisconnect(evt:PhidgetEvent):void {
trace(evt);
}
function onInputChange(evt:PhidgetDataEvent):void {
trace(evt);
}
function onOutputChange(evt:PhidgetDataEvent):void {
trace(evt);
}
function onSensorChange(evt:PhidgetDataEvent):void {
benshanderler(evt);
}
function benshanderler(evt:PhidgetDataEvent):void {
//trace(evt);
if (evt.Index ==0 && evt.Data<400) {
trace(”Magnet 1 - ON”);
Tweener.addTween(slider1, {_frame:30, time:0.5, transition:”linear”});
} else if (evt.Index ==0 && evt.Data>400) {
trace(”Magnet 1 - OFF”);
Tweener.addTween(slider1, {_frame:0, time:0.5, transition:”linear”});
}
if (evt.Index ==1 && evt.Data<400) {
trace(”Magnet 2 - ON”);
Tweener.addTween(slider2, {_frame:30, time:0.5, transition:”linear”});
} else if (evt.Index ==1 && evt.Data>400) {
trace(”Magnet 2 - OFF”);
Tweener.addTween(slider2, {_frame:0, time:0.5, transition:”linear”});
}
if (evt.Index ==2 && evt.Data<400) {
trace(”Magnet 3 - ON”);
Tweener.addTween(slider3, {_frame:30, time:0.5, transition:”linear”});} else if (evt.Index ==2 && evt.Data>400) {
trace(”Magnet 3 - OFF”);
Tweener.addTween(slider3, {_frame:0, time:0.5, transition:”linear”});
}
}
hi… I have one Magnet sensor… and I’m trying your “applet” on index 0, (sensor 1), and it’s not working…
as output I’ve got :
[ Phidget Event: connect]
Ignoring policy file with incorrect syntax: xmlsocket://localhot:5001
and that’s it… I don’t get like “magnet 1 - ON” or either off…
can u help me please! It’s really important…
Comment by Julie — March 16, 2008 @ 2:45 pm
Ben,
Very nice demonstration. Your handling of the sensor inputs through if statements is exactly how it should be done, though you could just stick all that code in the onSensorChange function.
If you want to adjust the height of the slider based on the evt.Data you could change your tweener call to something like this (given range 0-500):
Tweener.addTween(slider1, {_frame:Math.round((evt.Data/500)*30), time:0, transition:”linear”});
This will tell the animation to go to the frame that is percentage-wise the same as how strong the sensor is reading, so a reading of 250 would essentially make the slider go to frame 15.
time is set to 0 because in theory as the sensor value climbs it will move 1 frame at a time just based on the math it is doing.
'&& evt.Data<400'from your if statements, and you can then remove all your ‘else if’s since a reading of 0 should animate to frame 0.I haven’t yet bought my phigdets, but I think I will be going for the 8/8/8 LCD display board with some magnet and vibration sensors to play around with.
Great Stuff, keep it up!
-Andrew Walpole
Comment by Andrew — March 27, 2008 @ 5:41 pm
Hey Andrew,
Thanks for the comment. I shall try that out. I’m having real fun teaching my self AS3 and Phidgets, and just trying to put more stuff out there to make it easier for others.
Changing the tweener based on a math.round feature sounds really interested. I shall look into it.
Comment by benarent — March 27, 2008 @ 5:53 pm
Hey,
.
Thanks for putting your project on line, I have a similar project were I want to move a slider, but in function of a stretch sensor. I am not that good at AS3. But I’ll will try your example
greetz
Comment by Femke — May 1, 2008 @ 1:24 pm