phidgets magnet sensor

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 .

phidgets magnet sensor closeup

import com.phidgets.*;<br /> import com.phidgets.events.*;<br /> 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”});
}
}