PDA

View Full Version : 8/8/8 outputs open on startup



Riley
03-28-2007, 06:39 PM
How do I open the Interface Kit without activating all of the outputs upon Attach? I am using a Touch Sensor to activate a video greeting and a door bell. The doorbell goes off upon start up, and the sensor (or GUI button) must be activated in order to turn it off.
Thanks for an suggestions!
Riley

The offending code if it helps:

Public Class Form1

Dim WithEvents IFKit As New Phidgets.InterfaceKit

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Label1.Text = "Sensor Activated: ? "
Label2.Text = "Status: Not Connected"
Label3.Text = "Serial Number: "

IFKit.open()


End Sub


Private Sub IFKit_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles IFKit.Attach


Label2.Text = "Status: Connected"
Label3.Text = "Serial Number: " & IFKit.SerialNumber



End Sub

Private Sub IFKit_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles IFKit.Detach
'Subroutine called when the Phidget is detached from the computer


Label2.Text = "Status: Not Connected"
Label3.Text = "Serial Number: "

End Sub

Private Sub IFKit_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles IFKit.Error
'A subroutine that deals with error conditions

MessageBox.Show(e.Description)
'Show the error that has occurred
Me.Close()

'Close the program

End Sub

Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()

'"Close" button closes program

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

'The subroutine that is run as the program closes
IFKit.outputs(5) = False
IFKit.close()


'This block of code ensures that the Phidget is disabled.
'It is not strictly necessary but recommended

End Sub



Private Sub Sensor1_Touch(ByVal sender As System.Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles IFKit.SensorChange

IFKit.outputs(5) = e.Value < 500

Label5.Text = "Sensor Working: Yes!"

'Activate IFKit Digital Output 1
'when value is below 500
'The range of the Phidget Touch Sensor is 0-1000
'so this will automatically reset the Digital Output to off
'when the Phidget Touch Sensor is no longer being touched

End Sub

Private Sub Sensor2_Touch(ByVal sender As System.Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles IFKit.SensorChange

Dim prc As New Process
prc.StartInfo.FileName = "C:\movies\1.avi"
prc.StartInfo.Arguments.Insert(0, "-Fullscreen")
prc.Start()


End Sub

Public Sub Sensor3_Touch(ByVal sender As System.Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles IFKit.SensorChange

If IFKit.outputs(5) = e.Value < 500 Then
IFKit.outputs(5) = True
End If


End Sub

Public Sub Sensor4_Touch(ByVal sender As System.Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles IFKit.SensorChange
If IFKit.outputs(5) = e.Value > 500 Then
IFKit.outputs(5) = False
End If
End Sub


Private Sub Button1_Down(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

IFKit.outputs(5) = True
Label1.Text = "Output Activated: Yes! "

'This causes Button1 to activate IFKit Digital Output 5

End Sub


Private Sub Button1_Up(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp

IFKit.outputs(5) = False
Label1.Text = "Output Activated: No! "

'This causes Button1 to deactivate IFKit Digital Output 5

End Sub


End Class

Alex
03-29-2007, 12:07 PM
Hi Riley,

Thanks for using Phidgets for your project! It sounds like a really cool one:) We'd love it if you get ahold of us with some pictures and such once it's completed so that we can blog about it!

Anyways, I know exactly how to do this in C#, but in VB.NET, I'm not 100% sure. I think what you need to use is the AddHandler and RemoveHandler statements in VB.NET. Here's a decent intro to it:

http://msdn2.microsoft.com/en-us/library/6yyk8z93(VS.80).aspx

Basically, you would have something like the following:


Public Class Form1

Dim WithEvents ifk As New Phidgets.InterfaceKit()


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' To add the delegate
AddHandler ifk.SensorChange, AddressOf ifk_SensorChange


' To remove the delegate
RemoveHandler ifk.SensorChange, AddressOf ifk_SensorChange

End Sub


Private Sub ifk_SensorChange(ByVal sender As Object, ByVal e As Phidgets.Events.SensorChangeEventArgs)

End Sub
End Class

Note that the ifk_SensorChange delegate does not have a Handles clause at the end.

All that aside though, judging by your code, it looks like you may have the SensorChanged event confused a bit. You have SensorChange events for each sensor, which really isn't the way that it is intended. The way that you have it set up has a bug, because from the code you've provided, any sensor that is touched will end up with the same results: they all activate everything. In your project, I'd suggest there only be one SensorChange event, and have a select statement or If statement checking the index of the sensor that changed, and then perform the actions you want.

Hope that helps:)

Riley
03-29-2007, 02:54 PM
Thanks very much for the direction, I'll give it a whirl... Just an FYI, this is the first code I have written since original BASIC about 25 years ago (I'm an architectural designer, not a programmer). So, I have no idea what I am doing - it is mostly trial and error, and sifting through others' examples using cut and paste... It is a wonder it works at all!

I'll definitely share the project when I get it completed/installed. It is for a Victorian house in London that is being completely redesigned with very modern sensibilities.

Riley

Alex
03-29-2007, 03:03 PM
ah, gotcha:) To tell ya the truth, for not programming for that long, you sure do catch on well. No worries though Riley. Trial and Error is your best way to learn, but if you're stuck on something, don't forget we're here to help ya out!