Reply to comment

Phidgets Program

First Objective: Get the LED to simply Flash on and off without any external user input.

Second Objective: Get the LED to Flash on and off with it's speed dependant from the input from a rotation touch sensor.

First, an interface object is declared then initialized, and then tell it to attempt to open a connection to the kit.

private InterfaceKit interFaceKit; interFaceKit = new InterfaceKit();

interFaceKit.Open();

Once we tell it to open, it may take a few moments to actually connect to the kit as it is called asynchronously (program won't hang while it's connecting). We need to check if it is attached(opened) before attempting to do anything to the outputs.

So, we are going to placle an if inside of the loop.

if (interFaceKit.Attached)
{
if (!isLEDon){
interFaceKit.outputs[0] = true;isLEDon = true;}

else
{
interFaceKit.outputs[0] = false;isLEDon = false;
}
lbl_LEDStatus.Text = "LED ON: " + isLEDon.ToString();

}

The interFaceKit.Attached is a bollean property that will get a true / false status wether the kit is attached. If it is, we can do stuff to it, if not, we don't do anything.

Then we can access the on/off states of the outputs through the interFaceKit.Outputs[(outputNumber)], and set it to true if we want it to be turned on, and then false if we want it turned off. Running this code would strobe the led at the default amount the timer object ticks.

So now we want the value of how fast it changes based on the position of a touch sensor.

We need to add another Interface Kit object to our initialize method because the rotation touch sensor is considered an interface kit.

private InterfaceKit rotationSensor;

rotationSensor = new InterfaceKit();

rotationSensor.Open(27515);

We pass in 27515 as that is it's ID number. We need to pass it's ID number in because we have more than one interface kit attached and the computer needs to know which one to control. we don't need to use the ID number on the other one because it is the only interface kit left and the software is able to assign it automatically.

Then so we can get the status of the sensor when the sensor is changed, we need to setup a method that will be called when the event of the sensor changing is fired.

So we create a onSensorChanged Method and have an object and Sensor Change Arguments passed in:

private void onRotate(object obj, SensorChangeEventArgs args)

From the SensorChangeEventArgs, we can access the current state of the sensor.

And in the initalization method, we need to "subscribe" to the event so it gets called everytime the base class calls it:

rotationSensor.SensorChange += new SensorChangeEventHandler(onRotate);

Then, insisde of the onRotate method, we want to add our code to change what the speed of the light flashing is. But it can't be 0 since the amount of time between intervals can not be 0, it would be considered out of range, and crash the program. So we need to check it before we assign it to the interval.

if (e.Value > 0)

{
int rate = e.Value / 10;

if (rate != 0)
this.timer1.Interval = rate;
else
this.timer1.Interval = 1;
this.lbl_JoyX.Text = "Current Rotation Position: " + e.Value.ToString();

}
First, we reduce the rate by a dividend of 10 since the rate goes from 0-1000 and we dont' want to wait so long to see a result. Then we check if the rate is not equal to o. And if it is inot, we set the timer interval ot the rate, and if it is equal to 0, we set it to 1. Then we change a label on the form that outputs the current rotation position of the sensor.

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may link to Gallery2 items on this site using a special syntax.

More information about formatting options